2 Replies Latest reply: Jun 18, 2010 12:02 AM by taylor.henderson
taylor.henderson Level 2 Level 2 (450 points)
Hi all.

I can run the following in a shell script and escape the ampersand with a backslash

*open -a Firefox http://www.aSite.org/data.php?email=myName88%40live.com\&passwd=myPass*

In applescript the following gives me a syntax error. _Expected """ but found unknown token_

*do shell script "open -a Firefox http://www.aSite.org/data.php?email=myName88%40live.com\&passwd=myPass"*

Attempting to use "quoted form of" has also not worked. I though "\" escaped any character after it? Am I missing something here? Thanks in advance.

applebook, Mac OS X (10.5.8), the white one
  • Camelot Level 8 Level 8 (46,120 points)
    This is a common issue.

    Both the shell and AppleScript use the backslash to escape characters - in the shell's case it tells the shell to pass the next character as-is.

    The issue you're running into is that AppleScript is jumping in and seeing the slash, which it also interprets as 'pass the next character as-is', so it strips the slash and passes the ampersand to the shell which, of course, treats it as it normally would since it is no longer escaped.

    The solution is simple, though - double-escape the ampersand. The first slash will be interpreted by AppleScript to tell it to pass the next character (the second slash) to the shell. The shell then sees a single backslash which it interprets as an escape of the ampersand. Voila!

    do shell script "open -a Firefox http://www.aSite.org/data.php?email=myName88%40live.com\\&passwd =myPass"

    (Note that if Firefox is your default web browser you can just use the simpler form:

    open location "http://www.aSite.org/data.php?email=myName88%40live.com&passwd=myPass"

    which avoids the whole escaping issue altogether.)

    Message was edited by: Camelot
  • taylor.henderson Level 2 Level 2 (450 points)
    Thank you. I had attempted that solution before, but because it was applescript giving me the error and not the shell I quickly dismissed it when it didn't work. Looking back on my code now I see I only ever tried to double escape with forward slashes by mistake :S

    Thanks again though.