Take a picture with the iSight camera when a folder is opened
Jan 21, '10 07:30:00AM • Contributed by: puzzlebobble
Jan 21, '10 07:30:00AM • Contributed by: puzzlebobble
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.
Third, select any file that, when opened, you want to take a photo. This step can be repeated with multiple files. Control-click on the file and select Folder Actions Setup from the contextual menu. This will either give you a list of scripts that can be attached to the folder, from which you can choose the script you just saved, or you will need to manually select the folder and then open the script list using the '+' buttons. Also check that the Enable Folder Actions box is ticked.
That's it. Any time the folder is opened, a picture is taken and saved in a folder called Sesame that has been created in the current user's home directory. You won't automatically be able to see the file, as the AppleScript also made it invisible. However, a log file is created called SesameLog in the Sesame folder.
If you want to be able to open the file without triggering the camera, you can either turn off Folder Actions Enabled manually, or save this very short AppleScript that turns off folder actions for three seconds, so that the file can be opened.
[robg adds: I haven't tested this one.]
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 folderThat's it. Any time the folder is opened, a picture is taken and saved in a folder called Sesame that has been created in the current user's home directory. You won't automatically be able to see the file, as the AppleScript also made it invisible. However, a log file is created called SesameLog in the Sesame folder.
If you want to be able to open the file without triggering the camera, you can either turn off Folder Actions Enabled manually, or save this very short AppleScript that turns off folder actions for three seconds, so that the file can be opened.
tell 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.
[robg adds: I haven't tested this one.]
•
[23,723 views]
