Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I am doing an applescript that is supposed to set an command with quotes an varible. 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

3 Answers 3

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... –  The Phone 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." –  The Phone 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
1  
quoted form of is meant for escaping strings for do shell script. –  ؘؘؘ Dec 18 '12 at 5:22

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
1  
It's not a bug. AppleScript Editor just displays result strings with the AppleScript syntax. –  ؘؘؘ Dec 18 '12 at 9:21

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.