【问题标题】:AppleScript Finder "open using" issueAppleScript Finder“打开使用”问题
【发布时间】:2014-05-02 11:06:27
【问题描述】:

我在编写脚本时遇到了一些麻烦(我认为这会是)我正在处理的一个非常简单的部分。基本上,我想告诉 Finder 使用特定应用程序打开文件。很简单,对吧?根据我的阅读,我应该可以使用:

tell application "Finder"
    open "the_file" using "the_application"
end tell

问题在于 Finder 似乎很难找到应用程序。当我使用以下代码时:

set webArcExtract to POSIX file (do shell script "mdfind 'WebArchive Folderizer.app' -onlyin '/Applications/'") as string #Find the Web Archive Extraction Program

tell application "Finder" #Temporary path to save the web archive
    set tempPath to ((home as string) & "temp:")
end tell

tell application "Fake" #Magic that saves a webpage as a webarchive
    load URL "www.google.com"
    delay 3
    capture web page as Web Archive saving in tempPath & "arc.webarchive"
end tell

tell application "Finder" #Open the arc.webarchive file saved in the tempPath with the WebArchive Folderizer application
    open tempPath & "arc.webarchive" using webArcExtract
end tell

变量的值如下:

tempPath: "OSX_Data:Users:user:" webArcExtract: "OSX:Applications:Utilities:WebArchive Folderizer.app"

我在尝试运行代码时遇到的错误发生在 open tempPath & "arc.webarchive" using webArcExtract 行上。 Finder 会弹出一条消息,指出“找不到应用程序”。

我真的被这个弄糊涂了。我知道路径是正确的,我知道应用程序可以通过这种方式打开文件。我可以使用 Finder 转到我尝试打开的 arc.webarchive 文件,右键单击该文件,然后选择“使用 > WebArchive 文件夹打开”,它可以完美运行。

有什么建议吗?

【问题讨论】:

    标签: applescript using finder tell


    【解决方案1】:

    这里有几个建议。 1) 获取应用程序路径的一种更简单的方法是只使用 applescript 的“path to”命令,这样这样的东西应该可以工作......

    set theFile to (path to desktop as text) & "a.txt"
    set appPath to path to application "TextWrangler"
    tell application "Finder" to open file theFile using appPath
    

    2) tempPath 不需要 Finder,再次使用“path to”...

    set tempPath to (path to home folder as text) & "temp:"
    

    3) 最后你需要一个文件说明符,所以在文件路径之前添加关键字“file”,就像我在 #1 中所做的那样......

    tell application "Finder"
        open file (tempPath & "arc.webarchive") using webArcExtract
    end tell
    

    希望对你有帮助。

    【讨论】:

    • 噢!我知道这很简单。缺少“文件”名词。 (facepalm) 现在我觉得自己像个白痴。也感谢有关“路径”技巧的提示。超级好用。我会一直用那个。我认为有更好的方法来做到这一点。
    • +1;值得指出的一个微妙之处:path to 实际上并没有返回一个 path string,正如人们可能怀疑的那样,而是一个指向文件的 alias object;要将此对象转换为 HFS 格式的路径字符串,请使用 appPath as text(如答案所示),要获取 POSIX 格式的路径字符串,请使用 POSIX path of appPath
    猜你喜欢
    • 1970-01-01
    • 2012-06-30
    • 2023-03-16
    • 1970-01-01
    • 2014-06-12
    • 1970-01-01
    • 1970-01-01
    • 2010-09-23
    • 1970-01-01
    相关资源
    最近更新 更多