This script describes an AppleScript that uses Imagesnap, a public-domain command line tool, to take a photo with the built-in iSight camera when a specified file is opened. It saves the photo as a JPEG with a filename based on the time/date, and then hides the JPEG. The light by the side of the camera will flash briefly when the snap is taken.
This hint could be very easily adapted to run lots of different functions, for example taking a screen shot. Before writing this script, I had not realized the large potential of Folder Actions Setup. Also described is a method to simply temporarily disable the action when you are opening the folder.
First create a new folder called UnixApps in the main (top-level) Library folder. After downloading Imagesnap, place the Imagesnap application in the new folder (resultant path: /Library/UnixApps/imagesnap). It could easily be saved elsewhere, but that would require some small changes to the AppleScript.
Now copy and paste the following AppleScript into Script Editor. Save it in the Library/Scripts/Folder Action Scripts folder, and name it open folder.scpt. Saving it there allows it to be attached to any folder.
on opening folder this_folder
--finds out who is logged in
set myusername to do shell script "whoami"
--creates new folder for pic in home folder called Sesame
--easy to change name of folder by changing next line
set picFolder to "Sesame"
set HomeFolder to (path to current user folder)
tell application "Finder"
if not (exists folder picFolder of HomeFolder) then
make new folder at HomeFolder with properties {name:picFolder}
end if
end tell
set PhotoDirectory to ("/Users/" & myusername & "/" & picFolder)
--start by creating an original filename based on the date and time
-- Get the "hour"
set timeStr to time string of (current date)
set Pos to offset of ":" in timeStr
set theHour to characters 1 thru (Pos - 1) of timeStr as string
set timeStr to characters (Pos + 1) through end of timeStr as string
-- Get the "minute"
set Pos to offset of ":" in timeStr
set theMin to characters 1 thru (Pos - 1) of timeStr as string
set timeStr to characters (Pos + 1) through end of timeStr as string
--Get "day and date" and convert it to something usable
set DateStr to date string of (current date)
set Pos to offset of "," in DateStr
set theDay to characters 1 thru (Pos - 1) of DateStr as string
set theDATE to characters (Pos + 2) through end of DateStr as string
set Pos to offset of " " in theDATE
set theDayNumber to characters 1 thru (Pos - 1) of theDATE as string
set theMonthandYear to characters (Pos + 1) through end of theDATE as string
set Pos to offset of " " in theMonthandYear
set theMonth to characters 1 thru (Pos - 1) of theMonthandYear as string
set theyear to characters (Pos + 1) through end of theMonthandYear
-- set the file name
set TheFileName to (theDay & "_" & theDayNumber & "_" & theMonth & "_" & theyear & "_" & theHour & theMin)
-- take the photo
set SavePhoto to (PhotoDirectory & "/" & TheFileName & ".jpg")
--IMPORTANT COMMENT
--alter the following line if you have not saved imagesnap to UnixApps
set ImageSnapCommand to ("/Library/UnixApps/imagesnap" & " " & SavePhoto)
do shell script ImageSnapCommand
--create log file
do shell script "date >> " & PhotoDirectory & "/SesameLog.txt"
delay 10
--hide pic
do shell script "chflags hidden " & SavePhoto
end opening foldertell application "System Events" set folder actions enabled to false end tell delay 3 tell application "System Events" set folder actions enabled to true end tellThis AppleScript can be linked to a keyboard shortcut using a script launcher such as Spark, Butler (download link), or any of the others available. You can then just press Shift+Z (for example) before opening the file, and the photo won't be triggered.
Mac OS X Hints
http://hints.macworld.com/article.php?story=20100117064356428