引号内的引号 applescript

引号内的引号 applescript

我正在编写一个 applescript,它应该设置一个带引号的命令和一个变量。这是目前的代码:

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

但是会出现错误,因为 applescript 不允许在引号内使用引号。我尝试在每个引号前加上反斜杠,但输出结果如下:

"This is a \"quoted\" text."

答案1

\"my quoted text\" 是将文本括在双引号中的正确方法。运行以下脚本并打开 example.txt 查看结果。

set xxx to "This is a \"quoted\" text."
do shell script "echo " & quoted form of xxx & " > ~/Desktop/example.txt"

答案2

Apple 直接表示,你可以使用转义符:

选项1:

特殊字符串字符

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)

选项 2

在同一页面上,Apple 表示你可以使用quote插入来\"代替:

set myString to "this is a " & quote & "quoted" & quote & " text."

选项 3

发现的另一个选项是:

set inString to "quoted"
set myString to "this is a " & quoted form of inString & " text."

编辑:不过,您说最后一个输出this is 'quoted' text

答案3

applescript 中有一个错误,如果你运行该命令,return TestVarible 输出会带有反斜杠。但是,最终的命令是正确的:

display dialog TestVariable输出"This is a "quoted" text."

感谢所有回答!

答案4

在 applescript 中引用文本的另一种方法:

将输入设置为“带引号”

设置输入以执行 shell 脚本“echo ” & 引用形式 (quote & input & quote)

echo '\"带引号\"'

或更长

设置输入以执行 shell 脚本“echo ”&input&“|sed's#"&input&“#”&“\”"&input&“\”"&“#'”

相关内容