Need Tool to Create Escaped String for AppleScript

Anyone know of a tool that will create an escaped string for use in AppleScript code?

What would be ideal is to use KM:
Select the string to be escaped
Run KM macro to create the escaped string and put on clipboard

For example, say I have a complex RegEx expression:

<\s*a\s+[^>]*href\s*=\s*[\"']?([^\"' >]+)[\"' >]

The tool would add the escape backslashes to output the string below that I need to use in AS code:

set strRegExPattern to "<\\s*a\\s+[^>]*href\\s*=\\s*[\\\"']?([^\\\"' >]+)[\\\"' >]"

Its really lucky you have Keyboard Maestro 7…

Except I don't YET have Ver 7. I've got to upgrade two systems to Yosemite first. :frowning:

But thanks. Any Ver 6 solutions?

Well, the sequence of replaces for AppleScript is pretty straight forward:

\ ➤ \\
" ➤ \"

Then add a " at the start and the end.

1 Like

Many thanks Peter. Works perfectly! :sunglasses:

Sorry I missed such an easy solution.
For some clouded reason I thought it was more complicated than that.

Thanks for taking the time to provide the actual code. :smile:

For anyone interested, here's my slightly modified version of @peternlewis' macro:

DOWNLOAD from here:
AS Convert String to Escaped Quoted String for AppleScript.kmmacros (4.7 KB)