无法在 Applescript 中获取给定应用程序的 POSIX 路径
Can't get POSIX Path of given application in 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.
更新: 我使用了 中第一个建议的修改版本来解决我的问题:
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")在 ~/Library/Script Libraries
中。如果文件夹不存在,请创建。
现在可以从普通脚本文件中调用脚本库(脚本库命名为"NSWorkspace.scptd")
use script "NSWorkspace"
set appsRunning to launchedApplications() of script "NSWorkspace"
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.
更新: 我使用了
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")在 ~/Library/Script Libraries
中。如果文件夹不存在,请创建。
现在可以从普通脚本文件中调用脚本库(脚本库命名为"NSWorkspace.scptd")
use script "NSWorkspace"
set appsRunning to launchedApplications() of script "NSWorkspace"