Keynote - Movie Export Droplet with ProRes 4444 @ 1920 x 1080

My iMac is unable to run the late version o Keynote but I will try to help you step by step.

Here is a point of departure which is supposed to be executed with a Keynote document open.

tell application "Keynote"
	set exportTo_loc to localized string "1543.title" from table "MainMenu" --> "Exporter vers"
end tell

set {mt, mi, ms} to {3, 14, 3}
tell application id "com.apple.systemevents" to tell process "Keynote"
	set frontmost to true
	tell menu bar 1
		get name of menu bar items --> {"Apple", "Keynote", "Fichier", "Édition", "Insérer", "Diapositive", "Format", "Disposition", "Présentation", "Lecture", "Partager", "Fenêtre", "Aide"}
		get name of menu bar item mt --> "Fichier"-- {"Tableau"}
		tell menu bar item mt to tell menu 1
			get name of menu items --> {"Nouveau", "Créer à partir de la liste de thèmes…", "Ouvrir…", "Ouvrir un document récent", missing value, "Fermer", "Tout fermer", "Enregistrer", "Enregistrer sous…", "Dupliquer", "Renommer…", "Déplacer vers…", "Revenir à", "Exporter vers", missing value, "Réduire la taille du fichier…", "Avancé", missing value, "Définir un mot de passe…", missing value, "Modifier le thème…", "Enregistrer le thème…", missing value, "Imprimer…"}
			get name of menu item mi --> "Exporter vers"
			tell menu item mi to tell menu 1
				get name of menu items --> {"PDF…", "PowerPoint…", "Vidéo…", "GIF animé…", "Images…", "HTML…", "Keynote ’09…"}
				get name of menu item ms --> "Vidéo…"
				click menu item ms
			end tell -- menu item…
		end tell -- menu bar item mt 
	end tell -- menu bar 1
	(*
	tell window 1
		class of UI elements --> {radio group, checkbox, static text, scroll area, scroll area, scroll area, button, button, button, menu button, toolbar, image, static text, sheet}
		tell sheet 1
			class of UI elements --> {static text, list, static text, pop up button, static text, text field, static text, static text, text field, static text, static text, static text, text field, static text, text field, radio button, radio button, static text, pop up button, static text, button, button, button}
		end tell
	end tell
	*)
end tell -- System Events

What is returned by the instruction
get name of menu items → {“PDF…”, “PowerPoint…”, “Vidéo…”, “GIF animé…”, “Images…”, “HTML…”, “Keynote ’09…”}
I assume that it return different strings. Which are there ?

Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) mercredi 5 février 2020 12:13:18

BitCy.

Just to explain that what you want isn’t possible using Keynote’s own AppleScript implementation. The only hope is to use “GUI Scripting”, ie. faking the clicking of menu items and buttons in the application as it appears on screen. This is a bit of a hit-or-miss process as an application’s layout and text content can vary from version to version, system to system, and user preference to user preference.

Yvan’s script is simply a test to find out what’s in your menus when running in English. On my machine (running Keynote 9.2.1 in Mojave), it also opens the “Export Your Presentation” sheet with the “Movie” pane selected. If you run it with a Keynote document open and look in Script Editor’s “Replies” pane, you should see something like the following, which could help Yvan to develop a script for you:

tell application "Keynote"
	localized string "1543.title" from table "MainMenu"
		--> error number -1708
	«event ascrgdut»
		--> error number -1708
	localized string "1543.title" from table "MainMenu"
		--> "Export To"
