iPhoto add many photos to an album at once (not one by one)

I am trying to write an AppleScript for iPhoto that will create a few albums and add photos which are already in iPhoto into these albums based on each photo’s EXIF data. A perl script handles the EXIF parsing, grouping of photos based on the EXIF data. The perl script will result in a list of all the original file paths to each desired photo, as well as the album name each photo should be added to. It will then generate the required AppleScript to do move each photo into the correct albums in iPhoto.

In it’s current form the AppleScript is working, but very slow. This is because it adds photos one-by-one into their various albums. I cannot find the correct iPhoto AppleScript grammar to add several photos at a time based on their original path property.

Here is my script in its current form, with some bits removed for clarity:


-- album_names is a list of names for new albums
-- Each entry in album_names corresponds to an entry in album_files
set album_names to {"Search Results 1", "Search Results 2"}

-- Build album_files, which is a list of lists
-- Each list in album_names corresponds to an album name from album_names
-- Each list in album_names is a list of filenames that should be put into their own new album
-- This structure is being built in this manner for convenience of calling it through perl.
set album_files to {}
set album_file to {}
copy "/Users/x/Pictures/iPhoto Library/Originals/2011/AlbumName/DSC_3199.JPG" to end of album_file
copy "/Users/x/Pictures/iPhoto Library/Originals/2011/AlbumName/DSC_3200.JPG" to end of album_file
copy "/Users/x/Pictures/iPhoto Library/Originals/2011/AlbumName/DSC_3201.JPG" to end of album_file
copy album_file to end of album_files
set album_file to {}
copy "/Users/x/Pictures/iPhoto Library/Originals/2011/AlbumName/DSC_3202.JPG" to end of album_file
copy "/Users/x/Pictures/iPhoto Library/Originals/2011/AlbumName/DSC_3203.JPG" to end of album_file
copy "/Users/x/Pictures/iPhoto Library/Originals/2011/AlbumName/DSC_3204.JPG" to end of album_file
copy album_file to end of album_files
set album_file to {}

tell application "iPhoto"
	-- Album creation removed for simplification of example
	
	-- Find the photos and add them to their albums
	repeat with album_number from 1 to (count album_names)
		set album_name to item album_number of album_names
		set file_list to item album_number of album_files
		repeat with file_path in file_list
			set photo_list to (every photo of photo library album whose original path is file_path)
			
			-- Error checking removed for simplication of example
			
			add item 1 of photo_list to album album_name
		end repeat
	end repeat
end tell

As you can see, the code finds photos one by one in iPhoto based on their “original path” property, and adds them to the correct album. I would much rather add all photos to their albums using only one call per album. The following bit of code is my attempt at doing that:


tell application "iPhoto"
	repeat with album_number from 1 to (count album_names)
		set album_name to item album_number of album_names
		set file_list to item album_number of album_files
		add (every photo of photo library album whose original path is in file_list) to album album_name
	end repeat
end tell

But this produces an error: Can’t make {“/Users/x/Pictures/iPhoto Library/Originals/2011/AlbumName/DSC_3199.JPG”, “…”} into reference. So I also tried:


tell application "iPhoto"
	repeat with album_number from 1 to (count album_names)
		set album_name to item album_number of album_names
		set file_list to item album_number of album_files
		add (every photo of photo library album whose (original path as text) is in file_list) to album album_name
	end repeat
end tell

But this also produces an error: Can’t make «class opth» of application “iPhoto” into type text. I don’t know AppleScript very well–I’ve pieced this code together using some heavy Googling. Could anyone assist me to make this script add all the photos for each album in one “add” call per album?

Model: MacBook Pro
AppleScript: 2.1.2
Browser: Safari 534.50
Operating System: Mac OS X (10.6)