27 February 2008

Screenshots

I've just discovered the most fun command ever:

vncviewer 127.0.0.1


I let my computer recursively explode for a minute before I closed it. I tried to get a screenshot, but I couldn't get my computer to do much until I closed vnc. Speaking of screenshots, however, I figured I'd post my screenshot script. I tried a bunch of different screenshot programs and pretty much hated all of them, so I ended up writing a script that uses ImageMagick's import tool. It depends on two other programs that I use fairly often and really like.

  • Zenity is like a really good version of the standard dialog command. It can show all the same types of dialogs, like calendars and progress bars and whatnot, but they're the standard GTK dialogs so they look good. Most of the time the only dialog type I use is file-selection, which is what the screenshot script uses; I tend to use libnotify to display other types of alerts instead of zenity's info dialogs

  • Notify-more is a python script that uses PyNotify, the python interface to gnome's standard libnotify. Libnotify comes with a command-line tool to access it (notify-send), and notify-more intentionally mirrors it's syntax so you can use them fairly interchangably, but notify-more provides several other features, like the pie display to show when the notification will timeout and buttons that launch commands.


Here's the screenshot script itself. I don't generally write my scripts to be used elsewhere, so it might not be particularly cross-platform. I have PrntScrn bound to launch screenshot root, and Alt+PrntScrn to screenshot window:

#!/bin/bash
action=${1:-"default"}

case $action in
root|window)
echo -n Touching temporary file...
filename=`mktemp /tmp/screenshotXXXX`;
echo done

if [ $action == root ]; then
echo -n Capturing root X window...
import -frame -window root png:$filename
echo done
printText="Captured root window into $filename"
else
echo -n Determining window ID...
windowID=`xdpyinfo | awk '/focus/ {print $3}' | sed s/,//`
echo $windowID

echo -n Capturing window...
import -frame -window $windowID png:$filename
echo done
printText="Captured window $windowID into $filename"
fi

notify-more -u low -t 5000 -i /usr/share/icons/gnome/32x32/devices/video-display.png "Screenshot" "$printText" -n "Open" -x "eog \"$filename\"" -n "Save" -x "$0 save \"$filename\"" -n "GIMP" -x "gimp \"$filename\"";;

save)
filename=$2

if [ "$filename" ]; then
echo -n Getting destination...
newfile=`zenity --file-selection --title "Save Screenshot" --save --confirm-overwrite`
echo done

if [ "$newfile" ]; then
echo -n Moving screenshot to $newfile...
mv "$filename" "$newfile"
echo done

notify-more -u low -t 5000 -i /usr/share/icons/gnome/32x32/devices/video-display.png "Screenshot" "Screenshot saved to $newfile" -n "Open" -x "eog \"$newfile\"" -n "GIMP" -x "gimp \"$newfile\""
fi
fi;;
esac

No comments: