Engineering Blog

Querying Running Applications in MacOS

If you want to list out all of the currently running applications in a MacOS app, you can do:

for app in NSWorkspace.shared.runningApplications {
  print(app.localizedName!)
}

This will give you a whole bunch of running "applications", like:

loginwindow
ViewBridgeAuxiliary
talagent
Dock
ViewBridgeAuxiliary
QuickLookUIService (PID 363)
Finder
Wi-Fi
Notification Center
com.apple.dock.extra
Spotlight
Sync
AppSSOAgent
Alfred
TextInputMenuAgent
imklaunchagent
AirPlayUIAgent
... many more ...

If you're only interested in the normal user-apps that appear in the Dock, you would filter on the .activationPolicy like so:

for app in NSWorkspace.shared.runningApplications {
  if app.activationPolicy == .regular {
    print(app.localizedName!)
  }
}

Producing a much smaller list:

Finder
Xcode
Things
iTerm2
Console
Brave Browser
Slack
Code
Simulator
Google Chrome
Safari

Finally, if your app is doing something in the background (like observing global events), and you want to know what app is currently focused, or active (frontmost), you can get the same with

NSWorkspace.shared.frontmostApplication?.localizedName
Gertrude

The Gertrude mac app helps you protect your kids online with strict internet filtering that you can manage from your own computer or phone, plus remote monitoring of screenshots and keylogging. $5/mo, with a 60 day free trial.

Start free trial →