Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

Escaping ampersand

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

Posted on Jun 17, 2010 10:28 PM

Reply
Question marked as Best reply

Posted on Jun 17, 2010 11:59 PM

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
2 replies
Question marked as Best reply

Jun 17, 2010 11:59 PM in response to taylor.henderson

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

Escaping ampersand

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple ID.