Automating screencapture in OS X
October 26, 2008 – 3:00 pmI usually capture screens a lot when creating an article, blog post or presentation to show steps to follow in an application. And I wanted to automate this process to make it easier for me to get every step I go through without stopping to capture a screen and save it to a file. So I wrote a script that uses screencapture for the job.
screencapture is a command-line utility that is unique to Mac OS X. It can be used to capture the screen and save it to a file or copy it to the clipboard. Before writing the script I used sleep 2; screencapture -C ~/Desktop/screenshot.png for capturing the screen. Which captures the screen after 2 seconds, and saves it on the desktop, the -C option includes the mouse cursor in the picture.
The first thing the script does is set the $endtime variable to the time the script should end, which is 10 seconds from the time it started. $endtime is set to minutes and seconds concatenated together and converted to integer, for example 11:31:20 am will be 3120. The until loop will keep executing as long as the time in $endtime is not reached. The file names of the image files will be the time they were taken, and their extension is png because that is the default format of screencapture if no other format is specified.
#/bin/bash
#endtime = (minute)(second) + 10 seconds
declare -i endtime=`date "+%M%S"`+10
until test [`date "+%M%S"`] ">" [$endtime] ; do
#set the path of the images
filename="/Users/amgadsuliman/capturedimages/"
#set filename variable to the current date
#format: (year)(month)(day)-(hour)(minute)(second).png
filename+=`date "+%y%m%d_%H%M%S"`
filename+=.png
#capture image, options: C = include cursor, x = without sound
screencapture -Cx $filename
sleep 1 #delay 1 second between screencaptures
done





