-
Notifications
You must be signed in to change notification settings - Fork 1
/
lightshot_print_screen
executable file
·325 lines (276 loc) · 10.8 KB
/
lightshot_print_screen
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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
#!/bin/sh
#--------------------------------------------------------------------------------
#-- Lightshot print screen Linux handler --
#-- Git: https://burian.work/lightshot --
#-- Code: POSIX shell script --
#-- License: MIT --
#-- Version: 8.0 --
#-- Released on: 2024-Jun-20 --
#-- Copyright 2018-2024 Vlastimil Burián --
#-- E-mail: vlastimilburian@protonmail.com --
#--------------------------------------------------------------------------------
# Global shell script variable(s):
# The hotkey variable is on top for easy set-up; overridable with -k switch.
# Run this script with -h for complete help to this script with an example.
# Just run this script to test out, it will print out if Lightshot was found.
# Set this to the same hotkey which you have set up in your Lightshot options.
# For the left control key and the print screen key put in Control_L+Print.
hotkey=Print
# Global shell script option(s):
# Treat unset variables as errors when substituting.
# Clarification: this means that accessing any unassigned variable
# produces immediate termination of the script with an error printed.
set -o nounset
tput_init_linux() { set_fg_color='tput setaf'; reset_color=$( tput sgr0 2>/dev/null ); }
tput_init_bsd() { set_fg_color='tput AF'; reset_color=$( tput me 2>/dev/null ); }
tput_init_none() { set_fg_color=:; reset_color=; }
# This routine prepares `tput` on Linux or BSD and no-color console.
if tput setaf >/dev/null 2>&1; then
# Linux uses terminfo
tput_init_linux
# These if-conditions are the most straight ways of determining `tput` capability.
elif tput AF >/dev/null 2>&1; then
# BSD uses termcap
tput_init_bsd
else
# Null command (`:`) ignores everything after it
tput_init_none
fi
# In this function we use the $set_fg_color command to set the color sequences.
colorize()
{
case "$1" in
red) $set_fg_color 1 ;;
green) $set_fg_color 2 ;;
yellow) $set_fg_color 3 ;;
blue) $set_fg_color 4 ;;
magenta) $set_fg_color 5 ;;
cyan) $set_fg_color 6 ;;
white) $set_fg_color 7 ;;
none) printf '%s' "$reset_color" ;;
esac
}
# Finally, here we define common color variables named using our colorize() function.
color_red=$( colorize red )
color_green=$( colorize green )
color_yellow=$( colorize yellow )
color_blue=$( colorize blue )
color_magenta=$( colorize magenta )
# shellcheck disable=SC2034
color_cyan=$( colorize cyan )
color_white=$( colorize white )
color_none=$( colorize none )
# (bool) Function to test a variable for an integer.
is_int()
{
case "${1#[+-]}" in
*[!0123456789]*)
return 1
;;
'')
return 1
;;
*)
return 0
;;
esac
}
# (bool) Function to test a variable for an unsigned integer.
is_uint()
{
case "$1" in
[+-]*)
return 1
;;
esac
is_int "$1"
}
# (bool) Function to test a variable for a _safe_ exit code:
# From unsigned integer in a range <0;125> which is safe range,
# the remaining range is reserved or otherwise not recommended.
is_safe_exit_uint()
{
is_uint "$1" && [ "$1" -le 125 ]
}
# (string) Function to detect basic variable types passed to my functions:
# empty, integer, file (with specific info), the rest is considered string.
dump_args()
{
# we do not write anything for 0 arguments
[ "$#" -eq 0 ] && return 1
exec >&2
i=1
printf '%s\n' \
"${color_yellow}dump_args()$color_none" \
"$color_white$# arguments inspected:$color_none"
while [ "$#" -gt 0 ]; do
printf "[$color_white%d$color_none]: '%s' " "$i" "$1"
if [ -z "$1" ]; then
printf '%s' "(${color_blue}empty$color_none)"
elif is_int "$1"; then
printf '%s' "(${color_green}integer$color_none)"
elif [ -e "$1" ]; then
printf '%s' "(${color_magenta}file: $( file -b "$1" )$color_none)"
else
printf '%s' "(${color_red}string$color_none)"
fi
printf '\n'
shift
i=$(( i + 1 ))
done
}
# (string) Function to print an error message.
print_error()
{
printf >&2 '%s\n' \
"Error heading: $1" \
"Error message: $2"
}
# (string) Function to print an error message and exit right away.
print_error_and_exit()
{
if ! { [ "$#" -eq 2 ] && [ -n "$1" ] && [ -n "$2" ]; } &&
! { [ "$#" -eq 3 ] && [ -n "$1" ] && [ -n "$2" ] && [ "$3" -ge 1 ] && is_safe_exit_uint "$3"; } then
dump_args "$@"
# shellcheck disable=SC2016
printf >&2 '%s\n' \
'print_error_and_exit() input check' \
'Wrong number, type or empty arguments have been passed to the function.' \
'Expected the following strings: $1 = error heading, $2 = error message.' \
'Optionally, accepting a number: $3 = exit code; if not given 1 is used.' \
'Acceptable exit code range: anywhere between <1;125>; 1 is the default.'
exit 1
fi
print_error "$1" "$2"
exit "${3:-1}"
}
# shellcheck disable=SC2120
is_user_root()
{
[ "$( id -u )" -eq 0 ]
}
# shellcheck disable=SC2119
if is_user_root; then
print_error_and_exit 'is_user_root()' 'Running this script with root privileges is not recommended! Expected an ordinary user.'
fi
check_requirements()
{
if [ "$#" -eq 0 ]; then
print_error_and_exit 'check_requirements() input check' 'No arguments passed to the function! At least one program/package name pair expected.'
fi
if [ "$(( $# % 2 ))" -ne 0 ]; then
dump_args "$@"
print_error_and_exit 'check_requirements() input check' 'Odd number of arguments has been passed to the function. Program and package names in pairs expected.'
fi
dependency_missing=false
while [ "$#" -gt 0 ]; do
if ! command -v "$1" >/dev/null 2>&1; then
dependency_missing=true
print_error 'check_requirements(): unmet required dependency' "This script requires '$1' program but it is not available on this system! On Ubuntu/Debian OS, just install the '$2' package; other systems may differ."
fi
shift 2
done
if "$dependency_missing"; then
exit 1
fi
}
check_requirements \
pgrep procps \
xdotool xdotool
print_usage_and_exit()
{
if ! { [ "$#" -eq 1 ] && is_safe_exit_uint "$1"; } then
dump_args "$@"
# shellcheck disable=SC2016
print_error_and_exit 'print_usage_and_exit() input check' 'Number required: $1 = exit code from the script.'
fi
[ "$1" -ne 0 ] && exec >&2
cat <<EOF
Project: Lightshot print screen Linux handler
Version: 8.0 (2024-Jun-20)
GitHub : https://burian.work/lightshot
--------------------------------------------------------------------
Description: This script works with XDOTOOL to trigger Print Screen
key combination in Lightshot application installed on Linux in Wine.
The script, however, won't launch Lightshot for you due to variable
installation locations which make it impossible. You need to run it!
License is MIT. The code can be further enhanced by others provided
that any new copies have my copyright notice included in every case.
--------------------------------------------------------------------
Usage in terminal: ./$( basename "$0" ) [-k HotKey]
-k HotKey: Optional switch requiring one argument,
which is the print screen hotkey combination.
For the left Control key and the Print Screen key
that would be -k Control_L+Print (as an example).
-h: Show this help.
Usage in desktop environment:
You need to find Keyboard shortcuts, and re-map Print to run
this script, an example from Linux Mint is placed on GitHub.
Author: Copyright 2018-2024 Vlastimil Burián, vlastimilburian@protonmail.com
EOF
exit "$1"
}
while getopts ':hk:' option; do
case "$option" in
k)
hotkey=$OPTARG
shift 2
;;
h)
print_usage_and_exit 0
;;
*)
dump_args "$@"
print_usage_and_exit 1
;;
esac
done
if [ "$#" -ne 0 ]; then
dump_args "$@"
print_usage_and_exit 1
fi
get_process_id()
{
if ! { [ "$#" -eq 1 ] && [ -n "$1" ]; } then
dump_args "$@"
# shellcheck disable=SC2016
print_error_and_exit 'get_process_id() input check' 'String required: $1 = a process name to find id to.'
fi
# -o, --oldest: select least recently started
pgrep -o "$1"
}
get_window_id()
{
if ! { [ "$#" -eq 2 ] && is_uint "$1" && [ -n "$2" ]; } then
dump_args "$@"
print_error_and_exit 'get_window_id() input check' 'Two valid arguments were not passed to the function! Expected a process id (unsigned integer) and a window name (string).'
fi
#--all : Require all conditions to be met.
#--sync : Wait until a search result is found.
#--limit: Stop searching after finding N matching windows.
#--pid : Match windows that belong to a specific process id.
#--name : Match against the window name. This is the same string that is displayed in the window titlebar.
if ! xdotool search --all --sync --limit 1 --pid "$1" --name "$2"; then
print_error_and_exit 'get_window_id()' "No window matching process id '$1' and window name '$2' has been found."
fi
}
# The Lightshot file name is case-sensitive.
lightshot_process_id=$( get_process_id Lightshot.exe )
if ! is_uint "$lightshot_process_id"; then
# shellcheck disable=SC2016
print_error_and_exit 'is_uint $lightshot_process_id' 'Lightshot process is not running. Launch Lightshot app first, please.'
fi
# The Lightshot window-id is case-insensitive.
lightshot_window_id=$( get_window_id "$lightshot_process_id" Lightshot )
if ! is_uint "$lightshot_window_id"; then
# shellcheck disable=SC2016
print_error_and_exit 'is_uint $lightshot_window_id' 'Lightshot process was found (is running) but its window was not found for some reason / get_window_id() returned an empty value.'
fi
if xdotool key --window "$lightshot_window_id" "$hotkey"; then
printf '%s\n' \
"Success, the hotkey = $hotkey was successfully sent to Lightshot window!" \
'If the Lightshot overlay did not appear, you likely sent a wrong hotkey.' \
'In such a case, please adjust the hotkey in Lightshot tray options menu.'
else
print_error_and_exit "xdotool key --window $lightshot_window_id $hotkey" 'The above send-hotkey command encountered an error.'
fi