Bellhop Help

About Services

Services are a unique feature of Mac OS X that allow applications to be extended in various ways by invoking the functions (or services) of another application. Mac OS X's services system allow applications to advertise their functionality to other applications without each application having to know about the other. The operating system takes care of the "glue" required to send data back and forth. Bellhop allows you to implement your own custom services using scripting languages instead of having to write full-blown Mac OS X applications to provide them.

Every Cocoa application and most Carbon applications have a standard Services menu, which displays the list of services that are available based on the current selection. For more detailed information about system services, see the Apple developer documentation for services. The figure below is a sample of the Services menu in the TextEdit application (Note: The menu will be different based on which application is currently active).

Figure 12: Services menu within TextMate

 

The menu items in the Services menu are updated each time the selection changes. In a typical scenario, a user working in one application, say an email application, will select some text and then operate on that text using the available services. There might be a service that rewraps the selected text and then writes it back to the document which the user is editing. If our user selects this "rewrap" service from the Services menu, the text that he or she had selected would be rewrapped according to the way the service was implemented.

 

Behind the Scenes

When you invoke a service from the Services menu, you are actually initiating an inter-application communication process. The application from which you invoke the service (the requesting application) puts its current selection on a special pasteboard (the services pasteboard). When you invoke a service, the operating system then makes this pasteboard available to the application that carries out the service (the providing application), and then messages the providing application with the appropriate pasteboard. The provider then carries out its task, and puts the resulting data back on the pasteboard (if necessary). The operating system then puts this modified data back into the requesting application, replacing its current selection (if necessary).

To illustrate the process, consider the following example of a user that is doing the above steps manually. Our user is in an email program and is replying to an email. The email has been heavily quoted, so that there are many levels of indentation and the lines are broken up. However, she has a text editor that can rewrap text to fix the indentations. So, she copies the selected text in the email, switches to her text editor, pastes the copied text and reselects it. Then, while in the text editor, she applies the text editor rewrap function to the newly pasted text, selects the modified text and copies it to the pasteboard, and then switches back to her email program and pastes the modified text back into her email program. Using services, you can eliminate the cutting, pasting, and switching of applications, and instead rely on the operating system to do those tasks.

So how are services made available in the Services menu? There are a couple requirements that must be met in order for a service to be made available. First, there must be an application that provides the service. Going back to our hypothetical user, she might have a text editor installed that has a "rewrap" function. This brings us to the second requirement: an application has to advertise its services. The text editor has to let the Finder know that it has the ability to rewrap text, and that other applications are allowed to invoke this functionality via the Services menu.

After a particular service is advertised in the Services menu, there are still some conditions that must be met before an application is allowed to request it. Usually, the application that provides the service expects a certain type of data. Secondly, the application that requests the services must provide the data in a format that the providing application can understand. Finally, the providing application can also return data back to the requesting application, although this is not required. However, if it does return data back, it must be in a format that the requesting application can understand. If all of the above conditions are met, then the requesting application can safely invoke the service.

So, to summarize with our example, our user is composing an email message in her favorite email program. She is replying to a message that has a paragraph that is excessively indented, and she wishes to rewrap the entire paragraph. So, she selects the paragraph, looks in the Services menu, and selects the "Rewrap" menu item that is provided by the text editor. The operating system then takes the user's selection and hands it to the text editor. The text editor then takes the selection, applies its rewrap functionality and then returns the rewrapped text back to the email operating system, which then pastes the data into the email program. Note that after the user invokes the service, the remaining steps are carried out behind the scenes by the operating system.

 

Services with Bellhop

The above example is a simplified view of how Services work under Mac OS X, but it highlights a key point: In order to provide a service, you need to have an application that implements and advertises the service. In many situations, however, a simple script would be sufficient. For example, a Ruby script to rewrap text is quite easy to implement.

This is where Bellhop fits it. The Bellhop application allows you to create and edit services in one of the supported scripting families and then add them to the Bellhop database. Bellhop acts as the providing application that can execute these scripts on behalf of requesting applications, thus freeing you from writing your own full-blown applications or bundles and maintaining property lists, etc.—all you have to do is write the service code and let Bellhop take care of the low-level details. The hardest part might be deciding how to label your new service!

Bellhop supports services written in AppleScript, F-Script, Perl, Python and Ruby, however it is designed to support other scripting languages via a plug-in architecture. All of the details of writing out the property list files to advertise your services, making sure they're in the proper place, etc. are taken care of by Bellhop. All you have to do is write your services scripts!