Macworld > Ask the Script Doctor: Working with Text

Ask the Script Doctor is a regular column in which we solve real-world Mac problems using AppleScript and Automator. If you have a problem you’d like us to solve, send an email to the Script Doctor for consideration.

Whether we’re writing novels or email, we all use text all the time. AppleScript is a great tool for making text-focused work easier and more efficient. For that reason, I get a lot of questions about how to simplify common text tasks. Here are a few of those questions and my answers.

[Read more at Macworld.com, subscribe to the print edition, or get Macworld on your iPad...]

Do you have a scripting problem or a question for the Script Doctor? Send it to scriptdoctor@macworld.com.

Creating AppleScript Services in Mac OS X

Services are methods of inter-application communication in Mac OS X. They're ways for applications to share their features with other applications via the Application > Services menu, or through a Services contextual menu.

For example, open a TextEdit document and select some text. Next, choose TextEdit > Services > New Email with Selection. This is a Service provided by the Mail application.

I am often asked how to create AppleScript Services, i.e. scripts that you can run from the Services menu. There are a couple of ways to do this...

1) You can use Automator.

  • Launch Automator
  • Create a new Service workflow

  • Choose the type of content you want the script to process at the top of the workflow
  • Choose the application in which you want the service to be accessible
  • For text workflows, choose if you want the service to replace the selected text with the result of the workflow
  • Search for and insert the Run AppleScript action into the workflow
  • Enter your AppleScript code into the action. If you want the action to process the input, you'll need to write the code to do it. If you want to replace text, then you'll need to return text from the action as output

  • Save the workflow

You're done. You can now run the workflow from the services menu. Download a sample Service workflow that wraps selected text in quotes here.

2) The utility ThisService is a great tool for converting AppleScripts, Perl scripts, Python scripts, or Ruby scripts into Services.

Looking for some useful pre-existing Services to get you started? Visit macosxautomation.com to learn more about Services and download a TON of free Automator Service workflows.

Peachpit Blog Post > Extending Automator: Adding Workflows to the Services Menu

04.20.09 - Extending Automator: Adding Workflows to the Services Menu.

With the help of ThisService, a third-party utility from waffle software, and AppleScript, you can now run Automator workflows from the Services menu in Mac OS X. [Read more at Peachpit.com...]