74 lines
2.9 KiB
Bash
Executable File
74 lines
2.9 KiB
Bash
Executable File
#!/bin/bash
|
|
###
|
|
### ~/.config/hypr/scripts/screenshot/screenshot_window.sh
|
|
###
|
|
|
|
time=$(date +%Y-%m-%d-%H%M%S)
|
|
dir="$(xdg-user-dir PICTURES)/Screenshots"
|
|
file="Screenshot_${time}.png"
|
|
|
|
mkdir -p "$dir"
|
|
|
|
# Build selection boxes for windows on visible workspaces
|
|
ids=$(hyprctl monitors -j | jq -c '[.[].activeWorkspace.id]')
|
|
boxes=$(hyprctl clients -j | jq -r --argjson ids "$ids" '
|
|
[.[] | select(.workspace.id | IN($ids[]))] | .[] |
|
|
"\(.at[0]),\(.at[1]) \(.size[0])x\(.size[1]) \(.title)"
|
|
' | cut -d' ' -f1,2)
|
|
|
|
# Let the user pick a window (click the box); exit silently if cancelled
|
|
geometry=$(slurp -r <<< "$boxes") || exit 1
|
|
|
|
# Expand the selection by border + gap so the capture includes the border
|
|
border=$(hyprctl getoption general:border_size -j | jq -r '(.int // 0)')
|
|
gap=$(hyprctl getoption general:gaps_out -j | jq -r '(.int // 0)')
|
|
pad=$(( border + gap ))
|
|
|
|
geometry=$(awk -v p="$pad" '
|
|
{
|
|
split($1, pos, ",");
|
|
split($2, size, "x");
|
|
printf "%d,%d %dx%d\n",
|
|
pos[1] - p, pos[2] - p, size[1] + p * 2, size[2] + p * 2
|
|
}' <<< "$geometry")
|
|
|
|
# Capture the window, save it and copy to clipboard
|
|
grim -g "$geometry" "$dir/$file" || exit 1
|
|
wl-copy -t image/png < "$dir/$file"
|
|
|
|
if command -v notify-send >/dev/null 2>&1; then
|
|
notify-send -a Screenshot -i camera-photo "Screenshot saved" "$file"
|
|
fi
|
|
|
|
# Open the editor if satty is available (blocks until it is closed).
|
|
# satty writes the annotated image to --output-filename on Ctrl+s (and on
|
|
# Esc/Enter here) and exits immediately. It is launched floating, without
|
|
# decorations, with the pen preselected and moved over the captured area.
|
|
if command -v satty >/dev/null 2>&1; then
|
|
x=$(cut -d, -f1 <<< "$geometry")
|
|
y=$(cut -d, -f2 <<< "$geometry" | cut -d' ' -f1)
|
|
satty --filename "$dir/$file" --output-filename "$dir/$file" \
|
|
--copy-command wl-copy --actions-on-escape save-to-file --early-exit \
|
|
--floating-hack --no-window-decoration --initial-tool brush &
|
|
pid=$!
|
|
for _ in $(seq 1 20); do
|
|
addr=$(hyprctl clients -j 2>/dev/null | jq -r --argjson p "$pid" '.[] | select(.pid == $p) | .address' | head -1)
|
|
[ -n "$addr" ] && break
|
|
sleep 0.1
|
|
done
|
|
if [ -n "$addr" ]; then
|
|
# Lua config (Hyprland 0.55+) dispatches through hl.dsp.*, so try
|
|
# hyprctl eval first, then the raw lua dispatch, then classic syntax.
|
|
hyprctl eval "hl.dispatch(hl.dsp.window.move({ x = $x, y = $y, window = 'address:$addr' }))" >/dev/null 2>&1 ||
|
|
hyprctl dispatch "hl.dsp.window.move({ x = $x, y = $y, window = 'address:$addr' })" >/dev/null 2>&1 ||
|
|
hyprctl dispatch movewindowpixel "exact $x $y,address:$addr" >/dev/null 2>&1
|
|
fi
|
|
wait "$pid"
|
|
wl-copy -t image/png < "$dir/$file"
|
|
fi
|
|
|
|
# Compress the resulting image in the background if oxipng is available
|
|
if command -v oxipng >/dev/null 2>&1; then
|
|
oxipng --strip safe -o 4 "$dir/$file" &
|
|
fi
|