在 AppleScript 中的文本替换中使用反斜杠

Using backslashes in text replacement in AppleScript

我目前正在尝试创建一个 AppleScript 应用程序,它将一个文件夹从其 "Contents" 文件夹复制到用户的桌面。我现在的代码是:

set theFile to path to me as string
set hfsme to theFile & "Contents:Folder"
set posixPath to POSIX path of hfsme

set contentfolder to posixPath
set AppleScript's text item delimiters to " "
set textItem to text items of contentfolder
set AppleScript's text item delimiters to "\ "
set contentfolder to textItem as string
set AppleScript's text item delimiters to {""}

do shell script "cp -a " & contentfolder & " $HOME/Desktop"

但是,我在 运行 时收到以下错误:

"cp: /Users/myusername/Desktop/Test Application.app/Contents/Folder: No such file or directory"

我认为这是因为我希望复制位置是:

/Users/myusername/Desktop/Test\ Application.app/Contents/Folder

但是脚本无法在 space 之前添加反斜杠,而这正是 shell 命令所要求的。

我的问题是:如何用“\”替换变量中的space?

P.S。在建议简单地将应用程序重命名为不包含 space 的名称之前,拥有一个包含 space 的应用程序名称对我来说非常重要。 我也尝试过将 "duplicate" 命令与 AppleScript 一起使用,但无法使其正常工作,但我愿意接受建议! (请记住,我希望此脚本也能在其他人的计算机上运行,​​这意味着我不能假设我已经知道用户的用户名。

提前感谢您的帮助!

此致,汤姆

只需添加 quoted form of,它就会以非常智能(可靠)的方式处理报价。

set theFile to POSIX path of (path to me)
set contentfolder to theFile & "Contents/Folder"
do shell script "cp -a " & quoted form of contentfolder & " $HOME/Desktop"

如果do shell script不支持$HOME写入

do shell script "cp -a " & quoted form of contentfolder & space & quoted from of POSIX path of (path to desktop)