Export Keynote to PDF with Notes via GUI Scripting

Hi All,

I have the following script running through hazel on keynote files that I’m done with to export them as pdf for easier access. However, the export with notes button has been an issue for me. Do any of you know how to do that? I’d like to export the pdf’s through keynote with the notes.

Thanks in advance,

Maksim


-- Assumes a Keynote presentation is open and available for export
tell application "Keynote"
	open theFile
end tell

tell application "Finder"
	set finalPath to container of theFile as Unicode text -- where somePath is a valid path
	set file_name to name of theFile as Unicode text
	set _length to (count of file_name) - 4
	set file_name to text 1 thru _length of file_name
end tell

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

tell application "Keynote"
	quit
end tell

To activate the mode “embed notes”, we can’t use Export. We must use Print.



(*
keynote_to_pdf
*)

set theFile to (path to documents folder as text) & "tempo:beurk.key" as alias
my activateGUIscripting()

tell application "System Events" to tell disk item (theFile as text)
	set destinationFolder to path of container
	set fileName to name
end tell -- System Events
if fileName ends with ".key" then
	set nomFenetre to fileName
else
	set nomFenetre to fileName & ".key"
end if

tell application "Keynote"
	open theFile
	repeat until name of every slideshow contains nomFenetre
		--
	end repeat
end tell
set dossierParDefaut to my getDossierParDefaut()

set cheminPDFtemporaire to dossierParDefaut & nomFenetre & ".pdf"

tell application "System Events"
	if exists (disk item cheminPDFtemporaire) then set name of disk item cheminPDFtemporaire to nomFenetre & my horoDateur(modification date of file cheminPDFtemporaire) & ".PDF" (* name stamped *)
end tell

tell application "System Events" to tell application process "Keynote"
	set frontmost to true
	keystroke "p" using command down
	tell window nomFenetre
		repeat
			try
				if description of sheet 1 is "Print" then exit repeat
			end try
		end repeat
		tell sheet 1
			-- properties of every button
			(* --> {
(1) {minimum value:missing value, orientation:missing value, position:{835, 591}, class:button, role description:"bouton", accessibility description:missing value, focused:false, title:"Imprimer", size:{98, 32}, value:missing value, help:missing value, enabled:true, maximum value:missing value, role:"AXButton", entire contents:{}, subrole:missing value, selected:missing value, name:"Imprimer", description:"bouton"}, 
(2) {minimum value:missing value, orientation:missing value, position:{737, 591}, class:button, role description:"bouton", accessibility description:missing value, focused:false, title:"Annuler", size:{98, 32}, value:missing value, help:missing value, enabled:true, maximum value:missing value, role:"AXButton", entire contents:{}, subrole:missing value, selected:missing value, name:"Annuler", description:"bouton"}, 
(3) {minimum value:missing value, orientation:missing value, position:{49, 595}, class:button, role description:"bouton", accessibility description:"Aide", focused:false, title:missing value, size:{25, 25}, value:missing value, help:missing value, enabled:true, maximum value:missing value, role:"AXButton", entire contents:{}, subrole:missing value, selected:missing value, name:missing value, description:"Aide"}}
			*)
			-- properties of every menu button
			(* --> {
(1) {minimum value:missing value, orientation:missing value, position:{81, 593}, class:menu button, role description:"bouton de menu", accessibility description:missing value, focused:false, title:"PDF", size:{85, 26}, value:missing value, help:missing value, enabled:true, maximum value:missing value, role:"AXMenuButton", entire contents:{}, subrole:missing value, selected:missing value, name:"PDF", description:"bouton de menu"}}
*)
			-- properties of every radio group
			(* --> {
(1) {minimum value:missing value, orientation:missing value, position:{389, 355}, class:radio group, role description:"groupe de radio", accessibility description:missing value, focused:false, title:missing value, size:{219, 84}, help:missing value, enabled:true, maximum value:missing value, role:"AXRadioGroup", entire contents:{}, subrole:missing value, selected:missing value, name:missing value, description:"groupe de radio"}, 
(2) {minimum value:missing value, orientation:missing value, position:{555, 257}, class:radio group, role description:"groupe de radio", accessibility description:missing value, focused:false, title:missing value, size:{65, 41}, help:missing value, enabled:true, maximum value:missing value, role:"AXRadioGroup", entire contents:{}, subrole:missing value, selected:missing value, name:missing value, description:"groupe de radio"}}
*)
			tell first radio group
				-- properties of every UI element
				(*
--> {
(1) {minimum value:missing value, orientation:missing value, position:{389, 355}, class:radio button, role description:"bouton radio", accessibility description:missing value, focused:false, title:"Diapositives individuelles", size:{219, 18}, value:0, help:missing value, enabled:true, maximum value:missing value, role:"AXRadioButton", entire contents:{}, subrole:missing value, selected:missing value, name:"Diapositives individuelles", description:"bouton radio"}, 
(2) {minimum value:missing value, orientation:missing value, position:{389, 377}, class:radio button, role description:"bouton radio", accessibility description:missing value, focused:false, title:"Diapositives avec notes", size:{219, 18}, value:1, help:missing value, enabled:true, maximum value:missing value, role:"AXRadioButton", entire contents:{}, subrole:missing value, selected:missing value, name:"Diapositives avec notes", description:"bouton radio"}, 
(3) {minimum value:missing value, orientation:missing value, position:{389, 399}, class:radio button, role description:"bouton radio", accessibility description:missing value, focused:false, title:"Structure", size:{219, 18}, value:0, help:missing value, enabled:true, maximum value:missing value, role:"AXRadioButton", entire contents:{}, subrole:missing value, selected:missing value, name:"Structure", description:"bouton radio"}, 
(4) {minimum value:missing value, orientation:missing value, position:{389, 421}, class:radio button, role description:"bouton radio", accessibility description:missing value, focused:false, title:"Document", size:{219, 18}, value:0, help:missing value, enabled:true, maximum value:missing value, role:"AXRadioButton", entire contents:{}, subrole:missing value, selected:missing value, name:"Document", description:"bouton radio"}}
				*)
				tell radio button 2
					if value is 0 then click (* Active "Diapositives avec notes" *)
				end tell -- radio button
			end tell -- first radio group
			click menu button 1 (* PDF *)
			-- name of every menu item of menu 1 of menu button 1
			(* --> {
"Ouvrir le PDF dans Aperçu",
"Enregistrer au format PDF.",
"Enregistrer au format PostScript.",
"Faxer le document PDF.",
missing value,
"@ PDF-BAT.qfilter",
"@ PDF-prépresse CMJN.qfilter",
"@ PDF-web.qfilter",
"@ PDFX3-ISO.qfilter",
"Envoyer le document PDF par courrier électronique",
"Enregistrer en PDF-X",
"Enregistrer le document PDF vers iPhoto",
"Enregistrer le document PDF dans le dossier de reçus Web",
missing value,
"Modifier le menu."
} *)
			click menu item 2 of menu 1 of menu button 1 (* click item Save as PDF. *)
			keystroke return (* idem click OK *)
		end tell -- sheet 1
	end tell -- window.	
