Need some Help? Have a tip to share?

Email me at MacTipper@gmail.com, or, leave a comment!

5/20/08

Make Your Own Services

In a previous post, somebody commented asking how you can make your own services. After a bit of research, I have found out how.

First, a little bit of background. There are 3 kinds of services: input, output, filter. An input will take the text you have currently selected in the front application and pass it to you. You can then manipulate the text. An output will return some text, for example, the current date/time. A filter will take the currently selected text and return some filtered text.

For today's tip, we will be creating a filter service. Specifically, our service will take the selected text, and a url from the clipboard. It will then turn the selected text into a formatted url (which it got from the clipboard). A great use for this is if you hand-code your URLs for your blog.

You will need:
ThisService (As always, free.)
A smidgin of Applescript Skills


Step 1: Download ThisService. ThisService is an applicaiton which creates (and installs) services from Applescript, Ruby, Perl, and Python.

Step 2: If you want to create your own service, ThisService comes with 3 "starting points" applescripts. These are the barebones of what you need for an applescript service. You can find these in the ThisService folder/ThisService starting points/Applescript/.

The starting point for a filter applescript is:


on process(the_selection)

return (return_text as string)

end process


the_selection = the currently selected text.

return_text = the text that replaces the selected text. This must be in a string format. Hence why I convert it at the last step.


Step 3: So, if we wanted to take the selected text and add " my text" to the end of it. We would use:


(note that you do not have to use the return_text identifier. You could simply use "return (the_selection & " my text") as string", however, using return_text is easier. (For me at least.)


Step 3: With all this in mind, the applescript to format a link is:




Type this into script editor or download it as a zip file here. (BTW, it's an image because the HTML in the script was being activated in the browser.)



Step 4: Now that you have the applescript, open up ThisService. Click on the create service tab. Most of the fields are self-explanitory. However, I'll go over all of them.

Name: The name that you want to see in the service menu.

Script: Click on the choose... button and pick the applescript your using.

Type: Select the type of service you're creating. (Covered earlier.) We're creating a filter service for this tip.

Menu Shortcut: I would actually leave this blank. Then, go to System Preferences and create your own shortcut. The reason for doing this is so that you can have any shortcut you want.

Include: If you select "Copy of Script" then it will copy the script and put it into the service. However, you can still edit your script. (More later.) If you select "Reference to Script" then you can easily edit the script and have your edits automatically effective. However, if you delete your script, then you have to rewrite it. I would select "copy".



Step 5: After filling out everything. Hit the "Create Service" button. Select the "Reveal in Finder" option from the dialog that appears.


Step 6: Finder should open up with your service selected. Right click on your service and select "Show Package Contents". Then, navigate to /Contents/Resources/

The script file inside that folder is your service applescript. You can open it up and edit it. After editing it, hit Cmd-S to save and run your service. If all went well, your changes should be reflected immediately.


That's it! If you want to add a keyboard shortcut to your service, use the System Preferences' Keyboard & Mouse pane.

0 comments:

Post a Comment

Subscribe Via Email!

Enter your email address:

Delivered by FeedBurner