end tell
tell application "System Events"
	set frontmost of process "Keynote" to true
	get name of every menu bar item of menu bar 1 of process "Keynote"
		--> {"Apple", "Keynote", "File", "Edit", "Insert", "Slide", "Format", "Arrange", "View", "Play", "Share", "Window", "Help"}
	get name of menu bar item 3 of menu bar 1 of process "Keynote"
		--> "File"
	get name of every menu item of menu 1 of menu bar item 3 of menu bar 1 of process "Keynote"
		--> {"New…", "New…", "Open…", "Open Recent", missing value, "Close", "Close All", "Save…", "Save As…", "Duplicate", "Rename…", "Move To…", "Revert To", "Export To", missing value, "Reduce File Size…", "Advanced", missing value, "Set Password…", missing value, "Change Theme...", "Save Theme…", missing value, "Print…"}
	get name of menu item 14 of menu 1 of menu bar item 3 of menu bar 1 of process "Keynote"
		--> "Export To"
	get name of every menu item of menu 1 of menu item 14 of menu 1 of menu bar item 3 of menu bar 1 of process "Keynote"
		--> {"PDF…", "PowerPoint…", "Movie…", "Animated GIF…", "Images…", "HTML…", "Keynote ’09…"}
	get name of menu item 3 of menu 1 of menu item 14 of menu 1 of menu bar item 3 of menu bar 1 of process "Keynote"
		--> "Movie…"
	click menu item 3 of menu 1 of menu item 14 of menu 1 of menu bar item 3 of menu bar 1 of process "Keynote"
		--> menu item "Movie…" of menu "Export To" of menu item "Export To" of menu "File" of menu bar item "File" of menu bar 1 of application process "Keynote"
end tell
Result:
menu item "Movie…" of menu "Export To" of menu item "Export To" of menu "File" of menu bar item "File" of menu bar 1 of application process "Keynote" of application "System Events"

Thank you Nigel. You gave useful infos.

Here is where I am.

(*
-- code dropped because it doesn't apply when the app is used in French.
Apple forgot to insert the file: "Keynote.app:Contents:Resources:fr.lproj:MainMenu.strings". They put a MainMenu.nib file.
Happily, there is a clone available as :
"Keynote.app:Contents:Resources:fr_CA.lproj:MainMenu.strings"
On my copy I duplicated the file so now the code works.
Maybe the file is available in versions newer than 9.1
Same odd omission in Pages and Numbers
As the numbering of menu items doesn't change since several years, I choose to trigger the menu items by their index.

tell application "Keynote"
	set exportTo_loc to localized string "1543.title" from table "MainMenu" --> "Exporter vers"
end tell
*)

set indexFile to 3 -- index of menu "File"   ("Fichier")
set indexExportTo to 14 -- index of menu item "Export To"   ("Exporter vers")
set indexMovie to 3 -- index of menu item "Movie…"   ("Vidéo…")

