Report abuse

set flickr_folder to ((path to home folder as text) & "scripts:flickr")
set the_xml_folder to flickr_folder & ":photoinfo"
set the_download_folder to flickr_folder & ":download"
set already_downloaded to list folder the_download_folder

set the_xml_files to get_xml_files(the_xml_folder)

set all_the_photos to get_all_photos()


set start_index to 1
-- use the following to restart the script from a photo other than
-- the first, if the script stopped downloading or whatever.
--set start_index to get_index_of_photo(all_the_photos, "foo.jpg")
--if start_index is -1 then
--  log "failed to find photo"
--  return 1
--end if

set photo_count to count of all_the_photos
repeat with i from start_index to photo_count
    log i
    set the_photo to (item i of all_the_photos)
    set the_filename to get_photo_filename(the_photo)
    set the_id to (item 1 of (string_to_list(the_filename, "_")))
    
    set the_data to load_xml(the_xml_folder & ":" & the_id & ".xml")
    set the_xml to parse_it(the_data)
    
    -- generate the full URL to the original photo file
    set the_photo_tag to getAnElement(the_xml, "photo")
    set the_title to getElementValue(getElementFromPath(the_xml, {"photo", "title"}))
    set the_description to getElementValue(getElementFromPath(the_xml, {"photo", "description"}))
    
    -- get a list of the tags associated with this photo
    set the_xml_tags to getElements(getElementFromPath(the_xml, {"photo", "tags"}), "tag")
    set the_tags to {}
    
    repeat with j from 1 to count of the_xml_tags
        set the_tag to (item j of the_xml_tags)
        set end of the_tags to getElementValue(the_tag)
    end repeat
    
    tell application "iPhoto"
        tell the_photo
            set title to the_title
            set comment to the_description
        end tell
        
        select the_photo
        set keywords_count to the count of the the_tags
        repeat with i from 1 to keywords_count
            set the_keyword to (item i of the the_tags)
            «event KAnewkey» given name:the_keyword
            assign keyword string the_keyword
        end repeat
    end tell
    
end repeat

return 0

on get_index_of_photo(the_photos, the_photo_filename)
    set the_count to count of the_photos
    repeat with i from 1 to the_count
        set the_photo to (item i of the_photos)
        tell application "iPhoto"
            tell the_photo
                set the_filename to image filename
                if the_filename is the_photo_filename then
                    return i
                end if
            end tell
        end tell
    end repeat
    
    return -1
end get_index_of_photo

on get_all_photos()
    tell application "iPhoto"
        set all_the_photos to the photos of photo library album
        return all_the_photos
    end tell
end get_all_photos

on get_photo_filename(the_photo)
    tell application "iPhoto"
        tell the_photo
            return the image filename
        end tell
    end tell
end get_photo_filename

on getElementValue(theXML)
    if theXML is missing value or theXML is {} then
        return ""
    else if class of theXML is string then
        return theXML
    else
        try
            return item 1 of XML contents of theXML
        on error number -1728
            return ""
        end try
    end if
end getElementValue

on getElementFromPath(theXML, theElementPath)
    if theElementPath is {} then
        return theXML
    else
        local foundElement
        
        set foundElement to getAnElement(theXML, item 1 of theElementPath)
        if foundElement is not missing value and ¬
            class of foundElement is XML element then
            return getElementFromPath(foundElement, rest of theElementPath)
        else
            return missing value
        end if
    end if
end getElementFromPath

on getAnElement(theXML, theElementName)
    -- find and return a particular element (this presumes there is only one instance of the element)
    
    repeat with anElement in XML contents of theXML
        if class of anElement is XML element and ¬
            XML tag of anElement is theElementName then
            return contents of anElement
        end if
    end repeat
    
    return missing value
end getAnElement

on getNamedElement(theXML, theElementName, theName)
    -- find and return the first element with a particular name attribute
    
    if class of theXML is XML element or class of theXML is XML document then
        repeat with anElement in XML contents of theXML
            try
                if class of anElement is XML element and ¬
                    XML tag of anElement is theElementName and ¬
                    |name| of XML attributes of anElement is theName then
                    return contents of anElement
                end if
            on error number -1728
                -- ignore this error
            end try
        end repeat
    else if class of theXML is list then
        repeat with anElement in theXML
            try
                if class of anElement is XML element and ¬
                    XML tag of anElement is theElementName and ¬
                    |name| of XML attributes of anElement is theName then
                    return contents of anElement
                end if
            on error number -1728
                -- ignore this error
            end try
        end repeat
    end if
    
    return missing value
end getNamedElement

on getElements(theXML, theElementName)
    local theResult
    
    set theResult to {}
    repeat with anElement in XML contents of theXML
        if class of anElement is XML element and XML tag of anElement is theElementName then
            set end of theResult to contents of anElement
        end if
    end repeat
    
    return theResult as list
end getElements

on get_xml_files(the_xml_folder)
    return list folder the_xml_folder
end get_xml_files

on load_xml(the_file)
    set fRef to open for access file the_file without write permission
    set the_contents to (read fRef)
    close access fRef
    return the_contents
end load_xml

on parse_it(the_xml_data)
    set the_xml to parse XML the_xml_data
    return the_xml
end parse_it

on string_to_list(the_string, the_delim)
    my atid(the_delim)
    set the_list to (every text item of the_string) as list
    my atid("")
    return the_list
end string_to_list

on atid(the_delim)
    set AppleScript's text item delimiters to the_delim
end atid