Home > Development > Timed iSight Shots, or Narcissism on the Cheap

Timed iSight Shots, or Narcissism on the Cheap

Since most recent Macs are shipping with built-in iSights I thought it might be fun to put together a way to snap a picture at a certain time and then send it off somewhere.

Downloading the Requirements

Using two great free utilities, isightcapture, and sendEmail, in combination with a little script, and launchd I’m going to show you how I got this done. To get started you will need to download isightcapture, and sendEmail.

Installing the Tools

After you have downloaded both applications you need someplace to put them. I recommend /usr/local/bin/. This is a common location for programs installed by users. You don’t want to put stuff where OSX puts its files. Assuming you downloaded the applications to your Downloads folder do the following in a terminal (the first line may be unnecessary if the directory already exists):

$ sudo mkdir /usr/local/bin
$ cd ~/Downloads
$ hdiutil attach isightcapture1_1.dmg
$ sudo cp /Volumes/isightcapture1_1/isightcapture /usr/local/bin/
$ hdiutil detach /Volumes/isightcapture1_1/
$ tar zxvf sendEmail-v1.55.tar.gz
$ sudo cp sendEmail-v1.55/sendEmail /usr/local/bin/

Now you have the tools installed, you need to create the script that pulls them together.

Writing Your Script

I use the following BASH shell script to take the picture, and them email it. The email address I am sending it to is for a Web Gallery of my .Mac account. When the email is received it is automatically added to the gallery. While I use my .Mac account accout, Flickr allows you to post pictures via email, and Blogger allows you to post to your blog via email as well. These are just a couple examples, there are many more.

#!/bin/bash
 
SERVER="mail server here"
USER="username"
PASS="password"
FROM="from@address.com"
TO="to@address.com"
 
DATE=`date "+%Y-%m-%d %H:%M:%S"`
SUBJECT="$DATE"
MESSAGE=" "
FILE1="/tmp/$DATE.jpg"
FILE2=$(echo -n $FILE1 | sed -e "s/\:/qq/g")
 
/usr/local/bin/isightcapture "$FILE2"
 
/bin/mv "$FILE2" "$FILE1"
 
/usr/local/bin/sendEmail \
-f $FROM \
-t $TO \
-u "$SUBJECT" \
-m "$MESSAGE" \
-a "$FILE1" \
-s $SERVER \
-xu $USER \
-xp $PASS
 
rm "$FILE1"

So what’s going on here? Okay, the first five variables, SERVER, USER,
PASS, FROM, and TO need to be set by you. SERVER is your outgoing mail server. The easiest way to get this is to look at your mail settings in Mail, or whatever you use for email. For .Mac it would be smtp.mac.com:587. USER and PASSWORD are just that, what you need to connect to the server. The FROM address should be something, I use my real email address so if for some reason the email bounces it will hopefully make it back to me and I will know it happened. TO is where you want the email to end up. You do not need to edit anything else. Save this file as isightcam.sh somewhere, /usr/local/bin/ is a good choice. You want to make sure the file is executable so from a terminal run the following:

$ sudo chmod +x /usr/local/bin/isightcam.sh

Testing Your Script

Now you want to make sure it worked. Again from a terminal, run your script:

$ /usr/local/bin/isightcam.sh

You should see the camera light go on as it takes your picture, then a notification that the email was sent successfully should appear. Assuming all is well edit your script and change the line with the sendEmail command:

From:

/usr/local/bin/sendEmail \

To (this just makes sendEmail not produce any output):

/usr/local/bin/sendEmail -q \

Automating the iSight

Now that our script is working the way we want let’s set up some automation. My choice is launchd. Its a bit more confusing than using a simple cron job, but one thing I really like about launchd is if my laptop is sleeping when the event is supposed to occur launchd will fire it off as soon as it can once my Mac wakes.

The following plist file needs to be created for our agent:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>Disabled</key>
<false/>
<key>Label</key>
<string>net.sterlinganderson.isightcam</string>
<key>ProgramArguments</key>
<array>
<string>/usr/local/bin/isightcam.sh</string>
</array>
<key>StartCalendarInterval</key>
<array>
<dict>
<key>Hour</key>
<integer>9</integer>
<key>Minute</key>
<integer>0</integer>
</dict>
<dict>
<key>Hour</key>
<integer>16</integer>
<key>Minute</key>
<integer>0</integer>
</dict>
</array>
</dict>
</plist>

Save this file as net.sterlinganderson.net.plist in ~/Library/LaunchAgents/. Note the following about this file.

  • The string for the command is currently set to /usr/local/bin/isightcam.sh, make sure you have that set to whatever path your isightcam script is at.
  • Look at the <array></array> tags after StartCalendarInterval. Currently it is set to run at 9:00 AM and 4:00 PM. If you want to run the script only once remove one of the <dict></dict> tag sets and also the <array></array> tag set that enclose the two <dict></dict> tag sets. When you only have one set of <dict></dict> tags you don’t need them wrapped in the <array></array> tags.

To load it run the following:

$ launchctl load ~/Library/LaunchAgents/net.sterlinganderson.net.plist

Unloading:

$ launchctl unload ~/Library/LaunchAgents/net.sterlinganderson.net.plist

Finishing Up

That’s it. If you have any issues please feel free to comment.

Development , , , , ,

  1. No comments yet.
  1. No trackbacks yet.