As is, the late script trigger Duplicate then Export… when it apply to Preview.
I choosed Export… because when I pass the parameters {9, 8}
the log returned by AppleScript display the name of the menu item "Save a version" but it seems that it's just a cosmetic oddity. In fact, the applied action is really "Save…".
So, I made a new change.
I no longer trigger the menu item "Save…" but issue the shortcut command + S which is a bit faster than triggering the menu item.
I added some comments so that every action is explained.
Late not least, as the Original Poster wanted to apply the Save… action the script is now defaulting with :
Property triggerSave : true.
--{code}
(*
Yvan KOENIG (VALLAURIS, France)
2011/09/26
*)
property triggerSave : true
(*
true = trigger Duplicate then trigger Save…
false == trigger only Duplicate
*)
on run
(*
Use the property triggerSave
*)
local theApp, miDuplicate, sourceName
(*
Get the name of the frontmost application *)
tell application "System Events" to set theApp to name of first application process whose frontmost is true
(*
Define the index of the menu in which we will trigger an item.
The File menu is the 3rd one. *)
set mtFile to 3
(*
Define the index of the menu item to trigger *)
if theApp is "Pages" then
set miDuplicate to 10
else if theApp is in {"Numbers", "Keynote", "Preview"} then
set miDuplicate to 9
else if theApp is "TextEdit" then
set miDuplicate to 8
else
error number -128 (* Exit silently if the app isn’t one of the five treated ones *)
end if
tell applicationtheApp to activate
tell application "System Events" to tell process theApp
if triggerSave then
(*
Get name of current document *)
set sourceName to name of first window whose subrole is "AXStandardWindow"
(*
Prepare the loop which would close the sheet "Your document has been changed… " *)
set nbw to count of (every window whose subrole is "AXStandardWindow")
end if
(*
Trigger the menu item Duplicate *)
tell menu bar 1 to tell menu bar itemmtFile to tell menu 1 to clickmenu itemmiDuplicate
(*
Try to click the button Duplicate in a possible sheet "Your document has been changed… " *)
if triggerSave then
repeat 5 times
delay 0.1
try
click first button of first sheet of window sourceName
exit repeat
end try
end repeat
(*
Wait for the availability of the replicate document.
CAUTION : Preview doesn't add the word 'copy' to the replicate's name *)
repeat while nbw = (count of (every window whose subrole is "AXStandardWindow"))
--
end repeat
(*
Trigger the menu item Save… *)
keystroke "s" using {command down}
end if -- triggerSave
end tell
end run
--{code}
Yvan KOENIG (VALLAURIS, France) lundi 26 septembre 2011 14:42:59
iMac 21”5, i7, 2.8 GHz, 4 Gbytes, 1 Tbytes, mac OS X 10.6.8 and 10.7.0
My iDisk is : <http://public.me.com/koenigyvan>
Please : Search for questions similar to your own before submitting them to the community