Skip to content

Instantly share code, notes, and snippets.

@1ace
Created November 1, 2021 21:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save 1ace/ddd6767e708e500c12b4c180d41554b1 to your computer and use it in GitHub Desktop.
Save 1ace/ddd6767e708e500c12b4c180d41554b1 to your computer and use it in GitHub Desktop.
#!/usr/bin/env bash
set -eu -o pipefail
if [ -z "${WAYLAND_DISPLAY:-}" ]
then
echo >&2 "This script only supports Wayland compositors"
exit 1
fi
for tool in swaymsg jq sed slurp grim notify-send wl-copy
do
command -v $tool >/dev/null && continue
echo "This script requires $tool."
exit 1
done
file=~/screenshot_"$(date +%Y-%m-%d_%H-%M-%S_%N)".png
show_help() {
this=$(basename "$0")
echo >&2 "Usage: $this full"
echo >&2 " or $this monitor"
echo >&2 " or $this window"
echo >&2 " or $this selection"
exit 1
}
if [ $# -ne 1 ]
then
show_help
fi
action=$1
cleanup_string() {
sed 's/[^a-zA-Z0-9]/-/g; s/--\+/-/g; s/^-//; s/-$//'
}
case $action in
full)
grim "$file"
;;
monitor)
output_name=$(swaymsg -t get_outputs | jq -r '.[] | select(.focused) | .name')
grim -o "$output_name" "$file"
;;
window)
window="$(swaymsg -t get_tree | jq '.. | select(type == "object") | select(.focused)')"
window_appid="$(jq -r .app_id <<< "$window" | cleanup_string)"
window_name="$(jq -r .name <<< "$window" | cleanup_string)"
file=~/screenshot_"$window_appid"_"$window_name"_"$(date +%Y-%m-%d_%H-%M-%S_%N)".png
x="$(jq .rect.x <<< "$window")"
y="$(jq .rect.y <<< "$window")"
width="$(jq .rect.width <<< "$window")"
height="$(jq .rect.height <<< "$window")"
grim -g "${x},${y} ${width}x${height}" "$file"
;;
selection)
slurp | grim -g - "$file"
;;
help|*)
show_help
;;
esac
notify-send \
-t 1000 \
-i "$file" \
"Screenshot captured" \
"$(realpath "$file")"
wl-copy < "$file"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment