Applescript shell 脚本到 space 文件夹

Applescript shell script to folder with space

我正在尝试将文本文件写入名称中包含 space 的文件夹。例如:文件夹 2

写入文件夹的第一个脚本工作正常 写入文件夹 2 的第二个脚本无法正常工作。

编辑: 如果我对路径进行硬编码,我尝试在路径周围使用单引号并且它有效。有没有办法在不丢失 ~ 符号的情况下写入文件夹?

    --this works
   do shell script "echo " & "the text" & " >> ~/Desktop/folder/text.txt"

    --this does not work
   do shell script "echo " & "the text" & " >> ~/Desktop/folder 2/text.txt"

这会起作用:

do shell script "echo " & "the text" & " >> ~/Desktop/folder\ 2/text.txt"

注意:使用~时,与$HOME相同,在do shell script中有一个space 路径名 中,使用双反斜杠 \ 转义 space 并且不要引用 路径名 包含一个~。在 bash 中,space 只需要一个反斜杠 \ 来转义 space.

这也有效:

do shell script "echo " & "the text" & " >> \"$HOME/Desktop/folder 2/text.txt\""