macos - 无法在 Applescript 中获取给定应用程序的 POSIX 路径

标签 macos types path applescript

I've written some Applescript to create a list of paths of all applications running in the 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 

But when it runs I get the error - "Can't make application into type constant" and it highlights path to application p.

I don't understand why this happens because when I run

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

I get no error whatsoever and I see "/System/Library/CoreServices/Finder.app/" returned in the Results field.

How can I get this to work?

P.S. For my purposes it is essential that I get the path - the application name simply won't do. (This is because when I get the name of the application process, some of my applications which are SSBs made using Fluid return "FluidApp" as their name instead of "Gmail" or "Tumblr" or whatever site it is that I've made into an application. I need to distinguish between these and that only happens when I get the path.)

Any help would be appreciated! Thanks.

更新:我使用了 @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

最佳答案

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

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

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

use script "NSWorkspace"

set appsRunning to launchedApplications() of script "NSWorkspace"

关于macos - 无法在 Applescript 中获取给定应用程序的 POSIX 路径,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31471645/

相关文章:

path - 转储包含来自 g++ 的路径

iphone - DTDevice 和 DTDeviceBase 私有(private)框架使用

python - 如何从 Python 中的文件中读取列表?

c# - 动态与对象类型

haskell - 这如何构成 Haskell 刚性类型错误?

types - 使用原因显示类型和值

python - 如何在 OS X 10.6.2 上实际构建 64 位 Python

java - 如何设置全屏vlcj直接渲染播放器?

java - 在 Java 6 中使用通配符解析绝对路径

python - 在终端中设置路径