【问题标题】:Can't get POSIX Path of given application in Applescript无法在 Applescript 中获取给定应用程序的 POSIX 路径
【发布时间】:2015-10-06 22:16:37
【问题描述】:

我编写了一些 Applescript 来创建所有路径的列表 在 Dock 中运行的应用程序:

set appsRunning to []

tell application "System Events"
    repeat with p in every application process
        if background only of p is false then
            set appsRunning to appsRunning & POSIX path of (path to application p)
        end if    
    end repeat
end tell 

但是当它运行时我得到 错误 - “无法将应用程序转换为类型常量”,它 突出显示path to application p

我不明白为什么会这样,因为当我跑步时

set q to POSIX path of (path to application "Finder") -- or any other application  

我没有收到任何错误,我明白了 "/System/Library/CoreServices/Finder.app/" 在结果中返回 场地。

我怎样才能让它工作?

附:就我的目的而言,获得路径是至关重要的—— 应用程序名称根本行不通。 (这是因为当我得到名字 申请过程中,我的一些申请是 SSB 使用Fluid 返回"FluidApp" 作为他们的 名称而不是 "Gmail""Tumblr" 或任何网站 我已经做成了一个应用程序。我需要区分这些和 只有当我得到路径时才会发生这种情况。)

任何帮助将不胜感激!谢谢。

更新:我使用了@vadian's answer中第一个建议的修正版本来解决我的问题:

set appsRunning to {}

tell application "System Events"
    repeat with aProcess in (get application file of every application process whose background only is false)
        set appsRunning to appsRunning & POSIX path of aProcess
    end repeat
end tell

【问题讨论】:

    标签: macos types path applescript


    【解决方案1】:

    System Events的元素application process有一个属性application file,可以直接从中获取POSIX路径。

    set appsRunning to {}
    
    tell application "System Events"
        repeat with aProcess in (get every application process whose background only is false)
            set end of appsRunning to POSIX path of application file of aProcess
        end repeat
    end tell
    

    或者更简单

    tell application "System Events"
        set appsRunning to POSIX path of application file of every application process whose background only is false
    end tell
    

    这里还有一个解决方案,它排除了 Finder,因为它一直在运行并且路径是固定的

    tell application "System Events"
        set appsRunning to POSIX path of application file of every application process whose background only is false and name is not "Finder"
    end tell
    set end of appsRunning to "/System/Library/CoreServices/Finder.app"
    

    使用原始方法的另一种解决方案

    set appsRunning to {}
    
    tell application "System Events"
        set applicationNames to get name of every application process whose background only is false
    end tell
    repeat with aName in applicationNames
        set end of appsRunning to POSIX path of (path to application aName)
    end repeat
    

    最后但同样重要的是 AppleScriptObjC 版本(Mavericks 及更高版本,在 Mavericks 中仅在脚本库中)

    set appsRunning to (current application's NSWorkspace's sharedWorkspace()'s launchedApplications()'s valueForKey:"NSApplicationPath") as list
    

    虽然 NSWorkspace 的方法 launchedApplications 已被弃用,但它在 Yosemite 中有效

    要在脚本库中使用 AppleScriptObjC,请保存此代码

    use framework "Foundation"
    
    on launchedApplications()
        return (current application's NSWorkspace's sharedWorkspace()'s launchedApplications()'s valueForKey:"NSApplicationPath") as list
    end launchedApplications
    

    ~/Library/Script Libraries 中作为脚本包(在 Mavericks 中,您必须检查脚本侧栏中的“AppleScript/Objective-C 库”)。如果文件夹不存在,则创建该文件夹。

    现在您可以从普通脚本文件调用脚本库(脚本库名为“NSWorkspace.scptd”)

    use script "NSWorkspace"
    
    set appsRunning to launchedApplications() of script "NSWorkspace"
    

    【讨论】:

    • 太棒了!非常感谢。
    • 不客气。如果您不介意,请接受答案
    • 我只是想先将它添加到我的代码中,然后检查它是否与其余部分一起正常运行。但我现在会接受你的回答,如果我遇到任何麻烦,我会回来。
    • 嗯,它似乎不起作用。我收到错误Can’t get POSIX path of application file of application process "Finder".
    • 我运行脚本十多次没有任何问题。但是您可以排除 Finder,因为它的路径无论如何都是众所周知的,我编辑了帖子以添加相应的解决方案
    猜你喜欢
    • 1970-01-01
    • 2018-01-07
    • 1970-01-01
    • 1970-01-01
    • 2014-07-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多