18

I am working with AppleScript and need to do this:

set TextToWrite to " #!/bin/bash cd "$( dirname "$0" )" java -server -Xmx4G -jar ./craftbukkit.jar" "

As you can see, the text I need to make into a string has quotes in it. How do I set

#!/bin/bash cd "$( dirname "$0" )" java -server -Xmx4G -jar ./craftbukkit.jar"

to an AppleScript string without the quotes messing it up?

kopischke
  • 3,393
  • 1
  • 21
  • 40
hawkfalcon
  • 652
  • 1
  • 12
  • 24
  • 1
    My answer is [below](http://stackoverflow.com/a/10668503/990363). Your `bash` command could be simplified to `cd "${0%/*}" && java `, BTW. No externals and a modicum of flow control. – kopischke May 19 '12 at 20:14

5 Answers5

26

To insert literal quotes into an Applescript string, you have to escape them, i.e.

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

AppleScript has the same convention as most languages, which is to use a backslash for escaping of special characters, of which there are only two: quotes and … backslash. See the section “Special string characters” of the AppleScript Language Guide.

kopischke
  • 3,393
  • 1
  • 21
  • 40
  • 3
    Also note there are quite a few gotchas to the use of quotes in AppleScript strings if these are passed to the shell, most of them having to do with how the shell processes escaped and unescaped quotes. See [this question](http://stackoverflow.com/questions/8138167/how-can-i-escape-shell-arguments-in-applescript) for some more details. – kopischke May 19 '12 at 20:36
3

The following syntax can also be used:

set aString to "quoted"
set myString2 to "This is a " & quoted form of aString & " text."
0

quoted form of (dirname as POSIX path)

llange
  • 757
  • 2
  • 10
  • 14
0
set x to "He said   \" Enter the matrix.\"   "display dialog x

Just copy this into applescript the easiest way to understand.

Tunaki
  • 132,869
  • 46
  • 340
  • 423
-3

Using quotes in applescript is quite easy you just need to make the line end and start in quotes

E.G

display dialog "hello world"

but when you decide to put a variable in the text you must use &

set my_name to "michael"

display dialog "hello" & my_name

thankyou