在AppleScript中执行Shell命令时出现空格问题


12

以下代码以递归方式清除目录不起作用,因为我无法通过“ 应用程序支持”文件夹。

try
    do shell script "rm -r ~/Library/Application\ Support/Google/Chrome/Default/Pepper Data/Shockwave Flash"
end try
  • 如果我~/Library/Application Support/Google...什么也没做。这是预期的。
  • 如果使用~/Library/Application\ Support/Google...错误消息,我将无法保存脚本:Syntax Error Expected “"” but found unknown token.

如何解决此错误?

Answers:


10

尝试:

set pathwithSpaces to "/Users/John/Desktop/This is a test.docx"
do shell script "rm -r " & quoted form of pathwithSpaces

8

您必须将反斜杠加倍,并且还要避开其他空格:

do shell script "rm -r ~/Library/Application\\ Support/Google/Chrome/Default/Pepper\\ Data/Shockwave\\ Flash"

或以其他方式逃避路径:

do shell script "rm -r ~/'Library/Application Support/Google/Chrome/Default/Pepper Data/Shockwave Flash'"

quoted form of替换''\''并将字符串用单引号引起来,因此它不适用于以开头的路径~/


By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.