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

心不动则不痛 提交于 2021-01-21 07:32:15

问题


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.


回答1:


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.




回答2:


Updated for Swift 3

    let ws = NSWorkspace.shared()
    let apps = ws.runningApplications
    for currentApp in apps
    {
        if(currentApp.activationPolicy == .regular){
            print(currentApp.localizedName!)
        }
    }



回答3:


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




回答4:


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 }



回答5:


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.
    }
}



回答6:


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)        
    }
}



回答7:


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.



来源:https://stackoverflow.com/questions/20054662/how-to-get-list-of-all-applications-currently-running-and-visible-in-dock-for-ma

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!