Reputation: 1048
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.
Upvotes: 14
Views: 8516
Reputation: 91
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 }
Upvotes: 9
Reputation: 21
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.
Upvotes: 0
Reputation: 1545
Updated for Swift 3
let ws = NSWorkspace.shared()
let apps = ws.runningApplications
for currentApp in apps
{
if(currentApp.activationPolicy == .regular){
print(currentApp.localizedName!)
}
}
Upvotes: 8
Reputation: 1358
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)
}
}
Upvotes: 1
Reputation: 1048
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.
}
}
Upvotes: 1
Reputation: 22707
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
.
Upvotes: 20
Reputation: 7534
To access the list of all running applications, use the runningApplications
method in NSWorkspace
.
Upvotes: 2