Skip to content

Instantly share code, notes, and snippets.

@thennothinghappened
Last active March 14, 2026 01:50
Show Gist options
  • Select an option

  • Save thennothinghappened/988cd6b89f550e4983c361e47516601e to your computer and use it in GitHub Desktop.

Select an option

Save thennothinghappened/988cd6b89f550e4983c361e47516601e to your computer and use it in GitHub Desktop.
screenshot
#! /bin/bash
#
# Wrapper script for taking a screenshot on i3 or sway with fancy settings :)
readonly default_save_dir="$HOME/Downloads"
screenshot_to_stdout() {
if [ "$XDG_SESSION_TYPE" = "wayland" ]; then
if pgrep -x wayfreeze > /dev/null; then
exit
fi
wayfreeze &
local wayfreeze_pid=$!
# Hopefully give wayfreeze a moment so it definitely appears *below* grim's overlay.
# There's probably a better way to do this!
sleep 0.1
# https://github.com/emersion/slurp/issues/16#issuecomment-3896655428
local coords=$(swaymsg -t get_tree | jq -r '.. | (.nodes? // empty)[] | select(.pid and .visible) | .rect | "\(.x),\(.y) \(.width)x\(.height)"' | slurp)
grim -g "$coords" -t png -
local result=$?
kill $wayfreeze_pid > /dev/null
return $result
else
maim --select --highlight --color=0,0.5,0.6,0.5
fi
}
stdin_to_clipboard() {
if [ "$XDG_SESSION_TYPE" = "wayland" ]; then
wl-copy -t image/png
else
xclip -selection clipboard -t image/png
fi
}
case "$1" in
'file')
default_filename="Screenshot at $(date '+%Y-%m-%d %H:%M:%S').png"
temp_save_path="$(mktemp "$default_filename.XXXX")"
screenshot_to_stdout > "$temp_save_path"
if [ $? -ne 0 ]; then
exit 1
fi
save_path="$(zenity \
--file-selection \
--save \
--filename="$default_save_dir/$default_filename")"
if [ $? -ne 0 ]; then
# Fallback to saving to the clipboard if they cancelled chosing a
# file.
cat "$temp_save_path" | stdin_to_clipboard
rm "$temp_save_path"
else
mv "$temp_save_path" "$save_path"
fi
;;
'clipboard' | '')
screenshot_to_stdout | stdin_to_clipboard
;;
*)
echo 'Usage: screenshot [file|clipboard]'
;;
esac
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment