Reputation: 25
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.
Update: I used an amended version of the first suggestion in @vadian's answer to solve my problem:
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
Upvotes: 2
Views: 2319
Reputation: 285059
The element application process
of System Events
has a property application file
, which you can get the POSIX path directly from.
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
or easier
tell application "System Events"
set appsRunning to POSIX path of application file of every application process whose background only is false
end tell
additional here a solution which excludes the Finder because it runs all the time and the path is fixed
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"
another solution using your original approach
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
and last but not least the AppleScriptObjC version (Mavericks and higher, in Mavericks only in a script library)
set appsRunning to (current application's NSWorkspace's sharedWorkspace()'s launchedApplications()'s valueForKey:"NSApplicationPath") as list
Though the method launchedApplications
of NSWorkspace
is deprecated, it works in Yosemite
to use the AppleScriptObjC in a script library save this code
use framework "Foundation"
on launchedApplications()
return (current application's NSWorkspace's sharedWorkspace()'s launchedApplications()'s valueForKey:"NSApplicationPath") as list
end launchedApplications
as script bundle (in Mavericks you have to check "AppleScript/Objective-C library" in the side bar of the script) in ~/Library/Script Libraries
. Create the folder if it doesn't exist.
Now you can call the script library from a normal script file (the script library is named "NSWorkspace.scptd")
use script "NSWorkspace"
set appsRunning to launchedApplications() of script "NSWorkspace"
Upvotes: 2