Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

Create keyboard shortcut for duplicated action

Hello everyone:


I've tried this for a while yet I can't manage to do it... Since the Apple Geniuses decided to remove the handy save as instruction in Pages for lion (i know its called duplicate but its not the same). I've tried to set up a keyboard shortcut for Duplicate yet the thing is that there is a duplicate command under both Edit AND File on the menu bar. When I go to create the shortcut, it creates the shortcut for Duplicate under edit, but what I want is for the shortcut to create a duplicate of the fille (what would happen if i clicked File->Duplicate)


Anyone knows how to set it up so that i create the shortcut for the menu title under File and NOT under Edit?




I'm incredibly sorry this question is SO ambiguous its crazy... Tell me if you want me to explain clearer.


Thanks


Juan

MacBook Pro 13', Mac OS X (10.7)

Posted on Sep 25, 2011 8:40 AM

Reply
37 replies

Sep 25, 2011 12:32 PM in response to Don Ju4n

With a bit of luck, Badunit will have time available to describe the way to encapsulate a script in a service.


Here is a script which triggers the menu item Duplicate for Numbers and Pages.


--{code}


tell application "System Events"

set theApp to name of first application process whose frontmost is true

end tell

if theApp is not in {"Numbers", "Pages"} then error number -128


set {mt, mi} to {3, 9}

tell applicationtheApp to activate

tell application "System Events" to tell process theApp to tell menu bar 1 to tell menu bar item mt to tell menu 1 to click menu item mi


--=====


on decoupe(t, d)

local oTIDs, l

set oTIDs to AppleScript's text item delimiters

set AppleScript's text item delimiters to d

set l to text items of t

set AppleScript's text item delimiters to oTIDs

return l

end decoupe


--=====

--{code}


Yvan KOENIG (VALLAURIS, France) dimanche 25 septembre 2011 21:32:01

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

Sep 25, 2011 2:18 PM in response to KOENIG Yvan

I made a bit of cleaning in the script.


--{code}


tell application "System Events" to set theApp to name of first application process whose frontmost is true


if theApp is not in {"Numbers", "Pages"} then error number -128


set {mt, mi} to {3, 9}

tell applicationtheApp to activate

tell application "System Events" to tell process theApp to tell menu bar 1 to tell menu bar item mt to tell menu 1 to click menu item mi


--{code}


Yvan KOENIG (VALLAURIS, France) dimanche 25 septembre 2011 23:18:33

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



Sep 25, 2011 6:30 PM in response to KOENIG Yvan

I got it! The Automator help was not very helpful, but by trying different things I got it


Open Automator.app (in your Applications folder) & go to File > New. Click on Service & then Choose.


User uploaded file


You will then get an window with the name Untitled (Service). Now click on Automator in the first column then drag Run AppleScript into the main window.


User uploaded file

Highlight the line (* Your scriot goes here *) & paste in Yvan's script from his post. The text will be purple, so click the hammer to compile the script. I've added Preview to the scipt for myself because I need Save As… there, too.


User uploaded file


User uploaded file


Now go to File > Save… & save the service with a name you want. I chose Save As. The service will be saved in your home Library > Services folder.


Last, open System Preferences & choose Keyboard. In the left-hand column select Application Shortcuts then click the + below the main window. You need to create a shortcut for each of the applications listed in your AppleScript. Don't use All Applications as there are many that still have a Save As command.


User uploaded file

Close System Preferences & you're done!

Sep 25, 2011 7:36 PM in response to Peggy

When I first did this I named my service Duplicate file & that worked. For some reason I don't understand, naming it Save As doesn't work.


So, just a couple of corrections & additions to the above steps.


Make sure the drop-down menu that says "text" by default is changed to "no input."


User uploaded file


Don't name your service Save As.


Before setting the application shortcuts in System Preferences > Keyboard, click Services & enable/check/tick the box next to the name of your new service.


User uploaded file


Now it should work.


User uploaded file

Sep 26, 2011 2:39 AM in response to Peggy

Hello


(1) I must apologize, the posted script is OK for Numbers but not for Pages whose menu item aren't numbered the same.


(2) Here is an enhanced version which treat the five Apple Applications embedding the Duplicate / Save… scheme : Keynote, Numbers, Pages, Preview, TextEdit


