我已经写了一些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/"
领域。我怎样才能让它发挥作用?
P.S。为了我的目的,我必须得到路径 - 应用程序名称根本不会做。 (这是因为当我得到这个名字的时候 应用程序的过程中,我的一些应用程序是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
答案 0 :(得分:2)
application process
的元素System Events
有一个属性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
虽然launchedApplications
的{{1}}方法已被弃用,但它适用于Yosemite
在脚本库中使用AppleScriptObjC保存此代码
NSWorkspace
作为脚本包(在Mavericks中你必须在use framework "Foundation"
on launchedApplications()
return (current application's NSWorkspace's sharedWorkspace()'s launchedApplications()'s valueForKey:"NSApplicationPath") as list
end launchedApplications
中检查脚本侧栏中的“AppleScript / Objective-C库”)。如果该文件夹不存在,请创建该文件夹。
现在您可以从普通脚本文件调用脚本库(脚本库名为“NSWorkspace.scptd”)
~/Library/Script Libraries