In OS X, services let you access functionality in one app from within another app. An app that provides a service advertises the operations it can perform on a particular type of data. Services are triggered from the Application Name > Services menu, or from contextual menus that appear when you Control-click on text, files, and other kinds of data. When you’re manipulating a particular type of data, related services becomes available. For example, Mail provides a service that creates a new email from selected text.
Making a Script Available as a Service
A script can be made available as a service by embedding it in an Automator service workflow.
To create a service workflow that runs a script
Launch Automator, found in /Applications/.
Create a new Automator document.
When prompted, choose a document type of Service and click Choose.
Write the script code and add it to the action. If the action contains additional configuration options, adjust them as needed.
For AppleScripts and JavaScripts, use the action’s run handler template to process input data when the service runs, such as text or files. For workflows that replace selected text with processed text, be sure your workflow results in a text value. See Example Service Workflow Scripts.
Listing 40-1 and Listing 40-2 provide example code that can be pasted into the Run AppleScript and Run JavaScript Automator actions to convert selected text to uppercase.
Listing 40-2JavaScript: Example of an Automator service script that converts selected text to uppercase
function run(input, parameters) {
var selectedText = input[0]
return selectedText.toUpperCase()
}
Triggering Service Workflows
Saved Automator service workflows automatically appear in services menus throughout the system at the appropriate time. For example, text processing workflows become available when you select text in an app. To run a service, select Application Name > Services > Service Workflow Name from the menu bar, or select Services > Service Workflow Name from a contextual menu.