A Simple F-Script Example
Overview
The following F-Script service will capitalize the first letter of each word in the current selection. This is a trivial example in that there is no GUI interaction nor does it deal with multiple pasteboard types. It does, however, illustrate the basic points to consider when implementing services with F-Script.
A Simple Example: Capitalize Words
The following code listing shows a complete Bellhop service script written in the F-Script language. You can try this yourself by simply creating a Bellhop document, adding a new F-Script service, and then pasting the following code directly into the editor window. Then all you have to do is activate the service using the Settings panel, and then you can invoke it from any services-aware application.
" * Sample F-Script service to capitalize all words in the currently * selected text. This service might be labeled 'Capitalize Words' * in the Services menu. " runService := [ :aPasteboard | "** read string from pboard **" pbString := Pasteboard readStringOfType:NSStringPboardType forPasteboard:aPasteboard. "** capitalize string properly **" pbString := pbString capitalizedString. "** declare returned data type on pasteboard and write it back **" Pasteboard declareTypes:{NSStringPboardType} forPasteboard:aPasteboard. Pasteboard writeString:pbString ofType:NSStringPboardType forPasteboard:aPasteboard. ]
Listing 1: Sample F-Script service
If you are familiar with the concepts and implementation details regarding system services in Mac OS X, the above script should be straightforward. If not, you can refer to the Apple developer documentation covering System Services and the More Info section of this document.
Looking at Listing 1, the first non-comment line is
runService := [ :aPasteboard |
The following lines read the selection from the service pasteboard:
"** read string from pboard **" pbString := Pasteboard readStringOfType:NSStringPboardType forPasteboard:aPasteboard.
Next we manipulate the pasteboard data to modify it:
"** capitalize string properly **" pbString := pbString capitalizedString.
Finally, we must write the modified data back to the pasteboard so that it is made available to the requesting application:
"** declare returned data type on pasteboard and write it back **" Pasteboard declareTypes:{NSStringPboardType} forPasteboard:aPasteboard. Pasteboard writeString:pbString ofType:NSStringPboardType forPasteboard:aPasteboard.
These lines first inform aPasteboard (our unique service pasteboard) that we are about to provide it with string data. Then we actually write the modified string to aPasteboard. Again, notice that we are using the custom Bellhop Pasteboard class to deal with the Mac OS X pasteboard.
And finally, to conform to valid F-Script syntax, we close our block definition:
]
It is worth pointing out that we did not have to explicitly import any Bellhop custom classes or libraries, and we are using constants that mimic those defined in the Cocoa frameworks.
That's all there is to it! When a user invokes our "Capitalize Words" service from the Services menu (e.g. from the TextEdit application), the operating system will automatically forward the request to Bellhop, which will in turn invoke the appropriate script's runService block. All of your services will follow a similar pattern.