Created
January 4, 2026 00:48
-
-
Save karlovskiy/ab7a2acbb46841fdfca87b1c1b9f1941 to your computer and use it in GitHub Desktop.
Screenshots for sway window manager
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/bin/bash | |
| if [[ -n "${DEBUG}" ]]; then | |
| set -x | |
| fi | |
| set -euo pipefail | |
| # Choose directory to save screenshots and viewer | |
| readonly SCREENSHOT_DIR="${HOME}/screenshot" | |
| readonly IMG_VIEWER="/bin/imv-wayland" | |
| check_dependencies() { | |
| local missing=() | |
| local deps=("${IMG_VIEWER}" grim bemenu jq slurp swaymsg notify-send awk) | |
| for tool in "${deps[@]}"; do | |
| command -v "$tool" >/dev/null 2>&1 || missing+=("$tool") | |
| done | |
| if [ ${#missing[@]} -ne 0 ]; then | |
| printf "Missing dependencies:\n - %s\n" "${missing[@]}" >&2 | |
| exit 1 | |
| fi | |
| } | |
| get_geometry() { | |
| local filter=$1 | |
| swaymsg -t get_tree | jq -r ".. | (.nodes? // empty)[] | select(${filter} and .pid) | \"\(.app_id) \(.rect.x),\(.rect.y) \(.rect.width)x\(.rect.height)\"" | |
| } | |
| create_screenshot() { | |
| local filename=$1 | |
| local geometry=${2:-""} | |
| if [[ -n "$geometry" ]]; then | |
| grim -g "$geometry" "$filename" | |
| else | |
| grim "$filename" | |
| fi | |
| } | |
| save_screenshot() { | |
| local windows=() | |
| mapfile -t windows < <(get_geometry ".visible") | |
| local options=("Fullscreen" "Selection" "Focus" "${windows[@]}" "Abort") | |
| local lines=${#options[@]} | |
| local type | |
| type=$(printf "%s\n" "${options[@]}" | bemenu -l "${lines}" -p "Select screenshot type:") | |
| [[ -z "$type" || "$type" == "Abort" ]] && exit 0 | |
| local suffix | |
| suffix=$(echo "" | bemenu -p "Enter screenshot suffix:") | |
| [[ -z "$suffix" ]] && exit 0 | |
| local filename | |
| filename="${SCREENSHOT_DIR}/$(date +%Y%m%d)_${suffix}.png" | |
| mkdir -p "${SCREENSHOT_DIR}" | |
| case "$type" in | |
| Fullscreen) create_screenshot "$filename" ;; | |
| Selection) create_screenshot "$filename" "$(slurp)" ;; | |
| Focus) create_screenshot "$filename" "$(get_geometry ".focused" | awk '{print $2, $3}')" ;; | |
| *) create_screenshot "$filename" "$(echo "$type" | awk '{print $2, $3}')" ;; | |
| esac | |
| notify-send -t 5000 "Screenshot" "Saved to $filename.png" | |
| echo "$filename" | |
| } | |
| main() { | |
| check_dependencies | |
| local filename | |
| filename=$(save_screenshot) | |
| if [[ -f "$filename" ]]; then | |
| "${IMG_VIEWER}" "$filename" & | |
| fi | |
| } | |
| main "$@" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment