5

我是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
4

1 回答 1

7

有几种方法。

# 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
于 2012-12-22T18:49:43.303 回答