tell application id "com.apple.systemevents" to tell process "Keynote"
	set frontmost to true
	tell menu bar 1
		-- get name of menu bar items --> {"Apple", "Keynote", "Fichier", "Édition", "Insérer", "Diapositive", "Format", "Disposition", "Présentation", "Lecture", "Partager", "Fenêtre", "Aide"}
		-- get name of menu bar item indexFile --> "Fichier"
		tell menu bar item indexFile to tell menu 1
			-- get name of menu items --> {"Nouveau", "Créer à partir de la liste de thèmes…", "Ouvrir…", "Ouvrir un document récent", missing value, "Fermer", "Tout fermer", "Enregistrer", "Enregistrer sous…", "Dupliquer", "Renommer…", "Déplacer vers…", "Revenir à", "Exporter vers", missing value, "Réduire la taille du fichier…", "Avancé", missing value, "Définir un mot de passe…", missing value, "Modifier le thème…", "Enregistrer le thème…", missing value, "Imprimer…"}
			-- get name of menu item indexExportTo --> "Exporter vers"
			tell menu item indexExportTo to tell menu 1
				-- get name of menu items --> {"PDF…", "PowerPoint…", "Vidéo…", "GIF animé…", "Images…", "HTML…", "Keynote ’09…"}
				-- get name of menu item indexMovie --> "Vidéo…"
				click menu item indexMovie
			end tell -- menu item indexMovie
		end tell -- menu bar item indexFile 
	end tell -- menu bar 1
	-- class of UI elements
	tell (first window whose subrole is "AXStandardWindow") -- EDITED
		
		class of UI elements --> {radio group, checkbox, static text, scroll area, scroll area, scroll area, button, button, button, menu button, toolbar, image, static text, sheet}
		
		
		tell sheet 1
			-- class of UI elements --> {static text, list, static text, pop up button, static text, text field, static text, static text, text field, static text, static text, static text, text field, static text, text field, radio button, radio button, static text, pop up button, static text, button, button, button}
			-- help of text fields --> {"Saisissez le nombre de secondes d’attente entre chaque diapositive.", "Saisissez le nombre de secondes d’attente entre chaque composition.", missing value, missing value} -- ADDED
			set value of text field 1 to "10" -- must be a string -- ADDED
			set value of text field 2 to "8" -- must be a string -- ADDED
			
			-- help of pop up button 1 --> "Choisissez le mode de lecture de la vidéo."
			-- help of pop up button 2 --> "Choisissez une taille pour la vidéo."
			tell pop up button 2
				its value --> "1024 x 768"
				click it
				repeat 50 times
					if exists menu 1 then exit repeat
					delay 0.2
				end repeat
				tell menu 1
					-- class of UI elements --> {menu item, menu item, menu item, menu item}
					-- name of menu items --> {"1024 x 768", "720p", "1080p", "Personnaliser…"}
					click menu item -1 -- Custom
				end tell
			end tell
			set checkBoxAvailable to (class of UI elements) contains checkbox --> {static text, list, static text, pop up button, static text, text field, static text, static text, text field, static text, static text, static text, text field, static text, text field, radio button, radio button, static text, pop up button, text field, static text, text field, static text, static text, checkbox, radio button, radio button, radio button, button, button, button}
			
			-- help of text field -2 --> "Saisissez la largeur voulue pour la vidéo."
			-- help of text field -1 --> "Saisissez la hauteur voulue pour la vidéo."
			-- value of text field -2 --> "1024"
			-- value of text field -1 --> "768"
			set value of text field -2 to "1920" -- must be a string
			set value of text field -1 to "1080" -- must be a string
			
			-- name of radio button -3 --> "Apple ProRes 422"
			-- name of radio button -2 --> "H.264"
			-- name of radio button -1 --> "Apple ProRes 4444"
			click radio button -1
			if checkBoxAvailable then
				-- name of checkbox 1 --> "Exporter avec des arrière-plans transparents"
				value of checkbox 1 --> 1
				
				-- set value of checkbox 1 to 1 -- enable it to check the box
				-- set value of checkbox 1 to 0 -- enable it to uncheck the box
			end if
			
		end tell -- sheet 1
		
	end tell
end tell -- System Events

As is, the script selects the Custom… format
then it define the width (1920) and the height (1080) of the window
and select the compression Apple ProRes 4444.
It lets the checkbox linked to transparency as is but it contain the instructions required to edit the setting.

Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) mercredi 5 février 2020 16:58:08

Thank you, Yvan Koenig and Nigel Garvey!!! Wow! It is brilliant.:smiley:

I have made Quick Action from Automator using this script. Works flawlessly!

Thank you again, and really appreciate!

Hello Yvan Koenig,

I am trying to update and improve this script but I am lost.

How can I add this two fields as well? “Go to next slide after:” and “Go to next build after:”, Say if I want to always make that at 1 sec.

Thank you, Yvan Koenig. :slight_smile:

I added the 3 needed instructions in the script given in message #5.

Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) mardi 17 mars 2020 08:11:52

Thank you Yvan! Really Appreciate it!

I have one final question. Sometimes or rather most of the times in Keynote, I will have multiple floating windows open. Like Colour Picker, Build Order, and Font List. I notices that this script will not work when floating windows from Keynote are open.

Here is the error message.

How can we fix this?

Thank you, Yvan!

I was upon that when I saw your question.

replace the instruction

tell window 1

by

tell (first window whose subrole is "AXStandardWindow")

message #5 is edited accordingly.

Is it useful to add a test issuing a warning if the radio buttons related to compression aren’t available ?

Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) mardi 17 mars 2020 11:49:03

