Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

jscooper22

macrumors 6502
Original poster
Feb 8, 2013
255
612
Syracuse, NY
Hi,

I swear this used to work.

For some reason, when I run this:

set test to "\"Hello\""

and expect this:

"Hello"

I get this:

\"Hello\"

How does one properly escape quotation marks?

Thanks,

Jeff
 
...
I get this:

\"Hello\"
Exactly where do you get this?

If it's in a Script Editor results window, then the editor is adding the backslashes so you know it's a string with quotes in it.

If it's output from the 'osascript' command, then osascript is escaping the quotes.

Be sure to post which OS version this is in.
 
I believe your correct that this was not always the case, and believe that this is a deliberate change in behaviour, or a bug.

But although "Script Editor" displays the text variable wrong, when used it does display correctly.
So try the examples below to see the results correctly displayed.

Code:
set test to "\"Hello\""

display dialog test

OR

Code:
set test to "\"Hello\""

set testEcho to do shell script "echo " & test

The escaping "\" character also displays wrong in Script Editor when trying to use the "\" character itself in text.
But again displays correctly when passed to other commands expecting text.

This used to work, but also displays incorrectly in Script Editor
Code:
set test to quote & "Hello" & quote

display dialog test

Also displays incorrectly in "Script Editor" but displays correctly when used in other commands.

It seems that the "\" character only works correctly when using tab, return and linefeed syntax, like below.

Code:
set test to "\"Hello\t World\"\r Hello Again!\n And Hello once again"

display dialog test

Although AppleScript has the "tab" and "return" text constants for this.

Don't know when this behavour change happened, but you can still work knowing the text will display correctly when used outside of the Script Editor application.

Regards Mark
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.