scr.sh

  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
#!/usr/bin/env bash
# Constants
readonly TRUE=0;
# Variables
APP_ICON="emblem-photos";
APP_NAME="$(basename $0)";
FILE_NAME="$HOME/$(date +%F_%H%M%S).png";
LANG="${LANGUAGE#*:}";
BORDER_SIZE="20"; #px
SCRIPT_DEPS=(
xdpyinfo
import
awk
sed
);
# Check script dependencies
function require () {
local NF;
while (($#)); do
test -z $(which $1 2> /dev/null) && NF+=("$1");
shift;
done
# Return the elements of array what have been not found
test ${#NF[@]} -ne 0 && {
echo ${NF[@]} && return 1;
}
return 0;
}
# Show message
function message () {
MESSAGE="$@";
notify-send -t 10000 -i $APP_ICON "$APP_NAME" "$MESSAGE" &> /dev/null \
|| echo -e "$@" 1>&2;
}
# Getting of selected area size
function getAreaSize () {
AREA=( $(
import -verbose /dev/null 2>&1 \
| sed 's/.*PS.\(.*\)[0-9][0-9]-.*/\1/g;s/[x+]/ /g' \
| awk '{print $1" "$2" "$5" "$6}';
)
)
# is fail?
test ${#AREA[@]} -ne 4 && return 2;
# is success?
# Return points array (x2-x1, y2-y1, x1, y1)
echo ${AREA[@]};
return 0;
}
# Increase rectangle size
function increaseRect () {
# FIXME: Complete this function :)
local POINTS=($@);
local x1=${POINTS[2]};
local y1=${POINTS[3]};
local x2=$(( ${POINTS[2]} + ${POINTS[0]} ));
local y2=$(( ${POINTS[3]} + ${POINTS[1]} ));
for i in ${#POINTS[@]}; do
local DONE=0;
xdpyinfo -ext XINERAMA | sed '/^ head #/!d;s///' |
while IFS=' :x@,' read i w h x y; do
case $i in
0)
;;
1)
;;
2)
;;
3)
;;
esac
#echo -e "x=$x\ny=$y\nwidth=$w\nheight=$h";
# test $DONE -eq 1 || { return 3 }
DONE=1;
done
done
return $DONE;
}
# Make screenshot
function makeScreenshot () {
local POINTS=($@);
local ANY_WARNINGS=$(
import -window root \
-crop "${POINTS[0]}x${POINTS[1]}+${POINTS[2]}+${POINTS[3]}" \
"$FILE_NAME" 2>&1 | awk -F':|@' '{print $2}';
);
test -z "$ANY_WARNINGS" || {
echo $ANY_WARNINGS;
return 3;
}
return 0;
}
### Check script dependency
NOT_FOUND=( $(require "${SCRIPT_DEPS[@]}") );
test ${PIPESTATUS[0]} -eq $TRUE || {
message "ERROR: Unresolved dependencies:\n$(printf '%s\n' ${NOT_FOUND[@]})";
exit 1;
}
### Try to get area size
SELECTED=( $(getAreaSize) );
test ${PIPESTATUS[0]} -eq $TRUE || {
message "ERROR: Can't get area size";
exit 2;
}
# TODO: uncomment this block when increaseRect works fine
### Make screenshot
##WARNING="$(makeScreenshot ${SELECTED[@]})";
##test ${PIPESTATUS[0]} -eq $TRUE || {
## message "ERROR: $WARNING";
## exit 3;
##}
increaseRect ${SELECTED[@]};
message "Screenshot successfully created:\n$FILE_NAME";
exit 0;