#!/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;