This repository has been archived by the owner on Apr 2, 2023. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
screech
executable file
·197 lines (178 loc) · 5.51 KB
/
screech
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
#!/usr/bin/env bash
# easy screenshot tool for both X and Wayland
# written by Reese Sapphire (github.com/katacarbix)
# kinda sorta based on swayshot - https://gitlab.com/radio_rogal/swayshot
# Dependencies:
# for both:
# - curl (upload to hosting site)
# - libnotify (desktop notifications) - there may be alternatives which also provide a notify-send command
# for Wayland:
# - sway (window manager/compositor)
# - grim (screenshot tool)
# - slurp (area selection)
# - jq (jquery parser)
# - wl-copy (clipboard tool)
# for X11:
# - maim (screenshot tool)
# - slop (area selector)(dependency of maim)
# - xclip (clipboard tool)
# - xdotool (X11 interface)
# Configuration
shot_dir="$HOME/Pictures/Screenshots" # screenshots folder
shot_name="screech_$(date +'%Y-%m-%d_%H-%M-%S').png" # file name to save as
# these parameters are for a small image hosting page on my home webserver. you
# will most likely need to change these and/or the upload_and_copy_url function
# if you want to use this feature.
uploader_base_url="https://i.example.com/" # base url of image host for -upload
uploader_script_path="upload.php" # image host upload page
uploader_key="YOUR_KEY_HERE" # image host api key
source "$HOME/.secrets/screech" # import the actual config values ;)
# notification handler
notify () {
if [ -t 0 ] || [ quiet ]; then
echo $@
else
notify-send -i desktop "$1" "$2"
fi
}
# save image data to file
# stdin: image data
save_to_file () {
cat - > "$shot_dir/$shot_name"
notify "Screenshot saved!" "$shot_dir/$shot_name"
}
# determine the right clipboard handler to use and copy data to clipboard
# stdin: image data
# args: type of data (png, txt)(default png)
copy_to_clipboard () {
if [ $WAYLAND_DISPLAY ] && type wl-copy >/dev/null 2>&1; then
cat - | wl-copy $([ "$1" = "txt" ] || echo "-t image/png")
elif [ -z $WAYLAND_DISPLAY ] && type xclip >/dev/null 2>&1; then
cat - | xclip -selection clipboard $([ "$1" = "txt" ] || echo "-t image/png")
else
cat - &>/dev/null
notify "Copy failed!" "No compatible clipboarder present on this system."
return 1
fi
}
# upload screenshot to self-hosted uploader script and copy the URL
# stdin: image data
upload_and_copy_url () {
result=$(cat - | curl -F "file=@-;filename=img.png" -F "key=$uploader_key" -F "action=up" "$uploader_base_url$uploader_script_path")
if [ $? -ne 0 ]; then
notify "Upload failed!" $result
return 1
fi
echo -n "$result" | copy_to_clipboard txt
notify "Uploaded successfully!" "Check your clipboard for the link."
return 0
}
# get the currently active window
# stdout: window id
get_focused_window () {
if [ $WAYLAND_DISPLAY ]; then
# jq filter for finding sway's active window
declare -r filter='
# returns the focused node by recursively traversing the node tree
def find_focused_node:
if .focused then . else (if .nodes then (.nodes | .[] | find_focused_node) else empty end) end;
# returns a string in the format that grim expects
def format_rect:
"\(.rect.x),\(.rect.y) \(.rect.width)x\(.rect.height)";
find_focused_node | format_rect'
swaymsg --type get_tree --raw | jq --raw-output "${filter}"
elif [ -z $WAYLAND_DISPLAY ] && type xdotool >/dev/null 2>&1; then
xdotool getactivewindow
fi
}
# stdout: image data
capture_screen () {
if [ $WAYLAND_DISPLAY ]; then
swaymsg --type get_outputs --raw | jq --raw-output '.[] | select(.focused) | .name' | grim -o -
else
options=$($cursor && echo -n "" || echo -n "-u")
maim $options | cat -
fi
}
capture_window () {
if [ $WAYLAND_DISPLAY ]; then
grim -g $(get_focused_window) -
else
options=$($cursor && echo -n "-i" || echo -n "-ui")
maim $options $(get_focused_window) | cat -
fi
}
capture_area () {
if [ $WAYLAND_DISPLAY ]; then
slurp -b '#00000000' -c '#808080ff' -s '#00000000' -w 1 -d | grim -g -
else
options=$($cursor && echo -n "-s" || echo -n "-us")
maim $options -c 0.5,0.5,0.5 | cat -
fi
}
show_help () {
echo "screech - Take screenshots in Wayland (sway) and X11."
echo "Usage: screech [option(s)]"
echo ""
echo "Options"
echo " Any of:"
echo " -save Save the screenshot to your computer."
echo " -copy Copy the screenshot to the clipboard."
echo " -upload Post the screenshot to a hosting site and copy the link"
echo " (takes precedence over -copy)."
echo " -cursor Include mouse cursor."
echo " -quiet Don't send desktop notifications."
echo " One of:"
echo " -screen (default) Take a screenshot of the entire screen."
echo " -window Take a screenshot of the current window."
echo " -area Use your mouse to select an area to take a screenshot of."
}
if [ $# -eq 0 ]; then
show_help
exit
fi
save=false
copy=false
upload=false
mode='screen'
cursor=false
quiet=false
while(($#)); do
case "$1" in
-save )
save=true ;;
-copy )
copy=true ;;
-upload )
upload=true ;;
-screen )
mode='screen' ;;
-window )
mode='window' ;;
-area )
mode='area' ;;
-cursor )
cursor=true ;;
-quiet )
quiet=true ;;
-h | -help | --help )
show_help
exit ;;
\; ) ;;
* )
echo "Unexpected argument: $1"
show_help
exit 1 ;;
esac
shift
done
# create temporary file to store image data
# (bash can't store raw data in variables easily)
temp_file=$(mktemp)
trap 'rm -f "$temp_file"' EXIT
# capture region
eval "capture_$mode" > $temp_file
# perform action(s)
$save && save_to_file < $temp_file
$copy && copy_to_clipboard < $temp_file
$upload && upload_and_copy_url < $temp_file