Brian Koponen

Programming and Tech Tips

iPhoto Batch Album Export Using AppleScript

I needed to automate a mass export from the old iPhoto '11. AppleScript UI Scripting made it an easy process, though time consuming. Since it is just scripting the UI of iPhoto, you have no questionable quality factors of third party programs. Before I wrote this, I didn't realize just how much UI scripting could handle. You can use the techniques to script just about anything if you have the patience for it.

Here is the script:

global rootFolder -- Export path
global exportType -- 1 = Original, 2 = Full Size JPG, 3 = Large Size JPG
global subFolderEvent -- 1 = No Event Subfolders, 2 = Event Subfolders

tell application "iPhoto"
    activate

    set startingAlbum to current album

    set rootFolder to "/Users/brian/Export/Original/"
    set exportType to 1
    set subFolderEvent to 1
    exportAlbum of me from startingAlbum

    set rootFolder to "/Users/brian/Export/FullJPG/"
    set exportType to 2
    set subFolderEvent to 1
    exportAlbum of me from startingAlbum

    set rootFolder to "/Users/brian/Export/LargeJPG/"
    set exportType to 3
    set subFolderEvent to 1
    exportAlbum of me from startingAlbum
end tell

--
-- Recursively export albums
--
to exportAlbum from selectedAlbum
    tell application "iPhoto"
        set originalRootFolder to rootFolder -- Push onto stack

        set rootFolder to rootFolder & (name of selectedAlbum) & "/" -- Add the name of the folder/album onto the end of the path

        set albumList to (children of selectedAlbum)

        -- export pictures if it is leaf node, recurse otherwise

        if (count of albumList) = 0 then
            uiScriptExport of me from selectedAlbum
        else
            repeat with childAlbum in albumList
                exportAlbum of me from childAlbum
            end repeat
        end if

        set rootFolder to originalRootFolder -- Pop off of stack
    end tell
end exportAlbum

--
-- Create and choose the folder to save to, start the export and wait for it to finish
--
to uiScriptExport from selectedAlbum
    tell application "iPhoto"
        activate

        -- Select the album
        select selectedAlbum

        -- Create save folder
        tell me
            do shell script "mkdir -p " & quoted form of rootFolder
            log rootFolder
        end tell

        tell application "System Events"
            tell process "iPhoto"
                keystroke "e" using {command down, shift down} -- Bring up the Export Window
                -- Wait for the window to appear
                repeat until (exists window "Export")
                    delay 1
                end repeat
                delay 1
                uiScriptExportSettings() of me
                delay 2

                keystroke return -- Save Dialog Appears
                uiScriptSaveDialog(rootFolder) of me
                delay 5

                keystroke return -- Export starts

                -- Wait for Export to finish
                repeat until (not (exists window "Export"))
                    delay 1
                end repeat
            end tell
        end tell
    end tell
end uiScriptExport

--
-- UI Scripting to pick the save location
--
on uiScriptSaveDialog(saveLocation)
    tell application "System Events"
        tell process "iPhoto"
            delay 3
            keystroke "g" using {command down, shift down} -- Navigate to Folder
            delay 3
            keystroke saveLocation
            delay 5
            keystroke return
        end tell
    end tell
end uiScriptSaveDialog

--
-- Switches between the different uiScripts needed for the different export types
--
to uiScriptExportSettings()
    if exportType is equal to 1 then
        uiScriptExportOriginalSettings() of me
    else if exportType is equal to 2 then
        uiScriptExportFullJPGSettings() of me
    else if exportType is equal to 3 then
        uiScriptExportLargeJPGSettings() of me
    else
        display dialog "No ExportType"
    end if
end uiScriptExportSettings

