Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get list of all applications currently running and visible in Dock for Mac OS X programming?

Tags:

macos

cocoa

I need to know which applications are running and have an active window. Something similar must be what's in the 'command + tab' hotkey switcher.

When I run some code like this:

NSWorkspace * ws = [NSWorkspace sharedWorkspace];
NSArray * apps = [ws runningApplications];
NSLog (@"%@", apps);

I get this in my log:

"<NSRunningApplication: 0x6080001052b0 (com.apple.loginwindow - 66)>",
"<NSRunningApplication: 0x608000105340 (com.apple.systemuiserver - 285)>",
"<NSRunningApplication: 0x6080001053d0 (com.apple.dock - 284)>",
"<NSRunningApplication: 0x608000105460 (com.apple.finder - 286)>",
"<NSRunningApplication: 0x6080001054f0 (com.apple.iTunesHelper - 339)>",
"<NSRunningApplication: 0x608000105580 (cc.omh.Clyppan - 340)>",
"<NSRunningApplication: 0x608000105610 (ws.agile.1PasswordAgent - 333)>",
"<NSRunningApplication: 0x6080001056a0 (com.irradiatedsoftware.Cinch-Direct - 342)>",
"<NSRunningApplication: 0x608000105730 (com.leapmotion.Leap-Motion - 343)>",
"<NSRunningApplication: 0x6080001057c0 (com.apple.notificationcenterui - 316)>",
"<NSRunningApplication: 0x608000105850 (com.smileonmymac.textexpander - 348)>",
"<NSRunningApplication: 0x6080001058e0 (com.lightheadsw.caffeine - 350)>",
"<NSRunningApplication: 0x608000105970 (2BUA8C4S2C.com.agilebits.onepassword4-helper - 309)>"

It's showing all running applications. I only want the applications that are running and visible in the Dock.

like image 864
Cam Avatar asked Nov 18 '13 18:11

Cam


1 Answers

The members of the array returned by [NSWorkspace runningApplications] are of type NSRunningApplication. You want to pick out the ones whose activationPolicy property has type NSApplicationActivationPolicyRegular.

like image 80
JWWalker Avatar answered Oct 13 '22 16:10

JWWalker