How to use Applescript with Keynote

applescriptkeynote

My first Problem: For some reason the Applescript Editor is not able to pick up events when recording from Keynote'09 (I already turned on the acessibility options). The Editor stays blank for all actions I perform. It works perfectly for things like Finder though.

The resulting problem: I still want to tell Keynote to export presentations so I made this Script:

set thisFile to "/Users/ks/Desktop/test.key"
tell application "System Events"
    tell application "Keynote"
        activate
        open thisFile
        set frontmost to true
        click menu item "Export…" of menu 1 of menu bar item "File" of menu bar 1
    end tell
end tell

This script will choke on the last line (the one with "click menu item") when being saved saying: "expected end of line but found class name" and "menu item" will be highlighted. I don't know how to fix that. Any suggestions? I see code all over the web doing the exact same thing. Is this a version Issue because its Keynote '09?

Best Answer

The following works for me:

set thisFile to "/Users/danielbeck/Downloads/test.key"
tell application "System Events"
    tell application "Keynote"
        activate
        open thisFile
    end tell
    tell process "Keynote"
        click menu item "Export…" of menu of menu bar item "File" of menu bar 1
    end tell
end tell

Changes:

  • activate makes the application frontmost, AFAICT.
  • GUI scripting requires the process of application "System Events", not the application of the same name.
  • You need to enter the actual ellipsis character … (Opt-; on International layout) instead of the HTML entity.