Thank you for the fix Yvan! Awesome and really appreciate it!

I don’t think it would be useful for the warning as the radio buttons related to compression will always be available since we are in Custom mode.

Thanks for the help, Yvan! :slight_smile:

Hello Yvan,

I notice a bug and an issue in the script. I am not really sure what is causing it.

When I set both field to 1 and 0, and each time I export the keynote, the video file gets much longer. I have to manually enter 1 and 0 in the keynote movie export to export it out correctly.


			set value of text field 1 to "1" -- must be a string -- ADDED
			set value of text field 2 to "0" -- must be a string -- ADDED

I am wondering, what is causing the issue? Is there a way to fix this?

Thank you.

The disease of many experienced programmers is that they cannot convey the essence to inexperienced in a simple way. I hope most like the English interface, just like me. Although I perfectly know Russian, Greek, Georgian.

For example, if you use a simple programming language, then the above scripts can be reduced to this. Just compare with your monsters:


tell application id "com.apple.systemevents" to tell process "Keynote"
	
	set frontmost to true
	tell menu bar 1 to tell menu bar item "File" to tell menu 1
		tell menu item "Export To" to tell menu 1 to click menu item "Movie…"
	end tell
	
	delay 0.1
	tell window 1 to tell sheet 1
		set value of 1st text field whose help is "Type how many seconds to wait between slides." to "10"
		set value of 1st text field whose help is "Type how many seconds to wait between builds." to "8"
		
		tell (1st pop up button whose help is "Choose a size for the movie.")
			perform action "AXShowMenu"
			delay 0.1
			click menu item "Custom..." of menu 1
		end tell
		set value of 1st text field whose help is "Type the width you want for the movie." to "1920"
		set value of 1st text field whose help is "Type the height you want for the movie." to "1080"
		
		click (1st radio button whose title is "Apple ProRes 4444")
		delay 0.1
		click button "Next…"
	end tell
	
end tell

