Quotes within quotes applescript

4

1

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."

DevRandom

Posted 2012-12-17T15:42:57.927

Reputation: 155

Answers

2

\"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"

adayzdone

Posted 2012-12-17T15:42:57.927

Reputation: 592

3

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.

nerdwaller

Posted 2012-12-17T15:42:57.927

Reputation: 13 366

1The output of that is "this is a 'quoted' text.". It didn't work... – DevRandom – 2012-12-17T16:05:40.820

Option 1 and 2 gives "this is a "quoted" text." and Option 3 gives not real quotes, "this is a 'quoted' text." – DevRandom – 2012-12-17T16:33:56.347

@ThePhone Understood, just quoting directly from the Apple Developer Docs. Hopefully someone can enlighten us on why that's not working. – nerdwaller – 2012-12-17T17:23:01.640

1quoted form of is meant for escaping strings for do shell script. – Lri – 2012-12-18T05:22:18.183

0

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!

DevRandom

Posted 2012-12-17T15:42:57.927

Reputation: 155

I respectfully disagree. Any string "result" in a developer tool should be shown as a literal, so that you know exactly what the output is. It would save a lot of headaches like this during debugging, when you have complex strings with lots of slashes and escapes, etc. For example, JavaScript's console.log( "\"abc\"") logs "abc", not \"abc\" or abc. – Beejor – 2019-05-07T18:09:46.090

1It's not a bug. AppleScript Editor just displays result strings with the AppleScript syntax. – Lri – 2012-12-18T09:21:06.317

0

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 & "\"" & " #'"

deek5

Posted 2012-12-17T15:42:57.927

Reputation: 1

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 – 2015-06-25T14:27:43.623