Lately I've been using Apple Keynote to create graphics for using in videos and blog posts. It's a quick way to arrange things on a page, copying and pasting most things just works, and there are enough built in shapes and tools to get the point across. However, after spending a full day creating graphics for a video, I found myself frustrated by the number of clicks required to export a single slide at a time.
I posted a quick note about this, and immediately got a lot of helpful replies! A lot of people suggested it should be possible to automate with KeyboardMaestro, a tool I was not previously familiar with. It was also suggested that I ask on the KeyboardMaestro forums, so I did, and also quickly received a very detailed reply!
In the course of reading through these replies, I realized that the thing they all had in common was AppleScript! One reply in particular from Chris contained a pretty detailed AppleScript that appears to have been adapted from the iworkautomation website. So I took that as a starting point and wanted to see how I could make it work without also using KeyboardMaestro.
Create a Quick Action in Automator
First, open Automator and create a new Quick Action.
In the new action that is created, set the "Workflow receives" option to no input, and choose Keynote.app in the application list.
Then search for the AppleScript action and drag it into the empty grey area.
Next, replace the (* Your script goes here *)
text with the following AppleScript.
AppleScript to export the current slide as a PNG in Keynote
set the defaultDestinationFolder to (path to downloads folder)
tell application "Keynote"
activate
try
set currentSlideNumber to the slide number of (get the current slide of the front document)
--- skip all but the current slide
tell the front document
set skipped of ¬
(every slide where the slide number is not equal to currentSlideNumber) to true
end tell
--- capture the document name
set documentName to the name of the front document
if documentName ends with ".key" then ¬
set documentName to text 1 thru -5 of documentName
--- create an empty folder to temporarily store the image
tell application "Finder"
set newFolderName to documentName & " slide " & currentSlideNumber
set incrementIndex to 1
repeat until not (exists folder newFolderName of defaultDestinationFolder)
set newFolderName to documentName & "-" & (incrementIndex as string)
set incrementIndex to incrementIndex + 1
end repeat
set the targetFolder to ¬
make new folder at defaultDestinationFolder with properties ¬
{name:newFolderName}
set the targetFolderHFSPath to targetFolder as string
end tell
--- tell Keynote to export the current slide
export the front document as slide images to file targetFolderHFSPath with properties ¬
{image format:PNG, skipped slides:false}
--- set all the slides back to unskipped
tell the front document
set skipped of every slide to false
end tell
--- move the file to the destination folder and delete the temporary folder
tell application "Finder"
set folderContents to every item of targetFolder
repeat with theFile in folderContents
set fileExtension to name extension of theFile
set fileName to the name of theFile as string
set splitCharacters to ".001"
set ATID to AppleScript's text item delimiters
set AppleScript's text item delimiters to splitCharacters
set newFileName to text item 1 of fileName
set AppleScript's text item delimiters to ATID
set newFileName to newFileName & "." & fileExtension
set name of theFile to newFileName
-- display dialog quoted form of (name of theFile as string)
end repeat
move files of entire contents of targetFolder to defaultDestinationFolder with replacing
delete targetFolder
--- open the destination folder
tell application "Finder"
set thePath to file (defaultDestinationFolder & newFileName as string)
activate
reveal the thePath
end tell
end tell
on error errorMessage number errorNumber
display alert "EXPORT PROBLEM" message errorMessage
error number -128
end try
end tell
Setting up a keyboard shortcut
In the Automator app, save this quick action with a name like ExportCurrentSlide
. You can quit Automator now.
Now open System Preferences, open the Keyboard options, and go to the Shortcuts tab, then click on Services. Your new action should appear at the bottom of the list.
Click on the "none" at the right side of the action and you can record a keyboard shortcut for the action. I used "Shift Command 9" for mine.
Close this, and go to Keynote to test it out!
Choose the slide you want to export and press your keyboard shortcut! You should see a quick sequence of hiding all the slides, un-hiding them, and then the Downloads folder should pop up with your file name including the Keynote file name and slide number!
There you go! A pure AppleScript solution, no third party apps needed! I just finished setting this up this morning and I'm already so much happier exporting slides now!