Commit a5edd869 authored by Glen Mével's avatar Glen Mével

improve `screenshot`

parent e4ae69da
......@@ -3,50 +3,69 @@
# Take a screenshot.
# Usage: screenshot [root|active] (defaults to root)
SCREEN_DIR=~/Documents/Images/Screenshots/
SCREEN_PROMPT=yes # Ask for a custom filename with i3-input?
DATE=yes # Save date in the filename?
window='root'
# Directory where to put all screenshots.
SCREENSHOTS_DIR=~/Documents/Images/Screenshots
# If set to 'yes', the user will be prompted a caption (and may abort).
PROMPT_CAPTION=yes
# If set to 'yes', a timestamp will be written in the file name.
TIMESTAMP=yes
# Determine which window to shoot.
case "$1" in
'root')
'root' | '' )
window='root'
winname='root'
;;
'active')
if ! which xprop; then
'active' )
command -v xprop >/dev/null || {
i3-nagbar -t error -m 'Capturing a specific window requires the program ‘xprop.’'
exit
fi
exit 1
}
window=`xprop -root | grep "_NET_ACTIVE_WINDOW(WINDOW)" | cut -d' ' -f5`
winname=`xprop -id "$window" | sed -n '/WM_CLASS/s/.* = "\([^\"]*\)".*/\1\n/p'`
[ -z "$winname" ] && winname='window'
;;
*)
i3-nagbar -t error -m 'invalid argument: expecting "root" or "active"'
exit 1
esac
[ "$SCREEN_PROMPT" == "yes" ] && \
name=`i3-input -P 'screen name: ' | sed -n '/command = /s/command = //p'`
# Create the temporary file.
tmpfile="$(mktemp --tmpdir "screenshot-$winname-XXXX.png")" || {
i3-nagbar -t error -m 'Could not create a temporary file.'
exit 1
}
# Take the screenshot.
# NOTE: Add options -border, -frame to include these elements in the picture.
import -window "$window" "$tmpfile"
# Preview the screenshot.
command -v feh >/dev/null && feh -F "$tmpfile" &
if [ -z "$name" ];then
if [ "$window" == "root" ]; then
name='root'
else
name=`xprop -id "$window" | sed -n '/WM_CLASS/s/.* = "\([^\"]*\)".*/\1\n/p'`
[ -z "$name" ] && name='window'
# Ask a caption.
if [ "$PROMPT_CAPTION" = "yes" ] ; then
caption=`i3-input -P 'CAPTION: ' | grep '^command = '`
# If the user aborted, we discard the picture.
if [ -z "$caption" ] ; then
rm "$tmpfile"
exit 2
fi
caption="${caption#command = }"
fi
if [ "$DATE" == "yes" ]; then
filename="$name (`date +%Y.%m.%d-%H:%M:%S`).png"
# If the user gave no caption, use the name of the window as a default.
[ -z "$caption" ] && caption="$winname"
# Append the timestamp.
if [ "$TIMESTAMP" = "yes" ]; then
filename="$caption (`date +%Y.%m.%d-%H:%M:%S`).png"
else
filename="$name.png"
filename="$caption.png"
fi
# Pause to prevent the i3-input window from being captured on root window.
[ "$SCREEN_PROMPT" == "yes" ] && \
sleep .1
#import -border -frame -window "$window" "$SCREEN_DIR/$filename"
import -window "$window" "$SCREEN_DIR/$filename"
ln -sf "$filename" "$SCREEN_DIR/last"
# Move the picture to its destination.
mv -f "$tmpfile" "$SCREENSHOTS_DIR/$filename" || exit 1
ln -sf "$filename" "$SCREENSHOTS_DIR/last"
exit
printf 'Screenshot saved as %q\n' "$SCREENSHOTS_DIR/$filename"
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment