Scripted Plot Updates in Keynote
Published:
Working in particle physics means I don’t often get the feeling of producing something genuinely useful for a broad (non-physicist) audience. Today I have the pleasure of sharing something dozens of people might find game changing for their workflow.
Here’s the infomercial: Has this ever happened to you?! You spent hours producing hundreds of plots and figures to share with your colleagues only to realize your preliminary plots all boldly proclaim that they are “Preluminary”. Now you have to go through every slide painstakingly dragging the typo corrected PDFs of your figures into the corresponding Keynote Image Placeholders. If only you could push a single button to tell Keynote to grab the updated file! Well today you can and I’m going to show you how!
There are two simple steps, first when you are making your Keynote presentation, each time you create an Image Placeholder, add a description containing the file path (formatted with colons rather than the usual slashes because that’s how apple’s file system works) to the directory where the image is located:
Next open Script Editor and paste the following code into a new AppleScript:
--- Based on a script to perform this task where the files are grabbed from URLs in the placeholder descriptions
--- https://iworkautomation.com/keynote/image-from-URL.html
tell application "Keynote"
activate
if not (exists front document) then error number -128
tell the front document
-- Loop over the slides in this presentation
repeat with i from 1 to the count of slides
tell slide i
-- loop over the images in the slide
repeat with q from 1 to the count of images
-- Get the current image file name and description
tell image q
set imageFileName to get file name
set currentDescription to the description
end tell
-- if the description is a file path, try updating the image
if currentDescription contains ":" then
-- try getting a new image with the same name at the path from the description
set newImageFile to my fileFromPath(currentDescription, imageFileName)
if newImageFile is not false then
-- update the image and keep the current description
set file name of image q to newImageFile
set description of image q to currentDescription
end if -- found a new image
end if -- description looks like a file path
end repeat -- images
end tell -- this slide
end repeat -- slides
end tell -- this document
end tell -- keynote
on fileFromPath(filePath, fileName)
try
-- return an alias file reference to the image file
set fullPath to (filePath & fileName)
return alias fullPath
on error
-- try adding a colon between the filePath and fileName
try
set fullPath to (filePath & ":" & fileName)
return alias fullPath
on error -- adding a colon didn't fix it
return false
end try
end try
end fileFromPath
When you run this script it will update all the images in Placeholders with a description containing a file path of the most recent Keynote presentation you had active.
As an optional additional step, you can create a custom Automator Quick Action workflow to run this script with the push of a Touch Bar button. Yes, this does mean I have found an actual use for the universally loathed Touch Bar. Side Note: I’m actually friends with the Touch Bar now, I quite like having Caps Lock remapped to Escape, the Do Not Disturb toggle, and using sliders to perfectly set my volume and display brightness.
Just open Automator and create a new Quick Action:
Add a “Run AppleScript” action and paste the code from above. Set the workflow to receive no input, the script will do all the work.
Export the workflow to the Desktop where you can double click on it to install it.
In System Preferences > Extensions > Touch Bar you can now enable your new Quick Action.
Now when you are working on a presentation and you want to update all your image Placeholders just open the Touch Bar Quick Action Menu and select your Update Images action.
If you don’t have a Touch Bar and want to make a Quick Action for the Finder, you could do so with some Googling and modify the script and workflow accordingly.