to uiScriptExportLargeJPGSettings()
    tell application "System Events"
        tell process "iPhoto"
            -- Kind
            tell pop up button 2 of group 1 of group 1 of tab group 1 of window 1
                click
                click menu item "JPEG" of menu 1
            end tell

            -- Size
            tell pop up button 1 of group 1 of group 1 of tab group 1 of window 1
                click
                click menu item "Large" of menu 1
            end tell

            -- JPEG Quality
            tell pop up button 3 of group 1 of group 1 of tab group 1 of window 1
                click
                click menu item "High" of menu 1
            end tell

            -- File name
            tell pop up button 1 of group 1 of group 1 of group 1 of tab group 1 of window 1
                click
                click menu item "Use title" of menu 1
            end tell

            -- Subfolder Format, will only exist for albums with more than 1 picture.
            try
                tell pop up button 1 of group 2 of group 1 of group 1 of tab group 1 of window 1
                    click
                    if subFolderEvent is 1 then
                        click menu item "None" of menu 1
                    else
                        click menu item "Event Name" of menu 1
                    end if
                end tell
            end try
            delay 1
        end tell
    end tell
end uiScriptExportLargeJPGSettings

to uiScriptExportFullJPGSettings()
    tell application "System Events"
        tell process "iPhoto"
            -- Kind
            tell pop up button 2 of group 1 of group 1 of tab group 1 of window 1
                click
                click menu item "JPEG" of menu 1
            end tell

            -- Size
            tell pop up button 1 of group 1 of group 1 of tab group 1 of window 1
                click
                click menu item "Full Size" of menu 1
            end tell

            -- JPEG Quality
            tell pop up button 3 of group 1 of group 1 of tab group 1 of window 1
                click
                click menu item "High" of menu 1
            end tell

            -- File name
            tell pop up button 1 of group 1 of group 1 of group 1 of tab group 1 of window 1
                click
                click menu item "Use title" of menu 1
            end tell

            -- Subfolder Format, will only exist for albums with more than 1 picture.
            try
                tell pop up button 1 of group 2 of group 1 of group 1 of tab group 1 of window 1
                    click
                    if subFolderEvent is 1 then
                        click menu item "None" of menu 1
                    else
                        click menu item "Event Name" of menu 1
                    end if
                end tell
            end try
            delay 1
        end tell
    end tell
end uiScriptExportFullJPGSettings

to uiScriptExportOriginalSettings()
    tell application "System Events"
        tell process "iPhoto"
            -- Kind
            tell pop up button 2 of group 1 of group 1 of tab group 1 of window 1
                click
                click menu item "Original" of menu 1
            end tell

            -- File name
            tell pop up button 1 of group 1 of group 1 of group 1 of tab group 1 of window 1
                click
                click menu item "Use title" of menu 1
            end tell

            -- Subfolder Format, will only exist for albums with more than 1 picture.
            try
                tell pop up button 1 of group 2 of group 1 of group 1 of tab group 1 of window 1
                    click
                    if subFolderEvent is 1 then
                        click menu item "None" of menu 1
                    else
                        click menu item "Event Name" of menu 1
                    end if
                end tell
            end try
            delay 1
        end tell
    end tell
end uiScriptExportOriginalSettings

The rootFolder variable is the path for the export.

The exportType variable is either 1, 2 or 3. 1 is the original file, 2 is a full size jpg and 3 is a large size jpg.

The subFolderEvent variable is either 1 or 2. 1 is that the exported files are not sorted into event subfolders, 2 is that they are.

Configure the script to export with the settings you want. In iPhoto, organize the albums you want exported into an export folder. Select the export folder and run the script. The script will create a folder structure matching the album hierarchy in iPhoto and export each album. You can have as many subfolders as you want and it will be matched in the export. Since this is UI scripting, you can't use the computer while the export is running or the script will try to click on buttons and type while you're doing something else. Weird things will happen.

If you just want to export your entire iPhoto library in original quality, drag every event into a new album. Then in the script, set the rootFolder to your save location, set exportType to 1 and subFolderEvent to 2. Select the album in iPhoto and run the script. You will end up with every event exported into its own folder.

Question or Comment?