clean up code
- add dependency check
This commit is contained in:
parent
3b6e07f713
commit
55dabdaf64
@ -1,37 +1,47 @@
|
||||
#!/bin/bash
|
||||
#!/usr/bin/env bash
|
||||
#
|
||||
# GUI Screenshot Tool for Wayland Using Zenity, Grim, Slurp, and Rofi
|
||||
# Last Modification: Wed Mar 16 2025
|
||||
|
||||
SCRIPT_NAME=$(basename "$0")
|
||||
TMP_DIR=/tmp
|
||||
DEFAULT_FILENAME=screenshot.png
|
||||
HYPRLAND_REGEX='"\(.at[0]),\(.at[1]) \(.size[0])x\(.size[1])"'
|
||||
TMP_SCREENSHOT="$TMP_DIR/$DEFAULT_FILENAME"
|
||||
HYPRLAND_REGEX='.at[0],(.at[1]) .size[0]x(.size[1])'
|
||||
REQUIREMENTS=(grim slurp rofi zenity wl-copy)
|
||||
|
||||
for cmd in "${REQUIREMENTS[@]}"; do
|
||||
if ! command -v "$cmd" &> /dev/null; then
|
||||
echo "Error: $cmd is not installed. Please install it first."
|
||||
exit 1
|
||||
fi
|
||||
done
|
||||
|
||||
CHOICES=(
|
||||
"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"
|
||||
"1. Select a region and save - slurp | grim -g - \"$TMP_SCREENSHOT\""
|
||||
"2. Select a region and copy to clipboard - slurp | grim -g - - | wl-copy"
|
||||
"3. Whole screen - grim \"$TMP_SCREENSHOT\""
|
||||
"4. Current window - hyprctl -j activewindow | jq -r \"${HYPRLAND_REGEX}\" | grim -g - \"$TMP_SCREENSHOT\""
|
||||
"5. Edit - slurp | grim -g - - | swappy -f -"
|
||||
"6. Quit - exit 0"
|
||||
)
|
||||
IS_NOTIFY=1
|
||||
USE_NOTIFICATIONS=0
|
||||
|
||||
notify() {
|
||||
TITLE="$1"
|
||||
BODY="$2"
|
||||
IS_NOTIFICATION="$3"
|
||||
if [[ -z "$BODY" && -z "$TITLE" ]]; then
|
||||
local body="$1"
|
||||
local title="$2"
|
||||
if [[ -z "$body" ]]; then
|
||||
echo "notify: No message provided"
|
||||
return 1
|
||||
fi
|
||||
if [[ -z "$IS_NOTIFICATION" ]]; then
|
||||
IS_NOTIFICATION=0
|
||||
if [[ -z "$title" ]]; then
|
||||
title="$SCRIPT_NAME"
|
||||
fi
|
||||
local use_notification="${3:-$USE_NOTIFICATIONS}"
|
||||
|
||||
if ((IS_NOTIFICATION)); then
|
||||
notify-send "screenshot.sh" "$1"
|
||||
if ((use_notification)); then
|
||||
notify-send "$title" "$body"
|
||||
else
|
||||
printf "%s\n%s\n" "$TITLE" "$BODY"
|
||||
printf "%s\n%s\n" "$title" "$body"
|
||||
fi
|
||||
return 0
|
||||
}
|
||||
@ -41,35 +51,53 @@ CHOICE="$(rofi -dmenu -i -p "Enter option or select from the list" \
|
||||
-a 0 -no-custom -location 0 \
|
||||
-yoffset 30 -xoffset 30 \
|
||||
-theme-str 'listview {columns: 2; lines: 3;} window {width: 45%;}' \
|
||||
-window-title "screenshot.sh" \
|
||||
-window-title "$SCRIPT_NAME" \
|
||||
-format 'i' \
|
||||
<<< "$(printf "%s\n" "${CHOICES[@]%% - *}")")"
|
||||
|
||||
if [[ -z "$CHOICE" ]]; then
|
||||
notify "screenshot.sh" "No option selected." "$IS_NOTIFY"
|
||||
exit
|
||||
notify "No option selected." ""
|
||||
exit 0
|
||||
fi
|
||||
|
||||
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
|
||||
notify "No option selected." ""
|
||||
exit 0
|
||||
fi
|
||||
|
||||
FILE=$(zenity --file-selection --title="Select a File" --filename=$DEFAULT_FILENAME --save 2> /dev/null)
|
||||
# For option 2 (copy to clipboard), handle differently
|
||||
if [[ "$CHOICE" == "1" ]]; then
|
||||
if eval "$CMD"; then
|
||||
notify "Screenshot copied to clipboard"
|
||||
exit 0
|
||||
else
|
||||
notify "An error occurred while taking the screenshot."
|
||||
exit 1
|
||||
fi
|
||||
fi
|
||||
|
||||
if ! eval "$CMD"; then
|
||||
notify "An error occurred while taking the screenshot."
|
||||
exit 1
|
||||
fi
|
||||
|
||||
notify "screenshot.sh" "Screenshot saved temporarily.\nChoose where to save it permanently"
|
||||
|
||||
FILE=$(zenity --file-selection --title="Save Screenshot" --filename="$DEFAULT_FILENAME" --save 2> /dev/null)
|
||||
case "$?" in
|
||||
0)
|
||||
mv "$TMP_DIR/$DEFAULT_FILENAME" "$FILE"
|
||||
if mv "$TMP_SCREENSHOT" "$FILE"; then
|
||||
notify "Screenshot saved to $FILE"
|
||||
else
|
||||
notify "Failed to save screenshot to $FILE"
|
||||
fi
|
||||
;;
|
||||
1)
|
||||
rm "$TMP_DIR/$DEFAULT_FILENAME"
|
||||
rm -f "$TMP_SCREENSHOT"
|
||||
notify "Screenshot discarded"
|
||||
;;
|
||||
-1)
|
||||
notify "screenshot.sh" "An unexpected error has occurred." "$IS_NOTIFY"
|
||||
notify "An unexpected error has occurred."
|
||||
;;
|
||||
esac
|
||||
|
Loading…
x
Reference in New Issue
Block a user