Does anyone know how to export a Keynote '09 presentation to .pdf using AppleScript? There is no “Export” entry in Keynote’s dictionary, and I can’t seem to access the “Export” menu item using System Events.
Any help would be greatly appreciated.
Does anyone know how to export a Keynote '09 presentation to .pdf using AppleScript? There is no “Export” entry in Keynote’s dictionary, and I can’t seem to access the “Export” menu item using System Events.
Any help would be greatly appreciated.
There’s lots of posts on here about how to access the pdf menu from the print menu of an application using gui scripting techniques. Do some searching and I’m sure you’ll find something to help you.
Thanks, Hank,
I’ve done lots of searching and I’ve been trying to use GUI scripting to make it work, but I consistently get the following error: System Events got an error: Can’t get menu item “Export…” of menu “File” of menu bar item “File” of menu bar 1 of application process “Keynote”.
Here’s the code I’m working from.
set myName to "My Slideshow"
tell application "System Events"
if (system attribute "sysv") < 4144 or UI elements enabled then
tell application process "Keynote"
click menu item "Export..." of menu "File" of menu bar item "File" of menu bar 1
click radio button "PDF" of radio group 1 of sheet 1 of window 1
click button "Next..." of sheet 1 of window 1
keystroke myName
click button "Export" of sheet 1 of window 1
end tell
end if
end tell
I have assistive devices enabled in universal access. I just can’t seem to resolve this error.
Again, your help is appreciated.
I managed to get this to work to the point where a decision needs to be made about were to save the exported PDF. Since the script I posted earlier doesn’t work, I thought I’d update with the working script (to a point). BTW, the problem was using three dots instead of an ellipsis when defining the Export and Next buttons.
If I have a valid path stored in a variable, say, my_path, is it possible to use System Events to interact with the drop-down “Save As” sheet to point to my_path? Notice in the script below, I’ve listed all the UI elements on the sheet where I think the interaction should go. How might I access one of those elements to save the .pdf to my_path?
Thanks again for any help.
--assumes a keynote presentaion is available for export
set file_name to "Test presentation"
tell application "System Events"
if (system attribute "sysv") < 4144 or UI elements enabled then
tell application process "Keynote"
set frontmost to true
click menu item "Export." of menu "File" of menu bar item "File" of menu bar 1
click button 3 of sheet 1 of window 1
click button "Next." of sheet 1 of window 1
keystroke file_name
--utility to list all possible UI elements
set uiNames to every UI element of sheet 1 of window 1
end tell
end if
end tell
Some of the places to save the file have keyboard shortcuts. For example the desktop has command-d. You an look in the drop-down menu to see the shortcuts. So if you keystroke that while the dialog box is open it will switch to the desktop folder as the location of where to save your file.
Thanks, Hank. That’s exactly what I needed. For the sake of completeness, here’s the final code.
-- Assumes a Keynote presentation is open and available for export
set file_name to "Test Presentation"
set finalPath to somePath -- where somePath is a valid path
tell application "System Events"
if (system attribute "sysv") < 4144 or UI elements enabled then
tell application process "Keynote"
set frontmost to true
click menu item "Export." of menu "File" of menu bar item "File" of menu bar 1
click button 3 of sheet 1 of window 1
click button "Next." of sheet 1 of window 1
keystroke file_name
-- Export to desktop first
keystroke "d" using command down
click button "Export" of sheet 1 of window 1
-- Wait for the export to complete
repeat until not (exists window "Export to PDF")
end repeat
end tell
end if
end tell
-- Move to the final destination, finalPath
tell application "Finder"
set fileName to (file_name & ".pdf")
set mydesktop to path to desktop folder as alias
set exportedPDFfile to document file fileName in mydesktop
move exportedPDFfile to ((finalPath as text) as alias)
end tell