Search code examples
applescriptiphoto

Access "keyword" element of photo within iPhoto - Applescript


I need to access the name property of the keywords for a selection of photos within iPhoto. How can I access the keyword element of those photos? I can get to the properties of the photo but not to the keyword element.

I'm new to Applescript so I apologize if this is very noobish. Thanks for any help you can provide.

Edit: Here's what I've tried.

set keywordsRef to a reference to keyword of p     --p is a photo
set keyName to name of keywordsRef

I get error -1700. Not sure if that helps.

Edit 2:

tell application "iPhoto"
    activate
    try
        set myPhotos to selection

        if myPhotos is false or (the count of myPhotos) is 0 then
            error "Please select an image"
        end if

        repeat with p in myPhotos
            set keyname to the name of keyword 1 of p    --This works.
            set xmldata to xmldata & my addNewPhoto(p)   --Try to get keywords here.
        end repeat
    on error
        --display dialog "Error"
    end try
end tell

on addNewPhoto(p)
    set photodata to "<dict>" & return & my addPhotoName(name of p)  --This works.

    --set keyname to the name of keyword 1 of p            --This gets a syntax error at the "1" after keyword. It expects the end of line.

    set keycount to the count of keywords of p
    repeat with i from 1 to keycount
        --log (the name of keyword i of p as string)        --Syntax error also at "i" after keyword.
    end repeat

    set photodata to photodata & my addPhotoKeywords()
    set photodata to photodata & "</dict>" & return
end addNewPhoto

Solution

  • Given your photo has keywords, this works for me.

    tell application "iPhoto"
    
        set p to photo id 4.294981127E+9
    
        set keyname to the name of keyword 1 of p
    
    end tell
    

    To loop thru them:

    tell application "iPhoto"
    
        set p to photo id 4.294981127E+9
    
        set keycount to the count of keywords of p
    
        repeat with i from 1 to keycount
            log (the name of keyword i of p as string)
        end repeat
    
    end tell
    

    If these don't work it might be in the way you are initialising the photo (p) variable, if so include that code in your question too.