Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I am doing an applescript that is supposed to set an command with quotes an variable. This is the code so far:

set myString to "This is a "quoted" text."

But an error occurs because applescript doesn't allow quotes within quotes. I have tried to take backslash before each quotes but then the output is:

"This is a \"quoted\" text."
share|improve this question
up vote 2 down vote accepted

\"my quoted text\" is the proper way to wrap text in double quotes. Run the script below and open example.txt to see the result.

set xxx to "This is a \"quoted\" text."
do shell script "echo " & quoted form of xxx & " > ~/Desktop/example.txt"
share|improve this answer

Directly from Apple, it says you can use the escape:

Option 1:

Special String Characters

The backslash (\) and double-quote (") characters have special meaning
in text. AppleScript encloses text in double-quote characters and uses
the backslash character to represent return (\r), tab (\t), and
linefeed (\n) characters (described below). So if you want to include
an actual backslash or double-quote character in a text object, you
must use the equivalent two-character sequence. As a convenience,
AppleScript also provides the text constant quote, which has the value

Table 6-1  Special characters in text Character To insert in text
Backslash character (\) \\ Double quote (") \" quote (text constant)

Option 2

On that same page, Apple says you can use quote to insert \" instead:

set myString to "this is a " & quote & "quoted" & quote & " text."

Option 3

Another option found is:

set inString to "quoted"
set myString to "this is a " & quoted form of inString & " text."

Edit: Though, you said that last one outputs this is 'quoted' text.

share|improve this answer
The output of that is "this is a 'quoted' text.". It didn't work... – DevRandom Dec 17 '12 at 16:05
Option 1 and 2 gives "this is a \"quoted\" text." and Option 3 gives not real quotes, "this is a 'quoted' text." – DevRandom Dec 17 '12 at 16:33
@ThePhone Understood, just quoting directly from the Apple Developer Docs. Hopefully someone can enlighten us on why that's not working. – nerdwaller Dec 17 '12 at 17:23
quoted form of is meant for escaping strings for do shell script. – user495470 Dec 18 '12 at 5:22

other way for quoted text in applescript :

set input to "with quote"

set input to do shell script "echo " & quoted form of (quote & input & quote)

echo '\"with quote\"'

or more long

set input to do shell script "echo " & input & " | sed 's#" & input & "#" & "\"" & input & "\"" & " #'"

share|improve this answer
Without proper formatting it's hard to tell which lines are commands and which are comments. Could you please edit your answer? (Hint: start commands with four spaces to get them formatted as such) – Dmitry Grigoryev Jun 25 '15 at 14:27

There is a bug in applescript, if you run the command return TestVarible the output is with backslashes. But, the final command is correct:

display dialog TestVariable Output "This is a "quoted" text."

Thanks for all answers!

share|improve this answer
It's not a bug. AppleScript Editor just displays result strings with the AppleScript syntax. – user495470 Dec 18 '12 at 9:21

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .