gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 06:26:04 26 4
gpt4 key购买 nike

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

关于terminal - 如何在applescript终端中更改带有空格的目录?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14005362/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com