MaxB
MaxB

Reputation: 127

How do I double quote a string in applescript?

Let s = my string

Something like:

set s to quoted form of s

gives 'my string'

Which is only single quotes around the string and something like:

set s to ("\"" & s & "\"")

gives \"my string\"

Which adds the backslashes to the string for some reason.

Upvotes: 0

Views: 1613

Answers (1)

McUsr
McUsr

Reputation: 1409

If you display the string in your last example, you'll see that the script indeed has gotten double quotes around them.

The reason the double quotes are shown with backslashes internally, is that AppleScript needs to make a difference between the literal backslashes, is that you use double quotes, for say assigning a string to a variable. So usual double quotes has a distinct purpose in AppleScript, and it needs to tell a difference between those, and the literal double qoutes that you want to use in any variables. When your literal double quotes are put to display, then they are shown as usual double qoutes, also if they are written into a text file for instance, that you open with an editor, then you'll realize, that AppleScript perform escaping "behind the scenes", into an internal representation, that makes the data work with the conventions of AppleScript, so that any data doesn't break the script, when the script compiles first and foremost.

Try using the code below to display it.

tell application (path to frontmost application as text)
    display alert s
end tell

Then you'll realize that you have gotten "normal" double qoutes in your string for display.

Upvotes: 1

Related Questions