【问题标题】:How do I test for a folder existing in Apple Script?如何测试 Apple Script 中存在的文件夹?
【发布时间】:2020-01-12 23:46:31
【问题描述】:

如果桌面文件夹存在,我正在尝试在终端中运行“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/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

我对苹果脚本很陌生,所以我不知道为什么在保存/编译时会调用语法错误。谁能帮忙???谢谢。

【问题讨论】:

  • .......

标签: directory applescript


【解决方案1】:

AppleScript 是一种相当简单的语言,它依赖于其他应用程序来执行它不知道的事情。

您收到错误是因为 Terminal 不知道术语 folder 是什么。 FinderSystem Events 之类的东西可用于处理文件对象,您还可以强制指定别名的路径,如果不存在则会出错 - 请注意在这种情况下,您对术语 exists 的使用来自标准套件,该套件用于一般对象,因此它将返回 true,因为字符串确实存在。

另请注意,除非另有说明,否则每个 do script 语句都将位于其自己的窗口中。我认为您正在寻找类似的东西:

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但是路径,它使用冒号作为分隔符并且还包括卷名。根据您使用的内容(Finder 只知道 HFS 路径),可能需要在两者之间进行强制转换。
猜你喜欢
  • 1970-01-01
  • 2020-04-08
  • 2016-01-27
  • 2013-04-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-05-17
  • 1970-01-01
相关资源
最近更新 更多