How to (successfully) get album name as string in iPhoto?

Disclaimer: I’ve read every post in here with the word iPhoto, read the Apple Applescript reference (several) times, and looked at every iPhoto script available online.

What I’m trying to accomplish is to assign a keyword to each photo for the album it is in.

For the sake of simplicity, I’m just trying to get it to function. My test library has a few albums and a dozen photos, each album title is already a defined keyword, and each image only appears in a single album, so there shouldn’t be an issue with lists, multiple matches, or making new keywords.

tell application "iPhoto"
		activate
		set photo_list to selection
		repeat with i from 1 to the count of the photo_list
			set current_photo to item i of the photo_list
			set album_name to the name of the album of current_photo
			assign keyword string album_name
		end repeat
	end tell

If I run that, I get “iPhoto got an error: Can’t make name of album of photo id 4.294894E+9 into type reference”

if I change the middle line to

set album_name to the name of the album of current_photo as string

, then it returns “Can’t make name of <> of <> id 4.294894E+9 of application “iPhoto” into type string”

Reading through the apple Applescript docs, I thought it might be a unicode issue, so I changed everything from strings to Unicode text, and get the same result (of course with type Unicode text on the errors).

At this point I’m sure I’m missing something glaringly obvious. I think I’m pulling the name of the album as a simple string, but clearly I’m either completely wrong on what i’m doing or I’m completely wrong in what the data looks like. The name of the albums are “bob”, “tom”, “stephanie”, and similar, nothing exotic that i’d expect to cause issues.

I’d appreciate if someone could point out the error I’m making in my logic, syntax or whatever!

thanks
nathaniel

I think the problem here is you’re trying to use an “album” property of a photo, and that doesn’t exist. Photos don’t know what their album is. Furthermore, a photo can be in more than one album!

So if you want to accomplish something like this you’ll have to do something like: for each photo, go through all the albums, and ask if the album contains the photo.

Albums know about photos, but photos don’t know about albums.

Aah, thanks, that makes perfect sense. I know a photo can be in more than one album, which is why i specifically set up the test case so that it wasn’t so – figured it might return a list which would require parsing, but obviously it won’t return anything!

Let’s assume I don’t want to go through every image and see what albums it is in – am I correct that I could select an album, set the variable to the album name, and then do a loop on every image within that album, assigning the keyword?

This works for me:


tell application "iPhoto"
	set myAlbums to name of every album
	set albNo to count of myAlbums
	set thePhotos to {}
	repeat with kk from 1 to albNo
		set thePhotos to thePhotos & {title of every photo in album (item kk of myAlbums)}
	end repeat
end tell

If you copy the script to the script editor and run it, the result will be a nested list of the titles of every photo in every album in the variable “thePhotos”.