I don't guess what you made wrongly.
My late script was :
--{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}
and it compiles flawlessly.
To pack it in a Service, yout must edit it a bit :
--{code}
(*
Yvan KOENIG (VALLAURIS, France)
2011/09/26
*)
property triggerSave : true
(*
true = trigger Duplicate then trigger Save…
false == trigger only Duplicate
*)
on run {input, parameters} -- the list is required for Services
(*
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
return input-- required for Service.
end run
--{code}
If you continue to fail, send what you done to my mailbox (click my blue name to get my address).
I guess that I would understand what is wrongly done.
message edited because, I don't know why, the original script was clipped on its left edge
Yvan KOENIG (VALLAURIS, France) dimanche 15 avril 2012
iMac 21”5, i7, 2.8 GHz, 12 Gbytes, 1 Tbytes, mac OS X 10.6.8 and 10.7.3
My Box account is : http://www.box.com/s/00qnssoyeq2xvc22ra4k