Directory 如何测试Apple脚本中存在的文件夹?
如果桌面文件夹存在,我正在尝试在终端中运行“cd Desktop”。这是我目前的代码:Directory 如何测试Apple脚本中存在的文件夹?,directory,applescript,Directory,Applescript,如果桌面文件夹存在,我正在尝试在终端中运行“cd Desktop”。这是我目前的代码: tell application "Terminal" activate try if exists folder "Desktop" then do script "cd Desktop" in tab 1 of window 1 end if do script "java -jar /Users/Harry/Desk
tell application "Terminal"
activate
try
if exists folder "Desktop" then
do script "cd Desktop" in tab 1 of window 1
end if
do script "java -jar /Users/Harry/Desktop/Candle.app/Contents/candle.jar" in tab 1 of window 1
on error
if exists folder "Desktop" then
do script "cd Desktop"
end if
do script "java -jar /Users/Harry/Desktop/Candle.app/Contents/candle.jar"
end try
end tell
我对apple脚本非常陌生,所以我不知道为什么在保存/编译时会调用语法错误。有人能帮忙吗???谢谢。AppleScript是一种相当简单的语言,它依赖其他应用程序来执行它不知道的内容 您得到错误是因为终端不知道术语
文件夹是什么。类似于Finder或System Events的内容可用于处理文件对象,您还可以强制别名的路径,如果别名不存在,则会出错-请注意,在这种情况下,您对术语exists
的使用将来自标准套件,该套件适用于一般对象,因此它将返回true,因为字符串确实存在
另外请注意,除非另有规定,否则每个do脚本
语句都将在其自己的窗口中。我认为您正在寻找更像:
set theFolder to "/Users/you/Desktop"
tell application "Finder" to set folderExists to exists (folder theFolder as POSIX file)
if folderExists then
tell application "Terminal"
activate
try
do script "cd Desktop" in tab 1 of window 1
on error errmess
log errmess
do script "cd Desktop"
end try
delay 0.05
do script "echo testing" in tab 1 of window 1
end tell
else
beep
end if
啊,我现在明白了!POSIX是什么?POSIX路径,例如我的示例中使用的路径,是Unix-y路径,使用斜杠作为分隔符,终端使用它。Finder和操作系统的几个部分使用HFS路径,它们使用冒号作为分隔符,还包括卷名。根据您使用的内容(查找程序只知道HFS路径),可能需要在两者之间进行强制。