Terminal 如何在applescript终端中更改带有空格的目录?

Terminal 如何在applescript终端中更改带有空格的目录?,terminal,applescript,iphoto,Terminal,Applescript,Iphoto,我是applescripts新手,我正在尝试自动化一个过程,但是当目录中有空格时,如何通过脚本更改目录?我的命令应该正确,但语法错误不断出现: Expected “"” but found unknown token. 这是我的剧本: tell application "Terminal" activate do script "cd ~/Pictures/iPhoto\ Library" end tell 我不明白哪里错了。它在我的终端上运行良好 谢谢大家 更新:这是最有效的 # surr

我是applescripts新手,我正在尝试自动化一个过程,但是当目录中有空格时,如何通过脚本更改目录?我的命令应该正确,但语法错误不断出现:

Expected “"” but found unknown token.
这是我的剧本:

tell application "Terminal"
activate
do script "cd ~/Pictures/iPhoto\ Library"
end tell
我不明白哪里错了。它在我的终端上运行良好

谢谢大家

更新:这是最有效的

# surround in single quotes
tell application "Terminal"
    activate
    do script "cd  '/Users/username/Pictures/iPhoto Library'"
end tell
问题有几个方面

# escape the quotes with a backslash. AND Escape the first backslash for Applescript to accept it.
tell application "Terminal"
    activate
    do script "cd ~/Pictures/iPhoto\\ Library"
end tell

# surround in double quotes and escape the quotes with a backslash. 
tell application "Terminal"
    activate
    do script "cd \"/Users/username/Pictures/iPhoto Library\""
end tell

# surround in single quotes using quoted form of 
tell application "Terminal"
    activate
    do script "cd " & quoted form of "/Users/username/Pictures/iPhoto Library"
end tell
# surround in single quotes
tell application "Terminal"
    activate
    do script "cd  '/Users/username/Pictures/iPhoto Library'"
end tell
另外,我不认为当你在整个路径上使用引号时,平铺会扩展。 因此,您需要以另一种方式获取用户名

示例:

# inserting the user name. And surrond in brackets so the name and path are seen as one string before the quotes are added
set whoami to do shell script "/usr/bin/whoami"
tell application "Terminal"
    activate
    do script "cd /Users/" & quoted form of whoami & "/Pictures/iPhoto\\ Library"
end tell



tell application "System Events" to set whoami to name of current user
# inserting the user name. And surrond in brackets so the name and path are seen as one string before the quotes are added
tell application "Terminal"
    activate
    do script "cd /Users/" & quoted form of (whoami & "/Pictures/iPhoto Library")
end tell
正如你所看到的,有不止一种方法可以做到这一点

或者只引用目录部分

例如

tell application "Terminal"
    activate
    do script "cd ~" & quoted form of "/Pictures/iPhoto Library"
end tell

如果我再次执行“编写脚本…”。它将打开另一个终端。执行另一行代码的命令是什么:对于*.db中的dbase;执行sqlite3$dbase“vacuum;”;完成??你用分号分解命令“;“我在Applescript中不经常这样做,也不知道这样做的方法,但结果可能不是你所期望的。所以下面的内容是正确的<代码>告诉应用程序“终端”激活do脚本“cd'/Users/raymondlam/Pictures/iPhoto Library';echo'#Running iPhotoVaccum';对于*.db中的dbase;do sqlite3$dbase'vacuum;';done“end tell应该有效地工作。但正如我所说,我并不经常使用它。另外,最好创建一个bash脚本作为可执行文件。然后就这么说吧。