Here is a workaround replacing the failing export instruction.
It gives us an extraneous feature : define the level of quality of the issued PDF.
--[SCRIPT]
use AppleScript version "2.4" # Yosemite or later OS
use framework "Foundation"
use scripting additions
set POSIXDestFolder to POSIX path of (path to desktop folder)
set PDFName to "deleteme.pdf"
tell application "Pages"
activate
considering numeric strings
if its version < "6" then error "The script requires Pages version 6 or later."
end considering
set thisDocument to make new document
tell thisDocument
set body text to "hello"
end tell
--export document 1 in testDoc as PDF -- this makes and saves the output file
my exportAsPDF(POSIXDestFolder, PDFName)
close document 1 without saving
end tell
on exportAsPDF(POSIXDestFolder, PDFName)
set {theApp, mt, ms} to {"Pages", 3, 1}
set OSversion to (current application's NSProcessInfo's processInfo()'s operatingSystemVersion())
--> {majorVersion:10, minorVersion:12, patchVersion:2} if macOS 10.12.2
tell OSversion to set OSversion to (its majorversion as text) & "." & its minorversion
activate application theApp
tell application "System Events"
set maybe to POSIXDestFolder & "/" & PDFName
if exists disk item maybe then delete disk item maybe
tell process theApp
set frontmost to true
# Trigger the menu item "PDF." of the menu item "Exportation vers" of menu "Fichier"
tell menu bar 1
get name of menu bar items
(*{
01 - "Apple",
02 - "Pages",
03 - "Fichier",
04 - "Édition",
05 - "Insertion",
06 - "Format",
07 - "Disposition",
08 - "Présentation",
09 - "Partager",
10 - "Fenêtre",
11 - "Aide"}
*)
get name of menu bar item mt
--> "Fichier"
tell menu bar item mt to tell menu 1
set theNames to get name of menu items
(* {
01 - "Nouveau",
02 - "Créer à partir de la liste de modèles.", # CAUTION: exists only if, since the installation of the app, the menu File was triggered once with alt depressed
03 - "Ouvrir.",
04 - "Ouvrir un élément récent",
05 - missing value,
06 - "Fermer",
07 - "Tout fermer",
08 - "Enregistrer.",
09 - "Enregistrer sous.",
10 - "Dupliquer",
11 - "Renommer.",
12 - "Déplacer vers.",
13 - "Revenir à ",
14 - "Exportation vers",
15 - missing value,
16 - "Convertir en Mise en page",
17 - missing value,
18 - "Avancé",
19 - missing value,
20 - "Définir le mot de passe.",
21 - missing value,
22 - "Enregistrer comme modèle.",
23 - missing value,
24 - "Format d'impression.",
25 - "Imprimer."}
*)
set indx to 0
repeat with aName in theNames
set indx to indx + 1
if contents of aName is missing value then exit repeat
end repeat
set mi to 9 + indx
get name of menu item mi
--> "Exportation vers"
tell menu item mi to tell menu 1
get name of menu items
(* {
1 - "PDF.",
2 - "Word.",
3 - "Format texte.",
4 - "ePub.",
5 - "Pages '09."
}*)
get name of menu item ms
--> "PDF."
click menu item ms
end tell -- menu item.
end tell
end tell -- menu bar.
tell window 1
repeat
delay 0.01
if exists sheet 1 then exit repeat
end repeat
tell sheet 1
-- class of UI elements --> {group, static text, list, static text, static text, pop up button, checkbox, static text, button, button, button}
-- name of buttons --> {"Annuler", "Suivant.", missing value}
tell pop up button 1
click it
-- class of UI elements -->{menu}
tell menu 1
-- class of UI elements --> {menu item, menu item, menu item}
-- name of menu items --> {"Bonne", "Supérieure", "Optimale"}
# Define the wanted level of quality
click menu item 3 --> "Optimale"
end tell
end tell # pop up button 1
click button 2
end tell # sheet
delay 0.1
tell sheet 1 # Save As dialog
-- class of UI elements --> {button, button, button, group, UI element, text field, static text, static text, text field}
-- name of buttons --> {"Exporter", "Nouveau dossier", "Annuler"}
-- value of text fields --> {"Sans titre 4.pdf", ""}
set value of text field 1 to PDFName
# Open the dialog allowing to define the destination folder *)
keystroke "g" using {command down, shift down}
repeat until exists sheet 1
tell me to delay 0.02
end repeat
tell sheet 1
-- class of UI elements --> {static text, combo box, button, button}
-- value of static text 1 --> "Aller au dossier :"
# if 10.11 or higher
--> {static text, combo box, button, button}
# if 10.10.x
--> {static text, text field, button, button}
--|-- log "Point ox0090"
(*
CAUTION the class of the field where the path must be inserted changed with 10.11 *)
considering numeric strings
if OSversion < "10.11" then
set theTarget to text field 1
else
set theTarget to combo box 1
end if
end considering
value of theTarget
(*
Insert the destination folder *)
set value of theTarget to POSIXDestFolder
keystroke return # ditto click button 1 (ou Aller)
end tell # sheet 1
click button 1 # (Exporter)
end tell # sheet 1
end tell # window
end tell # process
end tell # System Events.
end exportAsPDF
--[/SCRIPT]
CAUTION, I just received a mail saying that on an user’s machine, the menu item 14 is not the title of a submenu.
I’m waiting for more infos about that.
I got it.
If, since the installation of the app, the menu File was triggered once with alt depressed there is one complementary menu item in the menu File. It’s commented in the edited script.
Yvan KOENIG running Sierra 10.12.2 in French (VALLAURIS, France) dimanche 18 décembre 2016 13:58:53