(*
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 version > "4.99" 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 window 1
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)
set nbw to count windows
keystroke "p" using command down
(*
Wait for the availability of the sheet *)
repeat
if exists sheet 1 of window 1 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 1
set cntUIitems to count UI elements
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 (count UI elements of sheet 1 of window 1) ≠ cntUIitems 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 1 to pdfName
keystroke "d" using command down # set destination folder to Desktop
keystroke return
end tell