end tell -- System Events.

if fileName ends with ".key" then set fileName to text 1 thru -5 of fileName
set nomFinal to fileName & ".pdf"
set cheminFinal to destinationFolder & nomFinal
tell application "System Events"
	repeat until exists disk item cheminPDFtemporaire
		delay 1
	end repeat
	if exists (disk item cheminFinal) then set name of disk item cheminFinal to fileName & my horoDateur(modification date of file cheminFinal) & ".PDF" (* name stamped *)
	make new file at end of folder destinationFolder with properties {name:nomFinal}
	repeat until exists disk item cheminFinal
		--
	end repeat
end tell -- System Events

if destinationFolder is dossierParDefaut then
	tell application "System Events"
		set name of disk item cheminPDFtemporaire to nomFinal
		delete disk item cheminPDFtemporaire
	end tell
else
	do shell script "mv " & (quoted form of POSIX path of cheminPDFtemporaire) & " " & quoted form of POSIX path of cheminFinal
end if -- destinationFolder.

tell application "Keynote" to quit

--=====
(* Build a stamp from the modification date_time
*)
on horoDateur(date_de_modification)
	local les_secondes
	set les_secondes to time of date_de_modification
	return "_" & text -2 thru -1 of ("0" & (month of date_de_modification as integer)) & text -2 thru -1 of ("0" & day of date_de_modification) & "_" & text -2 thru -1 of ("0" & les_secondes div 3600) & text -2 thru -1 of ("0" & (les_secondes mod 3600) div 60) & text -2 thru -1 of ("0" & les_secondes mod 60)
	(* 
¢ Here, the stamp is  "_YYYYMMDD_hhmmss" *)
end horoDateur

--=====

on getPlistValue(valName, default)
	local thePlist, u
	set thePlist to (path to preferences folder as Unicode text) & "com.apple.iWork.Keynote.plist"
	tell application "System Events"
		if exists file thePlist then
			tell contents of property list file thePlist
				try
					set u to (value of property list item valName) (* Unicode Text *)
				on error (*
On est là si AppleWorks n'a rien enregistré avec des préférences neuves
¢ Here if AppleWorks never saved with the new preferences file. *)
					set u to default
				end try
			end tell -- to contents of.
		else (*
On est là s'il n'y a pas de fichier de préférences
¢ Here if there is no preferences file. *)
			set u to default
		end if
	end tell -- to system events
	return u
end getPlistValue

--=====

on getDossierParDefaut()
	local u
	try
		set u to my getPlistValue("NSNavLastRootDirectory", "~/Documents")
		set u to "" & POSIX file (do shell script "echo " & u)
		if u ends with ":" then
			return u
		else
			return (u & ":")
		end if
	on error
		return ("" & (path to documents folder))
	end try
end getDossierParDefaut

--=====

on activateGUIscripting()
	(* to be sure than GUI scripting will be active *)
	tell application "System Events"
		if not (UI elements enabled) then set (UI elements enabled) to true
	end tell
end activateGUIscripting

--=====

Yvan KOENIG (VALLAURIS, France) jeudi 15 septembre 2011 23:17:13