AppleScripting Keynote PDF export

This past weekend I found myself in a situation where I would need to export 20 or so Keynote files to PDF. These generated PDFs all needed to have the same export options (no added dates, no presenter notes, etc.), and I really wanted to avoid having to go through all of them manually, clicking all the relevant boxes – which Keynote helpfully resets every time you hit Export.

Thankfully, there’s a (slightly) better way: AppleScript. With a couple hours’ work, I was able to tweak this script to work with the latest version of Keynote (5.3 at time of writing). The results aren’t great - the exported file always ends up in your Documents folder, and I don’t quite trust some of the delay statements - but it gets the job done with little more than a drag & drop.

on run
    display dialog "Drag keynote documents on me to convert to PDF."
end run

on open draggeditems
    repeat with thisFile in draggeditems as list
        tell application "Finder" to reveal item thisFile
        set thisFile to thisFile as alias

        tell application "Keynote" to open thisFile

        tell application "System Events"
            tell application process "Keynote"
                set frontmost to true

                click menu item "PDF…" of menu 1 of menu item "Export" of menu 1 of menu bar item "File" of menu bar 1

                repeat until sheet 1 of window 1 exists
                    delay 1
                end repeat

                tell sheet 1 of window 1
                    click radio button "Slides" of radio group 1
                    click checkbox "Print each stage of builds"
                    click checkbox "Include date"
                    click button "Next…"
                end tell

                repeat until button "Export" of sheet 1 of window 1 exists
                    delay 1
                end repeat

                tell sheet 1 of window 1
                    click button "Export"
                end tell

                delay 3
                keystroke "w" using command down
            end tell
        end tell

    end repeat
end open

Ideas for improvement? Clone the Gist on GitHub!