escape character nightmare-passing AS variable to shell script

Here is the literal bit of HTML I need to parse through with my script; it needs to be set to a variable and passed to a shell script:

I won’t bore you with every detail of all I’ve tried; suffice to say: quoted form of, escaping every character, passing octal values, passing ascii characters of the entire string. Everything I’ve tried has the AS compiler returning extra escape characters to the variable inside the run environment.

I have noticed that the string OUTSIDE of the AS environment is correct, ie display dialog, or paste from pasteboard. But, inside the environment where I need it, I can’t seem to pass the correct string.

Is there no way to pass the string above in to a ‘do shell script’ variable inside AppleScript?


You need to escape the backslashes and the quote:

set AppleScriptText to "\\047\\\\)\">"


I was unable to find a robust solution for this issue. It was a catch 22: escaping the character in AS allowed AS to compile but broke the AWK command; And, every attempt to pass a working character to the AWK command (verified by pasting the command directly in terminal) failed AS compilation.

I was finally able to get my script working in this specific case just by luck: I inserted a character wildcard (.) in to the search string where the offending character existed. I was lucky in that there were no other occurrences of the string that matched with the wildcard character.

In the same way that backslashes have to be escaped in AppleScript code to represent backslash characters in text, they have to be escaped in a shell script to represent backslash characters in text mentioned within that shell script. If the shell script itself is represented as AppleScript text, the AppleScript code for it needs four backslashes for each backslash character in the text mentioned by the shell script: two for the character itself and two for the backslash which escapes it in the shell script. Without knowing exactly what you’re doing, I’d guess that what you need here is actually:

set AppleScriptText to "\\\\047\\\\\\\\)\">"

So, for example:

do shell script "echo '\\\\047\\\\\\\\)\">' " --> "\\047\\\\)\">" 
display dialog result


do shell script "osascript -e 'display dialog \"\\\\047\\\\\\\\)\\\">\" ' "