/
gtk-scrot
executable file
·357 lines (313 loc) · 9.82 KB
/
gtk-scrot
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
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
#!/bin/bash
# this script is a gtkdialog front end to scrot, the screenshot utility.
# script by Ahau <ahau@porteus.org>
. /usr/lib/librokenman
#exec 2>/dev/null
## Variables
export GSCRT="/tmp/.gscrt_$$"
export PIXMAPS=/usr/share/pixmaps/porteus
export dirloc=`readlink -f ~/Desktop`
mkdir -p $GSCRT
### Skip the first screen if the script is called with the '-s' option
if [ "$1" != "-s" ]; then
#create a list of filetypes
cat << EOF >> $GSCRT/typelist
bmp
jpg
tif
png
EOF
#create a list of seconds to delay
cat << EOF >> $GSCRT/delaylist
1
3
5
10
30
60
EOF
## here's the first screen that accepts user input
export CAPT_IMAGE='
<window window_position="1" title="Screenshot" icon-name="camera" allow-shrink="false" width-request="450" height-request="200">
<vbox margin="10">
<hbox>
<pixmap><height>48</height><width>48</width><input file icon="camera"></input></pixmap>
'`txtcolor 300 darkred x-large normal " Take a Screenshot"`'
</hbox>
'`hsep`'
<hbox>
<vbox>
<hbox>
<vbox>
<text xalign="0" wrap="true" width-request="175"><label>"Select Region:"</label>
</text>
<radiobutton active="true" width-request="175">
<label>Entire Screen</label>
<variable>WHOLESCREEN</variable>
<action>if true enable:DELAY</action>
</radiobutton>
<radiobutton active="false" width-request="175">
<label>Active Window</label>
<variable>ACTIVEWINDOW</variable>
<action>if true enable:DELAY</action>
</radiobutton>
<radiobutton active="false" width-request="175">
<label>Click and Drag</label>
<variable>CLICKDRAG</variable>
<action>if true disable:DELAY</action>
</radiobutton>
</vbox>
</hbox>
</vbox>
<vbox>
<hbox>
<text use-markup="true" width-request="150" selectable="true" can-focus="no">
<label>"Filetype:"</label></text>
<comboboxtext focus-on-click="false" width-request="60">
<variable>FILETYPE</variable>
<input file>'"$GSCRT/typelist"'</input>
<default>'png'</default>
</comboboxtext>
</hbox>
<hbox>
<text use-markup="true" width-request="150" selectable="true" can-focus="no">
<label>"Delay (in seconds):"</label></text>
<comboboxtext focus-on-click="false" width-request="60">
<variable>DELAY</variable>
<input file>'"$GSCRT/delaylist"'</input>
<default>'1'</default>
<sensitive>enabled</sensitive>
</comboboxtext>
</hbox>
</vbox>
</hbox>
<hbox>
<button cancel>
</button>
<button ok>
<variable>OKBUT</variable>
<sensitive>enabled</sensitive>
</button>
</hbox>
</vbox>
<action signal="hide">exit:Cancel</action>
</window>
'
gtkdialog -p CAPT_IMAGE >> $GSCRT/dump
## Did user cancel ?
[ `egrep -o "Cancel|abort" $GSCRT/dump` ] && rm -rf $GSCRT && exit
## get all the parameters that affect how to take the shot
file_extension=`grep FILETYPE $GSCRT/dump|tail -n1|sed -e 's/FILETYPE="//g' -e 's/\"//g'`
delay_count=`grep DELAY $GSCRT/dump|tail -n1|sed -e 's/DELAY="//g' -e 's/\"//g'`
if grep WHOLESCREEN $GSCRT/dump|grep true; then
region=''
elif grep ACTIVEWINDOW $GSCRT/dump|grep true; then
region='-u -b'
elif grep CLICKDRAG $GSCRT/dump|grep true; then
region='-s -b'
fi
#take the screenshot, if the script was not called with '-s'
(cd $GSCRT && scrot -q 75 -d $delay_count $region '%Y-%m-%d--%s_$wx$h.'$file_extension'' -e 'echo "filename is $f" >> '"$GSCRT/dump"'')
else
##now we take the picture and pass additional info if the script
##was called with '-s'
(cd $GSCRT && scrot -q 75 '%Y-%m-%d--%s_$wx$h.png' -e 'echo "filename is $f" >> '"$GSCRT/dump"'')
file_extension=png
fi
#get some info about the picture and what programs we can open it with
export filename=`grep 'filename is' $GSCRT/dump|tail -n1|sed 's/filename is //g'`
export newname=$filename
for a in `grep "image/$file_extension" /usr/share/applications/mimeinfo.cache|cut -d '=' -f2|sed 's/;/ /g'`; do
for b in `find /usr/share/applications -name $a`; do
app_name=`grep '^Name=' $b|sed 's/Name=//g'`
app_exec=`grep '^Exec=' $b|sed 's/Exec=//g'|cut -d ' ' -f1`
echo $app_name >> $GSCRT/proglist
echo $app_name $app_exec >> $GSCRT/execlist
done
done
##this fuction opens a gtkdialog to find out what the user wants to do with
##the image after it's taken.
image_action()
{
export IMAGE_ACTION='
'`start_window "Save or Edit Image" camera 450`'
<frame>
<hbox>
<text use-markup="true" width-request="375" selectable="true" can-focus="no">
<label>"Would you like to save your screenshot or open it inside an application?"</label></text>
</hbox>
<hbox>
<text use-markup="true" width-request="75" selectable="true" can-focus="no">
<label>"File name:"</label></text>
<entry editable="true" width-request="150">
<variable>NEWNAME</variable>
<default>'`echo $newname`'</default>
</entry>
</hbox>
<hbox>
<vbox>
<radiobutton active="true" height-request="32">
<label>Save to folder...</label>
<variable>RBTSAVE</variable>
<action>if true enable:PDIR</action>
<action>if true enable:DIRBUT</action>
<action>if true disable:OPENPROG</action>
</radiobutton>
<radiobutton active="false">
<label>Open with:</label>
<variable>RBTOPEN</variable>
<action>if true disable:PDIR</action>
<action>if true disable:DIRBUT</action>
<action>if true enable:OPENPROG</action>
</radiobutton>
</vbox>
<vbox>
<hbox>
<entry editable="false" fs-title="Select an existing folder" fs-action="folder" fs-folder="/tmp" height-request="32">
<variable>PDIR</variable>
<default>'`echo $dirloc`'</default>
</entry>
<button width-request="32">
<input file stock="gtk-directory"></input>
<variable>DIRBUT</variable>
<action>fileselect:PDIR</action>
</button>
</hbox>
<hbox>
<comboboxtext focus-on-click="false" width-request="175">
<variable>OPENPROG</variable>
<input file>'"$GSCRT/proglist"'</input>
<sensitive>disabled</sensitive>
</comboboxtext>
</hbox>
</vbox>
</hbox>
<hseparator></hseparator>
<hbox>
<text use-markup="true" width-request="375" selectable="true" can-focus="no">
<label>"Preview:"</label></text>
</hbox>
<hbox>
<pixmap><height>250</height><width>250</width>
<variable>PREVIEW</variable>
<input file>'$GSCRT/$filename'</input></pixmap>
</hbox>
</frame>
<hbox>
<button cancel>
</button>
<button>
<variable>OKBUT</variable>
<label>OK</label>
<input file stock="gtk-apply"></input>
</button>
</hbox>
'`end_window`'
'
gtkdialog -p IMAGE_ACTION >> $GSCRT/dump
[ `egrep -o "Cancel|abort" $GSCRT/dump` ] && rm -rf $GSCRT && exit
newname=`grep NEWNAME $GSCRT/dump|tail -n1|sed -e 's/NEWNAME=//g' -e 's/\"//g'`
if grep RBTSAVE $GSCRT/dump|tail -n1|grep true; then
action=save
else
action=open
fi
if [ "$action" == "open" ]; then
finallocation=`echo /tmp/$newname`
elif [ "$action" == "save" ]; then
dirloc=`grep PDIR $GSCRT/dump|tail -n1|sed -e 's/PDIR=//g' -e 's/\"//g'`
finallocation=`echo $dirloc/$newname`
fi
}; export -f image_action
image_action
## this function warns the user if there is already a file in place with their
## chosen directory/filename and asks if we should overwrite.
check_existing()
{
. /usr/lib/librokenman
echo '
<window window_position="1" title="Overwrite?" icon-name="gtk-dialog-warning" allow-shrink="false" width-request="475">
<vbox>
<hbox>
<frame>
<pixmap icon_size="6">
<input file stock="gtk-dialog-warning"></input>
</pixmap>
</frame>
<frame>
<text wrap="true"><label>"The file '"$finallocation"' already exists. Do you wish to overwrite it? "</label></text>
</frame>
</hbox>
<hbox>
<button>
<variable>NOBUT</variable>
<label>No</label>
<input file stock="gtk-stop"></input>
<action type="command">echo OVERWRITE=false >> $GSCRT/dump </action>
<action type="exit">done</action>
</button>
<button>
<variable>YESBUT</variable>
<label>Yes</label>
<input file stock="gtk-apply"></input>
<action type="command">echo OVERWRITE=true >> $GSCRT/dump </action>
<action type="exit">done</action>
</button>
</hbox>
</vbox>
<action signal="hide">exit:abort main window by X</action>
</window>
' | gtkdialog -s
}; export -f check_existing
while [ -e $finallocation ]; do
check_existing
overw=`grep OVERWRITE $GSCRT/dump|tail -n1|sed 's/OVERWRITE=//g'`
if [[ "$overw" == "true" ]]; then
break
else
## if you don't want to overwrite the image, we bring back the
## processing window to allow the user to change the location and filename
image_action
fi
done
## move the image to /tmp and open it with the desired application
if [ "$action" == "open" ]; then
mv $GSCRT/$filename $finallocation
progname=`grep OPENPROG $GSCRT/dump|tail -n1|sed -e 's/OPENPROG=//g' -e 's/\"//g'`
exec_command=`grep "^$progname" $GSCRT/execlist|tail -n1|awk '{print$NF}'`
$exec_command /tmp/$newname &
fi
## or, move the image to the specified location
if [ "$action" == "save" ]; then
dirloc=`grep PDIR $GSCRT/dump|tail -n1|sed -e 's/PDIR=//g' -e 's/\"//g'`
mv $GSCRT/$filename $finallocation
if [ "$?" == "0" ]; then
#let the user know it copied successfully
echo '
<window window_position="1" title="Image saved" icon-name="camera" allow-shrink="false" width-request="475">
<vbox>
<hbox>
<frame>
<pixmap icon_size="6">
<input file stock="gtk-ok"></input>
</pixmap>
</frame>
<frame>
<text wrap="true"><label>"Your image has been saved as
'"$finallocation"' "</label></text>
</frame>
</hbox>
<hbox>
<button ok></button>
</hbox>
</vbox>
<action signal="hide">exit:abort main window by X</action>
</window>
' | gtkdialog -s
else
#or warn them that an error occurred. This should be rare.
gtk_message "An error occurred while attempting to save your screenshot. Please try again." 400 gtk-dialog-error
fi
fi
rm -rf $GSCRT &&
exit