macOS Automator – Create a Service to Put Selected Text into Quotation Marks

automatormacosscriptservicestext;

I'd like to create a service that puts any text I select in TextEdit or web page text editors into quotation marks. I understand Automator is the tool to use, but how do I create this action using AppleScript or another scripting language in OS X (Lion)?

Best Answer

One way is to create a service that runs a shell command.

For this, open Automator, create a new Service, check "Output replaces selected text", then add the action "Run Shell Script". In the box where you enter the Shell script write:

cat | sed 's/^/"/' | sed 's/$/"/' | sed 's/""//'

This takes the text you entered as input, and adds a quotation mark at the beginning and end.

Now, once you save your service, you can, for example, go to TextEdit, select the text you want to quote, then go to Services -> "name of your service", and your text should now be quoted!

This is how the automator Service looks like

Edit: I included the removal of trailing double quotation marks