-
Notifications
You must be signed in to change notification settings - Fork 0
/
sshot
executable file
·260 lines (224 loc) · 6.78 KB
/
sshot
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
#!/bin/bash
# author [email protected]
# Wrapper on top of maim that provides a few more functionalities. It can take
# a screenshot of a specified display.
# dependencies
# xrandr
# maim
# curl
# xclip
# Possible aliases
# sshot
# To implement
# possibility to add delay with -d
# store list of screenshots
# possible colors to assign to maim
LIGHTRED=".96,0.55,0.41,1.0"
MAGENTA="0.7,0.3,0.7,1.0"
# Settings
DEST=~/Pictures/screenshots/
DELAY=false
SELECTION_SETTINGS="--bordersize=3 --color=$MAGENTA"
SCREENSHOT_TYPE=""
if [[ -z "$IMGNAME" ]];then
IMGNAME="sshot-`date +'%Y-%m-%d_%H:%M:%S'`.png"
fi
IMGLOC=$DEST$IMGNAME
if [[ -z "$SSHOT_UPLOAD" ]]; then
SSHOT_UPLOAD=true
fi
if [[ -z "$ROFI_PLUGIN" ]]; then
ROFI_PLUGIN=false
fi
# Print help
function sm_help () {
echo -e "sshot.sh - take screenshot and upload to ptpb.pw\n"
echo -e "SYNOPSIS"
echo -e " sshot.sh [OPTION] [COMMAND]\n"
echo -e "OPTION"
echo -e " -d [delay] -add delay"
echo -e " e.g., sshot -d 10 DP-0\n"
echo -e "COMMAND"
echo -e " all -take screenshot of entire screen\n"
echo -e " <display> -take screenshot of specific display using its proper name (check xrandr). e.g. DP-0"
echo -e " e.g., sshot.sh DP-0\n"
echo -e " <display #> -take screenshot of specific display using its number. e.g. 0, 1"
echo -e " e.g., sshot.sh 0\n"
echo -e " sel|selection -take screenshot of area or window selected by user (interactive)\n"
}
# Print some helpful information in case of ERROR
function sm_cry () {
echo "DELAY: $DELAY"
echo "DELAY_TIME: $DELAY_TIME"
echo "SCREENSHOT_TYPE: $SCREENSHOT_TYPE"
echo "TARGET_DISP: $TARGET_DISP"
echo "TARGET_GEOM: $TARGET_GEOM"
echo "IMGLOC: $IMGLOC"
}
# Check that the screenshot file was actually created
function check_img () {
if [[ -f $@ ]]; then
return
else
echo "Something went wrong and screenshot was not taken. Exiting..."
sm_cry
exit 1
fi
}
# Upload image and notify via notify-send
function upload() {
if [[ ! -z $1 ]]; then
#p=1 == long url
#sunset=432000 kill screenshot after 5 days
$(curl -F c=@${1} -F p=1 sunset=432000 https://ptpb.pw &>/tmp/curl.progress)
wait
url=$(tail -n-2 /tmp/curl.progress | head -n1 | cut -c6-)
regex='(https?|http)://[-A-Za-z0-9\+&@#/%?=~_|!:,.;]*[-A-Za-z0-9\+&@#/%=~_|]'
if [[ ! $url =~ $regex ]]; then
echo "Upload failed..."
sm_cry
echo -e "Curl output:\n------------\n"
cat /tmp/curl.progress
notify-send "Upload failed... /tmp/curl.progress"
exit 1
else
notify-send "Screenshot taken" "${url}" && \
echo -n "$url" | xclip -i -selection primary && \
echo -n "$url" | xclip -i -selection clipboard
fi
fi
}
function _maim() {
if [[ ! $DELAY = true ]]; then
maim "$@"
else
maim -d "$DELAY_TIME" "$@"
# if hash notify-send.sh 2>/dev/null; then
# notid=$(notify-send.sh --print-id sshot "Screenshot in ${DELAY_TIME}s...")
# fi
fi
if [[ $? -eq 1 ]]; then
echo "Error in maim. In most cases, it just means the selection was cancelled (which is normal behavior)."
exit 1
fi
}
function parseargs () {
re='^-?[0-9]+$'
if [[ $# -eq 0 ]]; then
SCREENSHOT_TYPE="all"
elif [[ $# -eq 1 ]]; then
if [[ "$1" == "-h" ]]; then
sm_help
exit 0
elif [[ "$1" == "all" ]]; then
SCREENSHOT_TYPE="all"
elif [[ "$1" == "sel" ]] || [[ $1 == "selection" ]]; then
SCREENSHOT_TYPE="sel"
elif [[ "$1" == "list" ]]; then
echo "all"
echo "selection"
IFS=$'\n'
disps=($(xrandr | grep " connected" | awk {'printf ("%s %s %s\n", $1,$3,$4)'}))
for i in ${!disps[@]}
do
echo $PREPEND$(printf "%s" ${disps[$i]} | awk {'printf ("%s\n", $1)'})
done
exit 0
elif [[ $1 =~ $re ]]; then
SCREENSHOT_TYPE="dispnum"
TARGET_DISP_NUM=$1
else
SCREENSHOT_TYPE="display"
TARGET_DISP=$1
fi
# only case for 2 arguments will be sshot.sh -d 10
elif [[ $# -eq 2 ]]; then
if [[ $1 == "-d" ]]; then
DELAY=true
DELAY_TIME=$2
SCREENSHOT_TYPE="all"
else
echo -e "ERROR: Too many arguments."
echo -e "If -d option is used, it must be followed by a number."
echo -e "e.g., sshot -d 10 (will take a screenshot of entire screen after 10s"
echo -e "NOTE: sshot.sh -d 10 will be the only case where two arguments should be used"
echo ""
sm_help
exit 1
fi
# every other case with -d [delay_time] will have three arguments
elif [[ $# -eq 3 ]]; then
if [[ $1 == "-d" ]]; then
DELAY=true
DELAY_TIME=$2
if [[ "$3" == "-h" ]]; then
sm_help
exit 0
elif [[ "$3" == "all" ]]; then
SCREENSHOT_TYPE="all"
elif [[ "$3" == "sel" ]] || [[ $3 == "selection" ]]; then
SCREENSHOT_TYPE="sel"
elif [[ $3 =~ $re ]]; then
SCREENSHOT_TYPE="dispnum"
TARGET_DISP_NUM=$3
else
SCREENSHOT_TYPE="display"
TARGET_DISP=$3
fi
else
echo -e "ERROR: Too many arguments."
echo -e "If -d option is used, it must be followed by a number."
echo -e "e.g., sshot -d 10 (will take a screenshot of entire screen after 10s"
echo ""
sm_help
exit 1
fi
else
echo -e "ERROR: sshot.sh only takes zero, one or two arguments.\n"
sm_help
exit 1
fi
}
function mainf() {
parseargs "$@"
if [[ $SCREENSHOT_TYPE == "all" ]]; then
_maim "$IMGLOC"
wait
check_img "$IMGLOC"
elif [[ $SCREENSHOT_TYPE == "sel" ]]; then
_maim ''$SELECTION_SETTINGS'' -s "$IMGLOC"
wait
check_img "$IMGLOC"
elif [[ $SCREENSHOT_TYPE == "display" ]] || [[ $SCREENSHOT_TYPE == "dispnum" ]]; then
IFS=$'\n'
disp_ct=0
for disps in $(xrandr | grep " connected")
do
disp_ct=$((disp_ct+1))
#echo display: $disps
IFS=$' '
disp=($(echo -n "$disps" | awk {'printf ("%s %s %s %s", $1, $2, $3, $4)'}))
if [[ $disp_ct == "$TARGET_DISP_NUM" ]]; then
TARGET_DISP=${disp[0]}
fi
if [[ "$TARGET_DISP" == ${disp[0]} ]]; then
TARGET_GEOM=${disp[2]}
fi
IFS=$'\n'
done
if [[ -z "$TARGET_GEOM" ]]; then
echo -e "ERROR: Something went wrong and display geometry was not found\n"
sm_cry
exit 1
else
_maim -g "$TARGET_GEOM" "$IMGLOC"
wait
check_img "$IMGLOC"
fi
fi
if [[ $SSHOT_UPLOAD = true ]];then
upload "${IMGLOC}"
fi
exit 0
}
mainf $@