on imprimeDansPDF(the_App, nom_pdf)
activate application the_App
tell application "System Events" to tell application process the_App
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
set PDFButton to first menu button of sheet 1 of window 1
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
if modernVersion then
(*
The Print interface is no longer a window but a sheet *)
repeat
if exists sheet 1 of sheet 1 of window 1 then exit repeat
end repeat
tell sheet 1 of sheet 1 of window 1
if nom_pdf is not "" then set value of text field 1 to nom_pdf
# set the target folder to Desktop
keystroke "d" using {command down}
# name of buttons
--> {"Enregistrer", "Nouveau dossier", "Annuler"}
click button 1 # ditto keystroke return
end tell
else
(*
Wait for the availability of the Print window *)
repeat
if (count windows) > nbw then exit repeat
end repeat
# position of text fields of window 1
--> {position:{875, 190}}
(*
Set the name of the new PDF *)
if nom_pdf is not "" then set value of text field 1 of window 1 to nom_pdf
# set the target folder to Desktop
keystroke "d" using {command down}
# name of buttons of window 1
--> {missing value, missing value, missing value, "Enregistrer", "Nouveau dossier", "Annuler"}
click button -3 of window 1 # ditto keystroke return
end if # modernVersion
end tell
end imprimeDansPDF