Not only it triggers the menu item "Duplicate" but if you set the property triggerSave, it triggers the menu item "Save…"


--{code}

(*

Yvan KOENIG (VALLAURIS, France)

2011/09/26

*)


property triggerSave : false

(*

true = trigger Duplicate then trigger Save…

false == trigger only Duplicate

*)

on run


(*

Use the property triggerSave

*)

local theApp, miDuplicate, miSave, sourceName

tell application "System Events" to set theApp to name of first application process whose frontmost is true


if theApp is "Pages" then

set {miDuplicate, miSave} to {10, 9}

else if theApp is in {"Numbers", "Keynote"} then

set {miDuplicate, miSave} to {9, 8}

else if theApp is "TextEdit" then

set {miDuplicate, miSave} to {8, 7}

else if theApp is "Preview" then

set {miDuplicate, miSave} to {9, 10}

else

error number -128

end if

set mtFile to 3

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


if triggerSave then

delay 0.2

repeat 5 times

try

click first button of first sheet of window sourceName

exit repeat

end try

end repeat

repeat while nbw = (count of (every window whose subrole is "AXStandardWindow"))

--

end repeat

(*

Trigger the menu item Save… *)

tell menu bar 1 to tell menu bar itemmtFile to tell menu 1 to clickmenu itemmiSave

end if -- triggerSave

end tell

end run

--{code}


Complementary tip : as the system dislike the name "Save As", you may name the Service as "Save_As".

With the embedded undescore the system will be happy.


Yvan KOENIG (VALLAURIS, France) lundi 26 septembre 2011 11:39:42

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



Sep 26, 2011 2:53 AM in response to Peggy

Peggy said: "You will then get an window with the name Untitled (Service). Now click on Automator in the first column then drag Run AppleScript into the main window."



I have no "Automator" in the first column, nor many other apps --> ????


So, I used a "Run Applescript" from the entire Library. However...


When I try "Save_As" in Page.app, it simply does a Duplicate and nothing else. --> Huh???

Sep 26, 2011 3:05 AM in response to William Donelson

User uploaded file


Automator.app is installed on every system for years.


The enhanced script which I posted just before your own message is designed to be able to do Duplicate AND Save…


To achieve that, you must set the property triggerSave to true (by default I set it to false).


I don't see the interest of this script if it's not embedded in a Service.

Triggerring the menu items Duplicate and Save… is faster than triggering the Script from the Script menu.


Yvan KOENIG (VALLAURIS, France) lundi 26 septembre 2011 12:03:41

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



Sep 26, 2011 3:19 AM in response to KOENIG Yvan

KOENIG Yvan wrote:


User uploaded file


Automator.app is installed on every system for years.


The enhanced script which I posted just before your own message is designed to be able to do Duplicate AND Save…


To achieve that, you must set the property triggerSave to true (by default I set it to false).


I don't see the interest of this script if it's not embedded in a Service.

Triggerring the menu items Duplicate and Save… is faster than triggering the Script from the Script menu.


Yvan KOENIG (VALLAURIS, France) lundi 26 septembre 2011 12:03:41

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



😟

I am very frustrated on two counts:

1. I have Automator.app in my Applications folder, and it runs itself, so should appear in the list of apps in itself, yes? But it does NOT appear in the list of apps in the left-hand column.


2. When I go back into Automator, how do I find the previous "service" document that I created in your & Peggy's steps?


Thanks

Sep 26, 2011 4:54 AM in response to William Donelson

Under 10.7, the Automator app doesn't appear in the first column but the service "Run AppleScript" is available in the 2nd column. Here it appears with its French name : "Exécuter un script AppleScript".


User uploaded file


Yvan KOENIG (VALLAURIS, France) lundi 26 septembre 2011 13:53:54

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



Sep 26, 2011 5:43 AM in response to KOENIG Yvan

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



Sep 26, 2011 10:09 AM in response to Peggy

Hi Peggy


My screenshot was made under Lion too and there is no Automator entry.


Yvan KOENIG (VALLAURIS, France) lundi 26 septembre 2011 19:09:45

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



Create keyboard shortcut for duplicated action

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple ID.