11

I made an AppleScript, saved it as a file in a specific folder on my hard drive. Now I want to assign a shortcut to it and looks like I have to make a Service via Automator, but the Run AppleScript asks me to put the code inside, while I want to execute the already existing .scpt file. How to do that?

10

Yes, Automator is the best option here. To create the service follow these steps:

  1. Open Automator and select Service:

    enter image description here

  2. Search 'applescript' and drag the action to the right pane:

    enter image description here

  3. Change Service receives selected text to Service receives no input so that it always appears in the Services menu:

    enter image description here

  4. Replace (* Your script goes here *) with:

    do shell script "/usr/bin/osascript /path/to/your/script.scpt"
    

    if you wish to execute your existing .scpt file. Note that osascript can't handle user interaction like displaying a dialog window:

    enter image description here

    To overcome this limitation either use this workaround or simply replace (* Your script goes here *) with the contents of your script.

  5. Save it:

    enter image description here

  6. If you wish, you can assign a shortcut to your new service. Select Services>Services Preferences in Finder (or any open application):

    enter image description here

  7. Assign a not so common shorcut:

    enter image description here

  8. Now your service is accessible from any application through the shortcut you assigned in the previous step:

    enter image description here

If you ever want to get rid of the service open ~/Library/Services, delete the service and empty the trash:

enter image description here

9
  • I said I want to execute existing script file, not to write it again into Automator. – Nakilon Sep 2 '13 at 10:46
  • I thought that copy/paste would be OK, but yes, you're right, it's stated clearly in your question. Use osascript, as explained in other answers. – jaume Sep 2 '13 at 11:39
  • @Nakilon I've added how to use osascript in Automator to run your .scpt file, a limitation and two workarounds. – jaume Sep 3 '13 at 7:25
  • I just did this. It worked; but, it now takes 4 to 5 seconds to launch the script. (When the code was saved directly in the workflow, it launched almost immediately.) Any idea what causes this, or how make it launch faster? – Zack Dec 28 '18 at 18:03
  • 1
    @Zack Thanks, I don't know why osascript needs 4 additional seconds to execute your script, but Automator is not the cause of the delay, which was my concern. You may want to debug osascript, see opensourcehacker.com/2011/12/02/… and stackoverflow.com/questions/31045575/… – jaume Jan 9 '19 at 13:33
4

In the script, add an on run argument:

on run
    -- do whatever the script does
end run

Then you can use the Automator "Run Shell Script" action, with the osascript command:

osascript /path/to/script.scpt

osascript is a command which executes AppleScript in the shell. You need the lines in the script to allow the script to be executed outside AppleScript editor.

2
  • 1
    You don't need to include an explicit run handler. All scripts have an implicit run handler. The reason why Automator's Run AppleScript action includes an explicit run handler might be because Automator uses the special on run {input, arguments} form, but even in Automator you can just remove the run handler. – Lri Sep 2 '13 at 9:13
  • 1
    @͏͏͏ I'm wondering if this ping gets to you. Kudos on the creative name selection. – bmike Sep 4 '13 at 19:53
3

Perhaps you'd want to take a look at This Service, which allows you to "create Mac OS X services from any script."

1
  • 1
    Can you provide an answer here rather than just a link – mmmmmm Sep 2 '13 at 9:40
2

I wouldn't recommend creating Automator services to assign shortcuts to scripts. There is a bug in in 10.7 and 10.8 where the keyboard shortcuts for Automator services don't always work until you hover over the services menu from the menu bar. There is also a relatively long (maybe 0.1 to 1 s) delay before services are run.

See this answer for other ways to assign keyboard shortcuts to scripts. I use FastScripts and Alfred myself.

2
  • I have never experienced this need to hover? do you have more details of what you are talking about – markhunte Sep 2 '13 at 10:53
  • 1
    I'm not sure if it affects all OS X installations or all services, but I've ran into it multiple times on different installations, and it has mentioned by other people here and at Super User. Occasionally the keyboard shortcuts for all Automator services stop working (or nothing happens if you press them), but they start working again if you hover over the services menu from the menu bar, or if you for example select one service with the mouse. – Lri Sep 3 '13 at 8:08
2

You can RUN the existing script with a simple automator service like this. And assign your shortcut from within the keyboard short cuts as normal. Just remember some applications may need to be relaunched before they pick the shortcuts up.

example.

sayHello script:

say "hello"

Automator service: enter image description here

Which in this example uses:

  • No input
  • Any application.

The Action is a Run Shell Script The code just uses the unix command osascript and the path to the file. Pass input can be to stdn or to argument

Thats it nothing special needs to be done to the script.

For further details on the osascript command look at the man pages for it. The man page give another example where you can pass arguments to the script.

0

To develop a Service, you need some sort of software which will create a Service such as one of the following:

  1. Automator to create a .workflow
  2. ThisService to create a .service
  3. Xcode to create an .app

All of the above kinds of services are packages. They are all folders which contain an plist that specifies the attributes of a Service.

Here is how I created a Service using Automator that uses a "Run AppleScript" Automator action to call an existing .script.

Automator Service That Calls an Existing AppleScript

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .