Files
configs/.config/waybar/scripts/capture-share-shot.sh
2026-04-17 19:38:35 +02:00

104 lines
2.4 KiB
Bash
Executable File

#!/usr/bin/bash
set -euo pipefail
base_dir="/mnt/data/media/share/pictures"
tmp_file="$(mktemp --suffix=.png)"
selection_file="$(mktemp)"
cleanup() {
rm -f "$tmp_file" "$selection_file"
}
trap cleanup EXIT
trim_whitespace() {
local value="$1"
value="${value#"${value%%[![:space:]]*}"}"
value="${value%"${value##*[![:space:]]}"}"
printf '%s\n' "$value"
}
choose_destination_dir() {
if [[ ! -d "$base_dir" ]] || ! mountpoint -q /mnt/data; then
return 1
fi
{
printf '/\n'
find "$base_dir" -mindepth 1 -type d -printf '%P\n' | sort
} > "$selection_file"
if selected=$(wofi --dmenu --prompt 'Save screenshot path' < "$selection_file"); then
if [[ -z "$selected" || "$selected" == "/" ]]; then
printf '%s\n' "$base_dir"
else
printf '%s/%s\n' "$base_dir" "$selected"
fi
return 0
fi
printf '%s\n' "$base_dir"
}
choose_filename() {
local default_name="screenshot"
local selected=""
if selected=$(printf '%s\n' "$default_name" | wofi --dmenu --prompt 'Save screenshot filename'); then
selected=$(trim_whitespace "$selected")
else
selected="$default_name"
fi
if [[ -z "$selected" ]]; then
selected="$default_name"
fi
selected="${selected%.png}"
if [[ -z "$selected" ]]; then
selected="$default_name"
fi
printf '%s\n' "$selected"
}
build_unique_path() {
local destination_dir="$1"
local file_stem="$2"
local candidate="$destination_dir/$file_stem.png"
local counter=1
if [[ ! -e "$candidate" ]]; then
printf '%s\n' "$candidate"
return
fi
while [[ -e "$destination_dir/$file_stem-$counter.png" ]]; do
counter=$((counter + 1))
done
printf '%s\n' "$destination_dir/$file_stem-$counter.png"
}
if ! region="$(slurp)"; then
exit 0
fi
if ! grim -g "$region" - | swappy -f - -o "$tmp_file"; then
exit 1
fi
wl-copy --type image/png < "$tmp_file"
if destination_dir="$(choose_destination_dir)"; then
mkdir -p "$destination_dir"
file_stem="$(choose_filename)"
destination_path="$(build_unique_path "$destination_dir" "$file_stem")"
cp "$tmp_file" "$destination_path"
notify-send 'Screenshot saved' "$destination_path
Copied to clipboard"
else
notify-send 'Screenshot copied' 'NAS picture share is not available; image copied to clipboard only'
fi