iphoto 4 import

I am about ready to pull out my hair. I am trying to write an apple script that does among other things, imports a folder into iphoto. The part of the relavent script is:

set thealbumname to “To Be Captioned”
tell application “iPhoto”
if (album thealbumname exists) is not true then
try
new album name thealbumname
on error the_error
display dialog the_error giving up after 30
end try
end if
tell application “Finder”
activate
set thefilelist to every file in (choose folder)
end tell
repeat with i in every item in thefilelist
try
tell application “Finder”
set thefile to POSIX path of (i as alias)
end tell
activate
import from thefile to album thealbumname
on error the_error
display dialog the_error giving up after 30
end try
end repeat
end tell

Ok, it looks right. It compiles fine. it the logs look correct. it just doesn’t work. No photos get imported.

it does however work on the copy of iPhoto 5 I have on my work laptop.

any ideas?

I’m sorry to say i don’t have an answer for you.

I’m trying to do a bunch of iPhoto automation, and would love to see the full version of what you’re working on, though. I’m having trouble getting any functional examples of iPhoto scripts (since most of Apple’s sample scripts don’t even seem to work anymore).

For example, I can’t even get the finder to successfully pass on the names of files to import. I know that it’s an issue with aliases and the way that the Finder stores file paths (File “bob” inside folder “terry”, etc).

One example of my floudering:

That is actually the full version of the iPhoto section. The beginning part simply uses some unix shell scripts to modify the meta data of the photos. Put EXIF date into IPTC Caption, reformat as PNG from JPG.

As for your script, there are a buch of problems that I’ll comment on.

tell application “iPhoto”
set PhotosToAdd to every file in (choose folder) as alias (* iPhoto doesn’t understand the every file command. Run the line in a Tell Application “Finder” block. The “as alias” is causing problems because it is turing the choose folder into an alias instead of the items in it. It is also not needed as you make it an alias later on in the script*)
repeat with i from 1 to number of items in the PhotosToAdd
set currentPhoto to item i of the PhotosToAdd
set currentPhoto to POSIX path of (i as alias) (* In the line above, you made the variable currentPhoto into a file refernce. Then in this line you try to change it to something different. The above line is unneccessary. What you can do is use 2 different variable names. Also, i is not a file reference, it is simply a counter. You need to set the item of i in photostoadd to an alias, not just the count number. *)
try
import from currentPhoto to NewAlbum (You need to define what the variable NewAlbum is)
end try
end repeat
end tell

Here is the fixed code

tell application “iPhoto”
set NewAlbum to current album – We define the New Album variable.
tell application “Finder” --added the Tell Finder block
set PhotosToAdd to every file in (choose folder)
end tell
repeat with i from 1 to number of items in the PhotosToAdd
set thecurrentPhoto to item i of the PhotosToAdd – split this into 2 variables
set currentPhoto to POSIX path of (thecurrentPhoto as alias)
try
import from currentPhoto to NewAlbum
end try
end repeat
end tell

do note however, this still won’t work on iPhoto 4 because I can’t get the import command to work.

Thanks so much for your comments on the script – helped me immessurably in understanding the how and why, not just fixing it.

I don’t see an import command in iPhoto’s dictionary in any case. Is this something added in iPhoto 4?

Using the above code I got an error after a while. So rìght before the try-block, I inserted this:

repeat while importing
	delay 0.02
end repeat

You might like to adjust the delay depending on your system. My iPhoto 6 hung-up after approx. 126 images when I tried with delay 0

With the following script, I just imported approx. 6oo images and movies from 66 folders.

tell application "iPhoto" to activate

tell application "Finder"
	activate
	set basefolder to (choose folder)
	set theList to every folder in basefolder
	repeat with theFolder in theList
		set thePhotos to every file in theFolder
		tell application "iPhoto"
			set newAlbumName to (name of theFolder as string)
			new album name newAlbumName
			set newAlbum to album newAlbumName
			repeat with onePhoto in thePhotos
				set onePhotoPath to POSIX path of (onePhoto as alias)
				repeat while importing
					delay 0.02
				end repeat
				try
					import from onePhotoPath to newAlbum
				end try
			end repeat
		end tell
	end repeat
end tell