14

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.

rmaddy
  • 314,917
  • 42
  • 532
  • 579
Cam
  • 988
  • 1
  • 12
  • 25

7 Answers7

20

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.

JWWalker
  • 22,385
  • 6
  • 55
  • 76
8

Updated for Swift 3

    let ws = NSWorkspace.shared()
    let apps = ws.runningApplications
    for currentApp in apps
    {
        if(currentApp.activationPolicy == .regular){
            print(currentApp.localizedName!)
        }
    }
Ted Lowery
  • 1,535
  • 2
  • 13
  • 8
8

The following code uses the NSWorkplace workspace.runningApplications.filter call to extract the applications.

// Swift 4.1
//: Playground - noun: a place where people can play

import Cocoa

let workspace = NSWorkspace.shared
let apps = workspace.runningApplications.filter{  $0.activationPolicy == .regular }
Joe McMahon
  • 3,266
  • 21
  • 33
2

To access the list of all running applications, use the runningApplications method in NSWorkspace.

Brad Allred
  • 7,323
  • 1
  • 30
  • 49
1

I got it working here. Thanks for the help!

NSWorkspace * ws = [NSWorkspace sharedWorkspace];
NSArray * apps = [ws runningApplications];

NSUInteger count = [apps count];
for (NSUInteger i = 0; i < count; i++) {
    NSRunningApplication *app = [apps objectAtIndex: i];
    //NSLog (@"%@", app.bundleIdentifier );

    if(app.activationPolicy == NSApplicationActivationPolicyRegular) {
       //NSLog(@"0"); These are the ones we want.
    }
}
Cam
  • 988
  • 1
  • 12
  • 25
  • 4
    The value of `NSApplicationActivationPolicyRegular` happens to be 0, but it's a bad programming practice to write 0 when you mean `NSApplicationActivationPolicyRegular`. – JWWalker Nov 18 '13 at 19:59
1

In Swift. Write below code you will get all the running application names.

let ws = NSWorkspace.sharedWorkspace()

let apps = ws.runningApplications

for currentApp in apps.enumerate(){        
    let runningApp = apps[currentApp.index]

    if(runningApp.activationPolicy == .Regular){
        print(runningApp.localizedName)        
    }
}
Fabian N.
  • 3,807
  • 2
  • 23
  • 46
Anil Kukadeja
  • 1,348
  • 1
  • 14
  • 27
  • Changes for Swift 3: for currentApp in apps.enumerated() { let runningApp = apps[currentApp.offset]; if(runningApp.activationPolicy == .regular) { print(runningApp.localizedName); } } – Critter Sep 10 '16 at 14:38
  • it return's only the running application and what if i want other application which are visible in docks but not running(active) – Ajay Singh Thakur Jul 15 '17 at 08:13
  • This is quite odd: Why enumerate to get the index, only to get `runningApp`, when you already have `currentApp`? This is very roundabout. – Alexander Sep 13 '22 at 12:20
0

Updated for optimal Swift 3

let workspace = NSWorkspace()

let apps = workspace.runningApplications.filter{$0.activationPolicy == NSApplicationActivationPolicy.regular}

The apps array will contain every application that is currently running and on screen in view.

Aron Gates
  • 21
  • 2