dotfiles/sway/scripts/screenshot-region.sh

30 lines
824 B
Bash
Executable file

#!/usr/bin/env bash
# Region screenshot for swayfx.
# Drag a region with slurp, save a timestamped PNG to ~/Media (matching the
# naming of the existing screenshots there) and copy the same image to the
# Wayland clipboard (paste with Ctrl+V).
set -uo pipefail
dir="${HOME}/Media"
mkdir -p -- "$dir" || exit 1
# slurp exits non-zero when the selection is cancelled (Esc).
geometry="$(slurp)" || exit 0
[[ -n "$geometry" ]] || exit 0
# Same style as existing files (2026-02-08_18-05.png); add _n only if two
# captures land in the same minute, so nothing is ever overwritten.
base="$dir/$(date +%Y-%m-%d_%H-%M)"
file="${base}.png"
n=1
while [[ -e "$file" ]]; do
file="${base}_${n}.png"
((n++))
done
if grim -g "$geometry" - | tee -- "$file" | wl-copy --type image/png; then
exit 0
fi
rm -f -- "$file"
exit 1