Here is your original script without GUI scripting. I fixed in it 2 errors and it works now.

  1. For the movie export format you have 4 constants: small, medium, large, native size. They respond to 360p, 540p, 720p, native size. You should use always KeyNote’s constant (for exapmple, instead of 720p you should use large. The compiler automatically changes it to italic 720p, but now it is what should be. (That is, it is other, proper 720p). If you do not make changes in the script in future, it remains correct. If you change the script, always put large again, to fix the issue again. This was first error.

  2. Correct UTI of keyNote document is “com.apple.iwork.keynote.sffkey” and not “com.apple.iwork.keynote.key”. This was second error.

  3. Filtering process is simpler (see here). Handler checkForIdentifier no need at all.

  4. No need Posix files, Posix paths and do shell scripts…

Your fixed script (I use native size, you can use small, medium, large instead):


property destinationFolder : (path to desktop folder)

on run
	-- TRIGGERED WHEN USER LAUNCHES DROPLET. PROMPT FOR PRESENTATION FILE(S):
	set theItems to (choose file of type "com.apple.iwork.keynote.sffkey" with prompt "Pick the Keynote presentation(s) to export to movie:" with multiple selections allowed)
	open theItems
end run

on open theItems
	-- TRIGGERED WHEN USER DRAGS ITEMS ONTO THE DROPLET
	display dialog "This droplet will export dragged-on Keynote presentation files as movies" & return & return & "The movies will be encoded to MPEG format using H.264 compression." with icon 1
	set filesToProcess to {}
	-- filter the dragged-on items for Keynote presentation files
	repeat with anItem in theItems
		tell application id "com.apple.systemevents" to set anUTI to type identifier of anItem
		if anUTI is "com.apple.iwork.keynote.sffkey" then set the end of the filesToProcess to anItem
	end repeat
	if filesToProcess is {} then
		activate
		display alert "INCOMPATIBLE ITEMS" message "None of the items were Keynote presentations."
	else
		-- process the presentations
		my exportPresentationsToMovies(filesToProcess)
	end if
end open

on exportPresentationsToMovies(thePresentations)
	tell application "Keynote"
		activate
		if playing is true then tell front document to stop
		try
			repeat with aPresentation in thePresentations
				open aPresentation
				set the documentName to name of the front document
				set destinationFile to my deriveFileNameForNewFileInFolder(documentName, destinationFolder)
				with timeout of 1200 seconds -- 20 minutes
					export front document to file destinationFile as QuickTime movie with properties {movie format:native size}
				end timeout
				close front document saving no
				display notification documentName with title "Keynote Movie Export"
			end repeat
		on error errorMessage
			activate
			display alert "EXPORT ERROR" message errorMessage
			error number -128
		end try
	end tell
end exportPresentationsToMovies

on deriveFileNameForNewFileInFolder(sourceItemBaseName, targetFolderHFSAlias)
	-- Routine that derives a none-conflicting file name
	set targetName to ((targetFolderHFSAlias & sourceItemBaseName) as text) & ".m4v"
	try
		alias targetName
	on error
		return targetName
	end try
	set n to 1
	repeat
		set targetName to ((targetFolderHFSAlias & sourceItemBaseName) as text) & "(" & n & ").m4v"
		try
			alias targetName
			set n to n + 1
		on error
			return targetName
		end try
	end repeat
end deriveFileNameForNewFileInFolder

I apologizes but you are wrong.

On my machine, keynote documents are given the UTI “com.apple.iwork.keynote.key”.
Look at the file :
“Keynote.app:Contents:Info.plist”

You will learn that
Keynote Presentation document may be :
com.apple.iwork.keynote.key – package
com.apple.iwork.keynote.sffkey – flat file
com.apple.iwork.keynote.key-tef – package
com.apple.iwork.keynote.kpf – package

You may run the script below to check that.

set theApp to path to application id "com.apple.iWork.Keynote"
set thePlist to ((theApp as text) & "Contents:Info.plist") as «class furl»
set answer to choose from list {"Xcode", "BBEdit"} with title "Choose the application to open the plist" with prompt "Xcode: look at the properties “Document types” " & linefeed & "BBEdit: look at keys “UTTypeConformsTo”"
if answer is false then error number -128
set opener to item 1 of answer as text
tell application opener to open thePlist

On my side I never see com.apple.iwork.keynote.kpf items
and my memory tell me that com.apple.iwork.keynote.key-tef are grabbed from docs generated under iOS (but it may be a wrong souvenir).

Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) mercredi 25 mars 2020 10:55:39

Thanks for the info. I have only flat file presentations on my Mac. So, I can’t test the last script with other UTIs. If it works, then we can provide the list of UTIs in choose file dialog, and we can check if the file’s identifier is in the UTIs list in the on open handler.

When I posted a code asking for com.apple.iwork.keynote.key UTI, it’s what I had as files available here.
I knew the com.apple.iwork.keynote.sffkey one but I forgot to insert it in the list passed to choose file.
Below is your completed script


property destinationFolder : (path to desktop folder)
property permittedUTIs : {"com.apple.iwork.keynote.key", "com.apple.iwork.keynote.sffkey", "com.apple.iwork.keynote.key-tef", "com.apple.iwork.keynote.kpf"} -- ADDED

on run
	-- TRIGGERED WHEN USER LAUNCHES DROPLET. PROMPT FOR PRESENTATION FILE(S):
	set theItems to (choose file of type permittedUTIs with prompt "Pick the Keynote presentation(s) to export to movie:" with multiple selections allowed) -- EDITED
	open theItems
end run

on open theItems
	-- TRIGGERED WHEN USER DRAGS ITEMS ONTO THE DROPLET
	display dialog "This droplet will export dragged-on Keynote presentation files as movies" & return & return & "The movies will be encoded to MPEG format using H.264 compression." with icon 1
	set filesToProcess to {}
	-- filter the dragged-on items for Keynote presentation files
	repeat with anItem in theItems
		tell application id "com.apple.systemevents" to set anUTI to type identifier of anItem
		if anUTI is in permittedUTIs then set the end of the filesToProcess to anItem -- EDITED
	end repeat
	if filesToProcess is {} then
		activate
		display alert "INCOMPATIBLE ITEMS" message "None of the items were Keynote presentations."
	else
		-- process the presentations
		my exportPresentationsToMovies(filesToProcess)
	end if
