grab database id of photo when importing into iPhoto

Hey everyone,

I’m using an Applescript to import photos into iPhoto, and I was having quite a bit of trouble trying to keep track of the ‘id’ of each photo after importing. Unlike iTunes, iPhoto doesn’t return a pointer to the object after importing, so I was stuck trying to find a workaround for grabbing the database of the id of the photo I just imported.

I couldn’t find a solution for this posted anywhere, so I came up with this hack. If anyone has any comments about the code here, and/or other suggestions to get this done, feel free to post em’! I figure that this is a pretty common thing to want to do, as it’s usually a good idea to keep track of all the stuff that you’ve already added to iPhoto, by storing the database IDs of each added photo somewhere outside of iPhoto…

-Clayton

	set tempFile to (POSIX file (tempPath & "/" & fName)) as alias
	tell application "iPhoto"
		if (exists (album "000temp000")) then
			set tempAlbum to album "000temp000"
			remove tempAlbum
		end if
		new album name "000temp000"
		set tempAlbum to album "000temp000"
		import from tempFile to tempAlbum
		delay 0.5
		repeat while (importing)
			delay 0.5 -- wait 1/2 second and try again
		end repeat
		set tempPhoto to first photo of tempAlbum
		set cTrackID to (id of tempPhoto)
		remove tempPhoto from tempAlbum
		remove tempAlbum
	end tell

Model: ppc G5 (10.5.8) & intel MBPro (10.6.2)
AppleScript: newest (as of 30 Jan 10)
Browser: Firefox 3.5.7
Operating System: Mac OS X (10.6)

Hi dru3m1r1,

How about


tell application "Finder" to set tempFiles to (get selection as alias list)
tell application "iPhoto"
	import from tempFiles
	delay 0.5
set tempPhoto to every photo of album "Last Import"
	
end tell

iPhoto can import a alias list or a list of posix paths.
In my tests, each time a import is done, what ever is in “Last Import” is cleared and the new ones go into there.
A batch is treated as a single import. So no need for checking ‘importing’ or creating a temp album as far as I can tell.