Listing (visible) running applications in JavaScript for Automation

For details and other options, see the ObjC version of the Apple documentation at:

https://developer.apple.com/reference/appkit/nsworkspace?language=objc

(function () {
    "use strict";

    ObjC.import('AppKit');

    var unwrap = ObjC.unwrap;

    // show :: a -> String
    function show(x) {
        return JSON.stringify(x);
    };

    // sort :: Ord a => [a] -> [a]
    function sort(xs) {
        return xs.slice()
            .sort();
    };

    // unlines :: [String] -> String
    function unlines(xs) {
        return xs.join('\n');
    };

    return unlines(sort(
        unwrap($.NSWorkspace
            .sharedWorkspace
            .runningApplications
            .filteredArrayUsingPredicate(
                $.NSPredicate.predicateWithFormat("hidden == NO"))
        )
        .map(function (x) {
            return unwrap(x.localizedName);
        })));
})();

Thanks for sharing a JXA solution.

I'm not sure what the actual intent is of your script, but when running Safari 10.1.1 (11603.2.5) on macOS 10.11.6, it returns many more apps than just the visible apps:

1Password mini
AirPlayUIAgent
CoreServicesUIAgent
DXFinderWindowServer
Default Folder X
Dock
Dropbox
Dropbox Finder Integration
EscrowSecurityAlert
Evernote
Evernote Helper
Evernote Networking
Evernote Web Content
FastScripts
Finder
FolderActionsDispatcher
Google Chrome
Keyboard Maestro
Keyboard Maestro Engine
Keychain Circle Notification
LaterAgent
LaunchBar
Microsoft Alerts Daemon
Microsoft Database Daemon
Microsoft Outlook
Microsoft Update Assistant
Notification Center
Photos Agent
Quiver
Script Editor
SnagitAppleScriptExecutor
SnagitHelper
Speech Synthesis Server
Spotlight
System Events
SystemUIServer
TextExpander Helper
TotalFinder
TotalFinderCrashWatcher
ViewBridgeAuxiliary
Wi-Fi
WordService.service
com.apple.dock.extra
com.apple.speech.speechsynthesisd
iTunes Helper
loginwindow

Is your script supposed to be the JXA equivalent of this AppleScript?

tell application "System Events"
	set appNameList to (name of every process where background only is false)
end tell

set AppleScript's text item delimiters to linefeed
return appNameList as text

With that AppleScript, I get:

Finder
TextExpander
SnippetsLab
Evernote
Microsoft Outlook
LaunchBar
Keyboard Maestro
Google Chrome
Quiver
Script Editor
BBEdit
Script Debugger

1 Like

For details and other options, see the ObjC version of the Apple documentation at:

@ComplexPoint, if you really wanted to be helpful, you could let us know where to find the list of running application attributes that we can use here to filter the list.

I've read the Apple documentation and done extensive searching, but I've been unable to find the attribute list.

Thanks.