A Quick Hack to Quote Swift Strings in a Playground

Posted by on Dec 10, 2014 in Automator, Geekery, programming, Swift | One Comment
A Quick Hack to Quote Swift Strings in a Playground

Tonight, André Daniel asked a good question on Stack Overflow: does Swift have a “heredoc”-like syntax for saying, “no, really, these characters are exactly what I want in my string literal.”

Unfortunately, the answer is “no”. The same is true of Objective C. Previously I’ve worked around this limitation by putting my strings into string resources or plist files and loading them at runtime. However, this is more of a pain if you’re just playing around with things in a Swift playground.

So, it occurred to me that I could write a quick hack to make life easier, at least. Here it is: a very easy way of building your own OS X Services Menu item for proper Swift quoting of any text you happen to have selected, like a bunch of characters in a playground.

Making Services is child’s play. (Though if you already know how to make them, and you’re feeling a bit lazy, I’ve zipped up the workflow I created, and you can skip to the end of the post and download it instead…)

First, fire up Automator and create a new document. Choose the “Service” type:

Automator New Document Panel

We’re going to receive selected text, and our Automator output is going to replace the selected text, so check the little box:


Automator doesn’t have much built in to do string manipulation, so we’re going to rely on our venerable friend sed. Find “Run Shell Script” in Automator’s actions, and drag it to the main workflow window. Make sure “Pass input” is set to “to stdin”, which will take the selected text from our Service and pass it to the shell command.

Then, make this sed script the shell command:

sed -e 's/\([\\\"]\)/\\\1/g'

That regular expression matches backslashes or double quotes, and then we instruct sed to shove a backslash in front of either of them, which should be enough to escape Swift strings. Or Objective C strings, for that matter.

Sed Script Shell Command

Our output will fall out of Automator and replace the text we have selected without any further changes.

Okay, so, save the Service in Automator. It’ll prompt you for a name. I chose “Quote Swift String”:

Service Name

And you’re done. Told you it was easy. The Service is now available from the Services menu anywhere you can select text, including a Swift playground. Let’s test it. Fire up a playground, and write some code that prints a string:

MyPlayground playground Edited

Now paste in an unescaped string. If it’s got things that should be escaped — backslashes and double quotes — it’ll be a syntax error. I used one of André’s examples, and it certainly made the compiler unhappy. But we’ll soon fix that. Select the text of the string in the Playground:


And now use your shiny new service from the Xcode→Services menu:

Service Menu Item

Voilà! Your highlighted text has been replaced by a valid, escaped string!

Escaped String

Now, I knocked this up in half an hour, including writing the mini-tutorial, so there may be things I’ve missed. I’ve not tested it with non-ASCII characters, for example, so you’re probably out of luck if you need to paste emoji with your unquoted strings. But it does the basic job, and given the power of sed it should be easily adapted to cope with most string escaping needs. And hopefully it’ll make your life a bit easier if you find yourself pasting odd things into string literals in a playground…

If you don’t want to go through the short process above, here’s a zip file of the code. If you trust me, you can download it — it should just contain a .workflow file — and install it following the legendary Brett Terpstra’s instructions.

« Retrofitting | Burn Rate »

1 Comment

  1. Philip
    December 11, 2014

    I have £1,000 that says you can’t rewrite that in language your poor old dad might understand.