2

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
Community
  • 1
  • 1

1 Answers1

2

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"
vadian
  • 274,689
  • 30
  • 353
  • 361
  • Excellent! Thank you very much. – Oliver Baum Jul 17 '15 at 09:14
  • you're welcome. If you don't mind, accept the answer – vadian Jul 17 '15 at 09:18
  • I just wanted to add it to my code first and check it runs properly with the rest of it. But I'll accept your answer now and if I run into any trouble I'll come back. – Oliver Baum Jul 17 '15 at 09:34
  • Hmm, it doesn't seem to be working. I'm getting the error `Can’t get POSIX path of application file of application process "Finder".` – Oliver Baum Jul 17 '15 at 09:54
  • I ran the script more than a dozen times without any problems. But you could exclude the Finder because its path is well-known anyway, I edited the post to add the respective solution – vadian Jul 17 '15 at 09:55
  • And while the second one doesn't bring up an error. It just returns `{missing value, missing value, ... missing value}` :/ – Oliver Baum Jul 17 '15 at 10:03
  • How are you running the scripts? I'm using Applescript Editor. Have you tried running from there? I'm also running OSX 10.9. Will this have an impact on it? – Oliver Baum Jul 17 '15 at 10:06
  • I'm using 10.10 and tested all scripts in Script Editor and Script Debugger. But the property `application file` exists also in Mavericks. Try `file` instead of `application file`. I added another solution based on yours – vadian Jul 17 '15 at 10:12
  • It does work somewhat. It timed out a couple of times. Although it doesn't really help because the whole purpose of this is to avoid using Application names since apps made with Fluid show up as "FluidApp". Thanks for your help though. It seems odd that the initial code works for you and not for me. – Oliver Baum Jul 17 '15 at 10:30
  • maybe the AppleScriptObjC version is more suitable for you – vadian Jul 17 '15 at 10:43
  • I just tried it and I get the error: `"NSWorkspace doesn’t understand the “sharedWorkspace” message."` Eliminating the brackets appears to have helped. Now I get the error: `launchedApplications of sharedWorkspace of NSWorkspace doesn’t understand the “valueForKey_” message.' – Oliver Baum Jul 17 '15 at 10:44
  • in Mavericks in works only in a script library and you have to add `use framework "Foundation"` – vadian Jul 17 '15 at 10:47
  • Ah, okay. Well maybe I'll try that. Although I did have some mild success with `application file` - when I removed `POSIX path of` it returned an alias with an Applescript path. This may be enough for my purposes. I'll have a fiddle and see if I can get it to do what I want. Thanks for your voluminous knowledge of Applescript! You've been very helpful. I'm going to mark your answer as correct anyway. Since I think many of these solutions will probably work for other people. Or at least give them enough information to put together something that works. – Oliver Baum Jul 17 '15 at 10:56
  • I amended the code in your first suggestion slightly and got it working - hooray! I updated my question with the working code. Thanks for all your help. – Oliver Baum Jul 17 '15 at 11:49