You can make a difference in the Apple Support Community!

When you sign up with your Apple Account, you can provide valuable feedback to other community members by upvoting helpful replies and User Tips.

Keynote automation with AppleScript

I am automating some process with AppleScript and Python. But I got stuck to automate printing 1 slide per page and exporting it to a PDF. I can print 3 slides per page and export it to a PDF file, but not one slide. Can anyone help me with this? This is my code so far:

tell application "Keynote"

activate

open POSIX file "{keynote_file}"

delay 5 -- Allow more time for the document to open fully

if (exists front document) then

tell application "System Events"

tell process "Keynote"

set frontmost to true

repeat until window 1 exists

delay 0.5

end repeat


click menu item "First Slide" of menu of menu item "Go To" of menu "Slide" of menu bar 1

delay 3


keystroke "p" using {{command down}}

delay 5 -- Longer delay for print dialog to open


repeat until sheet 1 of window 1 exists

delay 1 -- Ensure the print dialog has fully appeared

end repeat


click checkbox 3 of group 1 of group 2 of scroll area 2 of splitter group 1 of sheet 1 of window 1 -- HANDOUT

delay 1

click checkbox 1 of group 1 of group 1 of group 2 of scroll area 2 of splitter group 1 of sheet 1 of window 1 -- INCLUDE PRESENTER NOTES

delay 1

click menu button 1 of group 2 of splitter group 1 of sheet 1 of window 1

delay 1

click menu item 0 of menu 1 of menu button 1 of group 2 of splitter group 1 of sheet 1 of window 1 -- Try index 0 for "1 slide per page"

-- Save to the desired folder

repeat until exists sheet 1 of sheet 1 of window 1

delay 1 -- Make sure the "Save as" dialog is ready

end repeat

keystroke "g" using {{shift down, command down}}

delay 2

keystroke "{dest_keynote_folder}"

delay 2

keystroke return

delay 2


keystroke "{export_filename}"

delay 2


click button 4 of sheet 1 of sheet 1 of window 1 -- save button

delay 5

end tell

end tell

delay 5 -- Give time for the file to save

save front document

close front document

end if

end tell


MacBook Air 11″, macOS 10.15

Posted on Oct 17, 2024 8:13 PM

Reply
1 reply

Oct 21, 2024 10:08 AM in response to michiko257

Quite simply, you're doing it wrong :)


UI Scripting (where you write a script to replicate manual actions) is rarely the best way to leverage AppleScript. In my opinion, it's always a last resort.


Instead, Keynote has specific AppleScript commands to export a presentation to PDF (it's literally called export), so use that.


The only gotcha in your ask is separate PDF files per slide, which you can achieve by manipulating the skipped flag on each slide - you can export just the non-skipped slides in the presentation, it's just a matter of setting which slide(s) are skipped/not skipped as you go through.


Here's an example:


use AppleScript version "2.4" -- Yosemite (10.10) or later
use scripting additions

tell application "Keynote"
	-- how many slides do we have?
	set slideCount to count slides of document 1
	-- mark them as as skipped
	set skipped of every slide of document 1 to true
	
	-- iterate through the slides
	repeat with slideNum from 1 to slideCount
		-- determine the file path for this slide (amend as needed)
		set filepath to (path to desktop as text) & "Slide " & slideNum & ".pdf"
		-- change the current slide to not-skipped
		set skipped of slide slideNum of document 1 to false
		-- export the presentation, skipping skipped slides
		export document 1 as PDF to (file filepath) with properties {skipped slides:false}
		-- reset this slide's skipped value
		set skipped of slide slideNum of document 1 to true
	end repeat
	-- re-enable all the slides
	set skipped of every slide of document 1 to false
end tell


Now this will export every slide, so if your presentation includes normally-skipped slides they would still be exported. If that's a problem, a simple change to the script can accommodate, but it's extra work that may not be normally needed, so I skipped it (pun intended :) )

Keynote automation with AppleScript

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.