Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!

Take a picture with the iSight camera when a folder is opened Desktop
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 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.
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 tell
This 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.]
    •    
  • Currently 2.32 / 5
  You rated: 3 / 5 (28 votes cast)
 
[23,279 views]  

Take a picture with the iSight camera when a folder is opened | 3 comments | Create New Account
Click here to return to the 'Take a picture with the iSight camera when a folder is opened' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
Take a picture with the iSight camera when a folder is opened
Authored by: CubaMark on Jan 21, '10 08:46:30AM

Anyone manage to make this work with 10.5.8 and the authsight hint posted awhile back, to capture photos at failed login attempts?

http://www.macosxhints.com/article.php?story=2006120918170984&query=isight+login



[ Reply to This | # ]
Take a picture with the iSight camera when a folder is opened
Authored by: mael on Jan 23, '10 03:46:58PM
Thank you! That *is* a useful script. Since I like my pictures in the corresponding folder, don't need a log (as I will use them from the Terminal) and want them sortable by date, if changed the script slightly. Thanks again! FWIW:

on opening folder this_folder
	
	set filename to "/$(date \"+%Y-%m-%d_%H:%M\").jpg"
	set snapshots_folder to "__Gotcha" --  change this name to your liking
	set pictures_folder to (path to the pictures folder)
	
	--  check if pictures folder exists, and create, if not
	tell application "Finder"
		if not (exists folder snapshots_folder of pictures_folder) then
			make new folder at alias (pictures_folder as text) with properties {name:snapshots_folder}
		end if
	end tell
	
	--  take picture and save it
	--  the binary imagesnap must be installed in /usr/local/bin (or the path below must be adapted)
	do shell script "/usr/local/bin/imagesnap " & (POSIX path of pictures_folder & snapshots_folder & filename)
	do shell script "chflags hidden " & (POSIX path of pictures_folder & snapshots_folder & filename)
	
end opening folder


[ Reply to This | # ]
Take a picture with the iSight camera when a folder is opened
Authored by: mael on Jan 23, '10 03:49:10PM

btw: too bad that folder action scripts only work when in symbol view. I am usually working in column view, where nothing happens... Not even if I explicitly try to open it with Cmd-O...
*sigh*



[ Reply to This | # ]