-
Notifications
You must be signed in to change notification settings - Fork 13
/
cap
executable file
·118 lines (104 loc) · 2.6 KB
/
cap
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
#!/usr/bin/env bash
# cap - screenshot a window or entire screen
#
# Uses GNOME Shell's screenshot functionality if available, so that window
# shadows get captured correctly (with PNG transparency).
. lib.bash || exit
set -u
has_gnome_shell() {
have busctl && busctl --user --acquired | grep -qw "^org\.gnome\.Shell"
}
usage() {
echo "Usage: $progname [-auw] [-l SEC]"
echo ""
echo_opt "-w" "capture active window"
echo_opt "-a" "capture selected area"
echo_opt "-l SEC" "delay before capture"
echo_opt "-u" "upload image after capture"
}
basedir=$(systemd-path user-pictures || echo ~/Pictures)
file=$basedir/Screenshots/$(date +%Y-%m-%d.%H%M%S).${HOSTNAME%%.*}.png
opt_window=0 # capture the active window only
opt_area=0
opt_upload=0
opt_delay=0 # delay between area select and capture
while getopts :al:uw OPT; do
case $OPT in
a) opt_area=1;;
l) opt_delay=$OPTARG;;
u) opt_upload=1;;
w) opt_window=1;;
*) lib:die_getopts;;
esac
done; shift $((OPTIND-1))
if (( $# )); then
vdie "excess arguments"
fi
if [[ ! $opt_delay =~ ^[0-9]+$ ]]; then
vdie "invalid -l value"
fi
if (( opt_area && opt_window )); then
vdie "-a and -w cannot be used together"
fi
if have gnome-screenshot && has_gnome_shell; then
args=()
if (( opt_window )); then
args+=(--window)
elif (( opt_area )); then
args+=(--area)
fi
if (( opt_delay )); then
args+=(--delay=$opt_delay)
fi
gnome-screenshot "${args[@]}" --file="$file"
elif have scrot; then
args=()
if (( opt_window )); then
args+=(-u -b)
elif (( opt_area )); then
args+=(-s -b)
fi
# TODO: implement opt_delay
scrot "${args[@]}" "$file"
elif have maim && have slop; then
args=()
if (( opt_window )); then
# XXX: 'xdotool getactivewindow' only reports the "real" window
# and has no way to report the parent WM frame, so we must
# select the window interactively.
args+=(-s)
elif (( opt_area )); then
if (( opt_delay )); then
geom=$(slop -f %g)
sleep $opt_delay
args+=(-g "$geom")
else
args+=(-s)
fi
fi
maim "${args[@]}" "$file"
else
vdie "no screenshot program"
fi
if [[ ! -s $file ]]; then
notifysend -i error "Screenshot failed" &
vdie "screenshot failed ('$file' not created)"
fi
shortfile=${file/#"$HOME/"/"~/"}
if (( opt_upload )); then
url=$(cap-upload.sh "$file")
if (( $? )); then
notifysend -i error "Screenshot captured but upload failed" &
echo "$file" | gclip
echo "$file"
vdie "screenshot upload failed"
else
notifysend -i "$file" "Screenshot captured and uploaded" "$url" &
echo "$url" | gclip
echo "$url"
fi
else
notifysend -i "$file" "Screenshot captured" "$shortfile" &
echo "$file" | gclip
echo "$file"
fi