If you want details, send a mail to :
koenig yvan sfr fr
my activateGUIscripting()
set sysv to get system attribute "sysv"
# 4144 = 10.3.0 = 4096 + 3*16 + 0
# 4153 = 10.3.9 = 4096 + 3*16 + 9
# 4160 = 10.4.0 = 4096 + 4*16 + 0
# 4171 = 10.4.11 = 4096 + 4*16 + 11
# 4176 = 10.5.0 = 4096 + 5*16 + 0
# 4184 = 10.5.8 = 4096 + 5*16 + 8
# 4192 = 10.6.0 = 4096 + 6*16 + 0
# 4200 = 10.6.8 = 4096 + 6*16 + 8
# 4208 = 10.7.0 = 4096 + 7*16 + 0
# 4213 = 10.7.5 = 4096 + 7*16 + 5
# 4224 = 10.8.0 = 4096 + 8*16 + 0
# 4226 = 10.8.2 = 4096 + 8*16 + 2
# 4240 = 10.9.0 = 4096 + 9*16 + 0
if sysv < 4144 then
# it's OS X 10.2.x or older
else if sysv < 4160 then
# it's OS X 10.3.x
else if sysv < 4176 then
# it's OS X 10.4.x
else if sysv < 4192 then
# it's OS X 10.5.x
else if sysv < 4208 then
# it's OS X 10.6.x
else if sysv < 4224 then
# it's OS X 10.7.x
my select_Menu("Preview", 3, 9) # Duplicate
(* {
01 - "Créer à partir du presse-papiers",
02 - "Ouvrir.",
03 - "Ouvrir l'élément récent",
04 - missing value,
05 - "Fermer",
06 - "Tout fermer",
07 - "Fermer l'image sélectionnée",
08 - "Enregistrer une version",
09 - "Dupliquer",
10 - "Exporter.",
11 - "Faire revenir le document à une version précédente...", # CAUTION this one use three dots when others use ellipsis !
12 - missing value,
13 - "Envoyer l'image sélectionnée",
14 - "Saisir le mot de passe.",
15 - missing value,
16 - "Importer depuis l'appareil photo.",
17 - "Importer depuis le scanner",
18 - "Effectuer une capture d'écran",
19 - missing value,
20 - "Imprimer."}
*)
else if sysv < 4240 then
# my select_Menu("Preview", 3, 9) # Export.
# it's OS X 10.8.x
(* {
01 - "Créer à partir du presse-papiers",
02 - "Ouvrir.",
03 - "Ouvrir l'élément récent",
04 - missing value,
05 - "Fermer",
06 - "Tout fermer",
07 - "Fermer l'image sélectionnée",
08 - "Enregistrer",
09 - "Exporter.",
10 - "Revenir à la version enregistrée.",
11 - missing value,
12 - "Envoyer l'image sélectionnée",
13 - "Saisir le mot de passe.",
14 - missing value,
15 - "Importer depuis la caméra.",
16 - "Importer depuis le scanner",
17 - "Effectuer une capture d'écran",
19 - missing value,
20 - "Imprimer."}
*)
activate application "Preview"
tell application "System Events" to tell application process "Preview"
tell menu bar 1 to tell menu bar item 3 to tell menu 1 to click menu item 9 # Exporter.
repeat
delay 0.1
if exists sheet 1 of window 1 then exit repeat
end repeat
keystroke "d" using {command down} # to save on the Desktop
tell sheet 1 of window 1
# If the default filename ends with .pdf, there is no need to trigger the pop up menu
if value of text field 1 does not end with ".pdf" then
tell group 1
class of UI elements
--> {pop up button, static text, pop up button, static text, text field, static text, text field, static text, checkbox}
value of pop up button 1
--> "JPEG"
click pop up button 1
tell pop up button 1
name of menu items of menu 1
--> {"JPEG", "JPEG-2000", "OpenEXR", "PDF", "PNG", "TIFF"}
click menu item "PDF" of menu 1
end tell # pop up button 1
end tell # group 1
end if
# Define the name of the PDF
set value of text field 1 to "nouveau PDF.pdf"
keystroke return # Save
end tell # sheet 1 of window 1
end tell # System Events
end if
#=====
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
#=====
(*
my selectMenu("Preview", 3, 9)
==== Uses GUIscripting ====
*)
on selectMenu(theApp, mt, mi)
activate application theApp
tell application "System Events" to tell application process theApp to tell menu bar 1 to ¬
tell menu bar item mt to tell menu 1 to click menu item mi
end selectMenu
#=====
(*
useful to get the indexs of the triggered item
my select_Menu("Preview", 3, 9) (* File > Duplicate *)
==== Uses GUIscripting ====
*)
on select_Menu(theApp, mt, mi)
activate application theApp
tell application "System Events" to tell process theApp to tell menu bar 1
get name of menu bar items
(*{
01 - "Apple",
02 - "Numbers",
03 - "Fichier",
04 - "Édition",
05 - "Présentation",
06 - "Aller",
07 - "Outils",
08 - "Signets",
09 - "Fenêtre",
10 - "Aide"}
*)
get name of menu bar item mt
--> {"Fichier"}
tell menu bar item mt to tell menu 1
get name of menu items
get name of menu item mi
--> {"Dupliquer"}
#click menu item mi
end tell -- menu bar.
end tell -- System Events
end select_Menu
#=====
As you see, my memory failed about 10.8 behavior.
Save As. isn’t back in Preview (it’s in TextEdit, Keynote, Numbers, Pages and it’s available in iBooks Autor and Script Editor).
So I gave two ways to replace the missing feature.
For 10.7.x, I gave the code triggering Duplicate leaving you the task to code : save the replicate
For 10.8.x, I gave the entire code using Export.
In fact, you may use the Export. scheme in both cases.
Of course, you may remove the instructions which are coded to explain the tools behavior.
I forgot to give the code moving the newly saved file from the Desktop to the wanted final location.
You will have to add it.
My best choice to do that is « do Shell script “mv .” » or ASObjC Runner’s manage file feature.
Yvan KOENIG (VALLAURIS, France) dimanche 27 janvier 2013 12:26:53