我是applemans的新手,我正在尝试自动化一个进程,但是当目录中有空格时,如何通过脚本更改目录?我的命令应该是正确的,但语法错误不断弹出:
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
答案 0 :(得分:5)
有几种方法。
# 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
当你在整个路径上使用引号时,我也不会扩展tild。 因此,您需要以另一种方式获取用户名。
示例:
# 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