How to make an existing AppleScript file to work as a service?
Yes, Automator is the best option here. To create the service follow these steps:
-
Open Automator and select Service:
-
Search 'applescript' and drag the action to the right pane:
-
Change
Service receives selected text
toService receives no input
so that it always appears in the Services menu: -
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:To overcome this limitation either use this workaround or simply replace
(* Your script goes here *)
with the contents of your script. -
Save it:
-
If you wish, you can assign a shortcut to your new service. Select Services>Services Preferences in Finder (or any open application):
-
Assign a not so common shorcut:
-
Now your service is accessible from any application through the shortcut you assigned in the previous step:
If you ever want to get rid of the service open ~/Library/Services
, delete the service and empty the trash:
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.
Perhaps you'd want to take a look at This Service, which allows you to "create Mac OS X services from any script."