Thursday, December 14, 2017

#1 2017-11-12 10:49:01 am

cudaboy_71
Member
Registered: 2014-03-04
Posts: 20

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:

\047\\)">


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?

Last edited by cudaboy_71 (2017-11-12 10:53:07 am)


Filed under: , applescript

Offline

 

#2 2017-11-12 11:46:46 am

Nigel Garvey
Moderator
From:: Warwickshire, England
Registered: 2002-11-20
Posts: 4453

Re: escape character nightmare-passing AS variable to shell script

Hi.

You need to escape the backslashes and the quote:

Applescript:

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


NG

Online

 

#3 2017-11-18 08:44:06 am

cudaboy_71
Member
Registered: 2014-03-04
Posts: 20

Re: escape character nightmare-passing AS variable to shell script

update:

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.

Offline

 

#4 2017-11-18 10:09:57 am

Nigel Garvey
Moderator
From:: Warwickshire, England
Registered: 2002-11-20
Posts: 4453

Re: escape character nightmare-passing AS variable to shell script

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:

Applescript:

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

So, for example:

Applescript:

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

Or:

Applescript:

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

Last edited by Nigel Garvey (2017-11-18 10:44:36 am)


NG

Online

 

Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)