diff --git a/screenshot.sh b/screenshot.sh index 176f728..334140e 100755 --- a/screenshot.sh +++ b/screenshot.sh @@ -1,62 +1,75 @@ #!/bin/bash # # GUI Screenshot Tool for Wayland Using Zenity, Grim, Slurp, and Rofi -# Last Modification: Wed Mar 5 2025 -# +# Last Modification: Wed Mar 16 2025 TMP_DIR=/tmp DEFAULT_FILENAME=screenshot.png - +HYPRLAND_REGEX='"\(.at[0]),\(.at[1]) \(.size[0])x\(.size[1])"' CHOICES=( - "1. a part of the screen" - "2. a part of the screen and put the output into the clipboard" - "3. whole screen" - "4. edit" + "1. Select a region and save - slurp | grim -g - $TMP_DIR/$DEFAULT_FILENAME" + "2. Select a region and copy to clipboard - slurp | grim -g - - | wl-copy && exit $?" + "3. Whole screen - grim $TMP_DIR/$DEFAULT_FILENAME" + "4. Current window - hyprctl -j activewindow | jq -r '$HYPRLAND_REGEX' | grim -g - $TMP_DIR/$DEFAULT_FILENAME" + "5. Edit - slurp | grim -g - - | swappy -f -" + "6. Quit - exit 0" ) +IS_NOTIFY=1 + +notify() { + TITLE="$1" + BODY="$2" + IS_NOTIFICATION="$3" + if [[ -z "$BODY" && -z "$TITLE" ]]; then + echo "notify: No message provided" + return 1 + fi + if [[ -z "$IS_NOTIFICATION" ]]; then + IS_NOTIFICATION=0 + fi + + if ((IS_NOTIFICATION)); then + notify-send "screenshot.sh" "$1" + else + printf "%s\n%s\n" "$TITLE" "$BODY" + fi + return 0 +} + CHOICE="$(rofi -dmenu -i -p "Enter option or select from the list" \ -mesg "Select a Screenshot Option" \ -a 0 -no-custom -location 0 \ -yoffset 30 -xoffset 30 \ - -theme-str 'listview {columns: 1; lines: 4;} window {width: 45%;}' \ + -theme-str 'listview {columns: 2; lines: 3;} window {width: 45%;}' \ -window-title "screenshot.sh" \ - <<< "$(printf "%s\n" "${CHOICES[@]}")")" + -format 'i' \ + <<< "$(printf "%s\n" "${CHOICES[@]%% - *}")")" -# echo $CHOICE +if [[ -z "$CHOICE" ]]; then + notify "screenshot.sh" "No option selected." "$IS_NOTIFY" + exit +fi -case $CHOICE in - "1. a part of the screen") - slurp | grim -g - "$TMP_DIR/$DEFAULT_FILENAME" - ;; - "2. a part of the screen and put the output into the clipboard") - slurp | grim -g - - | wl-copy - exit - ;; - "3. whole screen") - grim "$TMP_DIR/$DEFAULT_FILENAME" - ;; - "4. edit") - grim -g "$(slurp)" - | swappy -f - - exit - ;; - "") - exit - ;; -esac +CMD="${CHOICES[$CHOICE]#* -}" +if [[ -z "$CMD" ]]; then + notify "screenshot.sh" "No option selected." "$IS_NOTIFY" + exit +elif eval "$CMD"; then + notify "screenshot.sh" "Screenshot saved to $TMP_DIR/$DEFAULT_FILENAME" "$IS_NOTIFY" +else + notify "screenshot.sh" "An error occurred while taking the screenshot." "$IS_NOTIFY" + exit +fi FILE=$(zenity --file-selection --title="Select a File" --filename=$DEFAULT_FILENAME --save 2> /dev/null) - -# echo $FILE - -case $? in +case "$?" in 0) - mv "$TMP_DIR/$DEFAULT_FILENAME" $FILE + mv "$TMP_DIR/$DEFAULT_FILENAME" "$FILE" ;; 1) rm "$TMP_DIR/$DEFAULT_FILENAME" ;; -1) - echo "An unexpected error has occurred." + notify "screenshot.sh" "An unexpected error has occurred." "$IS_NOTIFY" ;; esac - -exit