【问题标题】:Using quotes in a AppleScript string在 AppleScript 字符串中使用引号
【发布时间】:2012-05-19 18:23:39
【问题描述】:

我正在使用 AppleScript,需要这样做:

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

如您所见,我需要制作成字符串的文本中包含引号。如何设置

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

到没有引号的 AppleScript 字符串?

【问题讨论】:

  • 我的答案是below。您的bash 命令可以简化为cd "${0%/*}" && java <params>,顺便说一句。没有外部因素和一点点流量控制。

标签: applescript


【解决方案1】:

要在 Applescript 字符串中插入文字引号,您必须对它们进行转义,即

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

AppleScript 与大多数语言具有相同的约定,即使用反斜杠来转义特殊字符,其中只有两个:引号和 ... 反斜杠。请参阅section “Special string characters” of the AppleScript Language Guide

【讨论】:

  • 另外请注意,如果将 AppleScript 字符串中的引号传递给 shell,那么在使用引号时会有很多问题,其中大部分与 shell 如何处理转义和非转义引号有关。有关更多详细信息,请参阅this question
【解决方案2】:

也可以使用以下语法:

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

【讨论】:

  • 这给了我单引号
【解决方案3】:

(dirname as POSIX path)的引用形式

【讨论】:

    【解决方案4】:
    set x to "He said   \" Enter the matrix.\"   "display dialog x
    

    只需将其复制到applescript中即可,这是最容易理解的方式。

    【讨论】:

    • 这个问题3岁了!你的答案比@kopischke 的好多少?
    【解决方案5】:

    在applescript中使用引号非常简单,您只需要将行结束并以引号开始

    E.G

    显示对话框“hello world”

    但是当你决定在文本中放置一个变量时,你必须使用 &

    将 my_name 设置为“michael”

    显示对话框“你好”和我的名字

    谢谢

    【讨论】:

    • 这个问题与在字符串中使用引号 within 相关(即转义它们)。这只是创建一个标准字符串。
    • 哦,对不起,我没有意识到你想摆脱字符串来做到这一点,你必须使用简单的“&”符号,如果使用
    猜你喜欢
    • 2010-12-15
    • 2011-04-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多