end open

on exportPresentationsToMovies(thePresentations)
	tell application "Keynote"
		activate
		if playing is true then tell front document to stop
		try
			repeat with aPresentation in thePresentations
				open aPresentation
				set the documentName to name of the front document
				set destinationFile to my deriveFileNameForNewFileInFolder(documentName, destinationFolder)
				with timeout of 1200 seconds -- 20 minutes
					export front document to file destinationFile as QuickTime movie with properties {movie format:native size}
				end timeout
				close front document saving no
				display notification documentName with title "Keynote Movie Export"
			end repeat
		on error errorMessage
			activate
			display alert "EXPORT ERROR" message errorMessage
			error number -128
		end try
	end tell
end exportPresentationsToMovies

on deriveFileNameForNewFileInFolder(sourceItemBaseName, targetFolderHFSAlias)
	-- Routine that derives a none-conflicting file name
	set targetName to ((targetFolderHFSAlias & sourceItemBaseName) as text) & ".m4v"
	try
		alias targetName
	on error
		return targetName
	end try
	set n to 1
	repeat
		set targetName to ((targetFolderHFSAlias & sourceItemBaseName) as text) & "(" & n & ").m4v"
		try
			alias targetName
			set n to n + 1
		on error
			return targetName
		end try
	end repeat
end deriveFileNameForNewFileInFolder

I applied it upon a “com.apple.iwork.keynote.key” file.

VLC executed the created m4v file without problem.

Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) mercredi 25 mars 2020 11:40:25

Cool :slight_smile:

It would be fine to get feedback from users having documents with UTI com.apple.iwork.keynote.key-tef or com.apple.iwork.keynote.kpf

This way, helpers like us would be aware of behaviors which they can’t test.

Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) mercredi 25 mars 2020 13:05:27

Hello Yvan!

Any fix on this? I got mixed up with the code from KniazidisR.

Thank you.

How many slides has your presentation? I tested with 4 slides using my GUI script. The duration of created movie was 20 seconds. When I set values manually it creates movie with duration = 4 seconds. Indeed, it is very strange. It seems to me that setting to 1 second doesn’t work, and remains default value = 5 seconds per slide!!!

The solution founded.

Mm, yes. I barely figured it out … It turns out that you first need to focus the text field. Otherwise, Keynote ignores the new values:


set movieName to "myNewKeynoteMovie"

tell application id "com.apple.systemevents" to tell process "Keynote"
	
	set frontmost to true
	tell menu bar 1 to tell menu bar item "File" to tell menu 1
		tell menu item "Export To" to tell menu 1 to click menu item "Movie…"
	end tell
	
	delay 0.1
	tell window 1 to tell sheet 1
		
		tell (1st text field whose help is "Type how many seconds to wait between slides.")
			set focused to true -- ADDED
			set value to "1"
		end tell
		tell (1st text field whose help is "Type how many seconds to wait between builds.")
			set focused to true -- ADDED
			set value to "0"
		end tell
		delay 1
		
		tell (1st pop up button whose help is "Choose a size for the movie.")
			perform action "AXShowMenu"
			delay 0.1
			click menu item "Custom..." of menu 1
		end tell
		set value of 1st text field whose help is "Type the width you want for the movie." to "1920"
		set value of 1st text field whose help is "Type the height you want for the movie." to "1080"
		
		click (1st radio button whose title is "Apple ProRes 4444")
		delay 0.1
		click button "Next…"
	end tell
	
	delay 1
	click text field 1 of sheet 1 of window 1
	keystroke movieName & return -- naming the movie
	delay 1
	repeat while sheet 1 of window 1 exists -- show creating movie progress
		delay 0.1
	end repeat
	
end tell