Mavericks: Saving PDF from Pages 09

This code, which worked in OS X 10.8, will result in a never ending dialog in Pages trying to export the PDF

set pathToSave to (path to desktop folder as string) & "Name.pdf"

tell application "Pages"
	save document 1 in file pathToSave
end tell

To solve this problem, I had to add “as string” to the path as shown here:

set pathToSave to (path to desktop folder as string) & "Name.pdf" as string

tell application "Pages"
	save document 1 in file pathToSave
end tell

Apparently this doesn’t fix it always, like in a loop when there are more than one document to save or just randomly. Any advice would be helpful…

When I try to run your script, I get this message :

Can’t move « Name.pdf.pages » because you don’t have the permissions required to access « Desktop »

(quick and dirty translation from French).

It appears that contrarily to Pages 4.3, Pages 5.0 requires the as parameter to export.

Alas, I didn’t found the correct syntax.

I tried

as “SLDocumentTypePDF”
as “PDF”
as PDF

all failed.

So, I will move back to the good old GUIScripting scheme.
Stay tuned.

Yvan KOENIG (VALLAURIS, France) vendredi 1 novembre 2013 11:05:36

(*
PagesOld2PDF
Cumbersome code to be sure that we use the old application,
which is "/Applications/iWork '09/Pages.app".
In this script grabbing the process ID wasn't required but the skeleton was grabbed from a script in which it was.
*)
tell application "/Applications/iWork '09/Pages.app"
	activate
	if not version < "5" then error "You aren't running Pages version 4.x"
end tell
tell application "System Events" to tell (first process whose frontmost is true)
	set procID to its id
	set dName to name of first window whose subrole is "AXStandardWindow" # take care of possible inspector whose subrole is "AXFloatingWindow"
end tell -- System Events

if dName ends with ".pages" then
	set pdfName to (text 1 thru -6 of dName) & "pdf"
else
	set pdfName to dName & ".pdf"
end if

tell application "System Events" to tell (first application process whose id is procID)
	keystroke "p" using command down
	(*
Wait for the availability of the sheet *)
	repeat
		if exists sheet 1 of window dName then exit repeat
	end repeat
	#name of menu buttons of sheet 1 of window dName
	--> {"PDF"} but I don't know if it's spelled this way worldwide
	tell sheet 1 of window dName
		set PDFButton to first menu button
	end tell
	click PDFButton
	# name of menu items of menu 1 of PDFButton
	--> {"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", "Add PDF to iTunes", "Envoyer le document PDF par courrier électronique", "Enregistrer le document PDF dans le dossier de reçus web", missing value, "Modifier le menu."}
	
	click menu item 2 of menu 1 of PDFButton
	(*
Wait for the availability of the save in PDF file sheet *)
	repeat
		if exists sheet 1 of sheet 1 of window dName then exit repeat
	end repeat
	(*
Set the name of the new PDF *)
	set value of text field 1 of sheet 1 of sheet 1 of window dName to pdfName
	keystroke "d" using command down # set destination folder to Desktop
	keystroke return
end tell

With this script I am able to print the Pages '09 document open at front in a PDF file stored on the Desktop.
I assume that you are able to write the instructions moving it from this location to the wanted one.
As far as I know, Desktop is the unique location which may be defined for sure with no cumbersome trickery when we use Print to PDF.

And now a slightly edited one which print to PDF from the new Pages ( the one embedded in iPlay '13 )

(*
PagesNew2PDF
Cumbersome code to be sure that we use the new application,
not the old one which is "/Applications/iWork '09/Pages.app".
In this script grabbing the process ID wasn't required but the skeleton was grabbed from a script in which it was.
*)

tell application "/Applications/Pages.app"
	activate
	if version < "5" then error "You aren't running Pages version 5.x"
end tell
tell application "System Events" to tell (first process whose frontmost is true)
	set procID to its id
	set dName to name of first window whose subrole is "AXStandardWindow" # take care of possible inspector whose subrole is "AXFloatingWindow"
end tell -- System Events

if dName ends with ".pages" then
	set pdfName to (text 1 thru -6 of dName) & "pdf"
else
	set pdfName to dName & ".pdf"
end if

tell application "System Events" to tell (first application process whose id is procID)
	keystroke "p" using command down
	(*
Wait for the availability of the print sheet *)
	repeat
		if exists sheet 1 of window dName then exit repeat
	end repeat
	#name of menu buttons of sheet 1 of window 1
	--> {"PDF"} but I don't know if it's spelled this way worldwide
	tell sheet 1 of window dName
		set PDFButton to first menu button
	end tell
	click PDFButton
	# name of menu items of menu 1 of PDFButton
	--> {"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", "Add PDF to iTunes", "Envoyer le document PDF par courrier électronique", "Enregistrer le document PDF dans le dossier de reçus web", missing value, "Modifier le menu."}
	click menu item 2 of menu 1 of PDFButton
	(*
Wait for the availability of the save in PDF file sheet *)
	repeat
		if exists sheet 1 of sheet 1 of window dName then exit repeat
	end repeat
	(*
Set the name of the new PDF *)
	set value of text field 1 of sheet 1 of sheet 1 of window dName to pdfName
	keystroke "d" using command down # set destination folder to Desktop
	#	keystroke return
end tell

Yvan KOENIG (VALLAURIS, France) lundi 4 novembre 2013 17:03:16