irongit
hygoland/scripts/ScreenShot.sh
166 lines4.4 KBBourne Again Shell (bash)
1#!/bin/bash
2# /* ---- 💫 https://github.com/JaKooLit 💫 ---- */ ##
3# Screenshots scripts
4
5# variables
6time=$(date "+%d-%b_%H-%M-%S")
7dir="$(xdg-user-dir)/Pictures/Screenshots"
8file="Screenshot_${time}_${RANDOM}.png"
9
10iDIR="$HOME/.config/swaync/icons"
11iDoR="$HOME/.config/swaync/images"
12sDIR="$HOME/.config/hypr/scripts"
13
14active_window_class=$(hyprctl -j activewindow | jq -r '(.class)')
15active_window_file="Screenshot_${time}_${active_window_class}.png"
16active_window_path="${dir}/${active_window_file}"
17
18notify_cmd_base="notify-send -t 10000 -A action1=Open -A action2=Delete -h string:x-canonical-private-synchronous:shot-notify"
19notify_cmd_shot="${notify_cmd_base} -i ${iDIR}/picture.png "
20notify_cmd_shot_win="${notify_cmd_base} -i ${iDIR}/picture.png "
21notify_cmd_NOT="notify-send -u low -i ${iDoR}/note.png "
22
23# notify and view screenshot
24notify_view() {
25 if [[ "$1" == "active" ]]; then
26 if [[ -e "${active_window_path}" ]]; then
27 "${sDIR}/Sounds.sh" --screenshot
28 resp=$(timeout 5 ${notify_cmd_shot_win} " Screenshot of:" " ${active_window_class} Saved.")
29 case "$resp" in
30 action1)
31 xdg-open "${active_window_path}" &
32 ;;
33 action2)
34 rm "${active_window_path}" &
35 ;;
36 esac
37 else
38 ${notify_cmd_NOT} " Screenshot of:" " ${active_window_class} NOT Saved."
39 "${sDIR}/Sounds.sh" --error
40 fi
41
42 elif [[ "$1" == "swappy" ]]; then
43 "${sDIR}/Sounds.sh" --screenshot
44 resp=$(${notify_cmd_shot} " Screenshot:" " Captured by Swappy")
45 case "$resp" in
46 action1)
47 swappy -f - <"$tmpfile"
48 ;;
49 action2)
50 rm "$tmpfile"
51 ;;
52 esac
53
54 else
55 local check_file="${dir}/${file}"
56 if [[ -e "$check_file" ]]; then
57 "${sDIR}/Sounds.sh" --screenshot
58 resp=$(timeout 5 ${notify_cmd_shot} " Screenshot" " Saved")
59 case "$resp" in
60 action1)
61 xdg-open "${check_file}" &
62 ;;
63 action2)
64 rm "${check_file}" &
65 ;;
66 esac
67 else
68 ${notify_cmd_NOT} " Screenshot" " NOT Saved"
69 "${sDIR}/Sounds.sh" --error
70 fi
71 fi
72}
73
74# countdown
75countdown() {
76 for sec in $(seq $1 -1 1); do
77 notify-send -h string:x-canonical-private-synchronous:shot-notify -t 1000 -i "$iDIR"/timer.png " Taking shot" " in: $sec secs"
78 sleep 1
79 done
80}
81
82# take shots
83shotnow() {
84 cd ${dir} && grim - | tee "$file" | wl-copy
85 sleep 2
86 notify_view
87}
88
89shot5() {
90 countdown '5'
91 sleep 1 && cd ${dir} && grim - | tee "$file" | wl-copy
92 sleep 1
93 notify_view
94}
95
96shot10() {
97 countdown '10'
98 sleep 1 && cd ${dir} && grim - | tee "$file" | wl-copy
99 notify_view
100}
101
102shotwin() {
103 w_pos=$(hyprctl activewindow | grep 'at:' | cut -d':' -f2 | tr -d ' ' | tail -n1)
104 w_size=$(hyprctl activewindow | grep 'size:' | cut -d':' -f2 | tr -d ' ' | tail -n1 | sed s/,/x/g)
105 cd ${dir} && grim -g "$w_pos $w_size" - | tee "$file" | wl-copy
106 notify_view
107}
108
109shotarea() {
110 tmpfile=$(mktemp)
111 grim -g "$(slurp)" - >"$tmpfile"
112
113 # Copy with saving
114 if [[ -s "$tmpfile" ]]; then
115 wl-copy <"$tmpfile"
116 mv "$tmpfile" "$dir/$file"
117 fi
118 notify_view
119}
120
121shotactive() {
122 active_window_class=$(hyprctl -j activewindow | jq -r '(.class)')
123 active_window_file="Screenshot_${time}_${active_window_class}.png"
124 active_window_path="${dir}/${active_window_file}"
125
126 hyprctl -j activewindow | jq -r '"\(.at[0]),\(.at[1]) \(.size[0])x\(.size[1])"' | grim -g - "${active_window_path}"
127 sleep 1
128 notify_view "active"
129}
130
131shotswappy() {
132 tmpfile=$(mktemp)
133 grim -g "$(slurp)" - >"$tmpfile"
134 # hyprshot -m region -z
135 # flameshot gui
136
137 # Copy without saving
138 if [[ -s "$tmpfile" ]]; then
139 wl-copy <"$tmpfile"
140 notify_view "swappy"
141 fi
142}
143
144if [[ ! -d "$dir" ]]; then
145 mkdir -p "$dir"
146fi
147
148if [[ "$1" == "--now" ]]; then
149 shotnow
150elif [[ "$1" == "--in5" ]]; then
151 shot5
152elif [[ "$1" == "--in10" ]]; then
153 shot10
154elif [[ "$1" == "--win" ]]; then
155 shotwin
156elif [[ "$1" == "--area" ]]; then
157 shotarea
158elif [[ "$1" == "--active" ]]; then
159 shotactive
160elif [[ "$1" == "--swappy" ]]; then
161 shotswappy
162else
163 echo -e "Available Options : --now --in5 --in10 --win --area --active --swappy"
164fi
165
166exit 0