如何获取Dock for Mac OS X编程中当前正在运行和可见的所有应用程序的列表?

时间:2013-11-18 18:06:34

标签: macos cocoa

我需要知道哪些应用程序正在运行并且有一个活动窗口。类似的东西必须是'command + tab'热键切换器中的内容。

当我运行这样的代码时:

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

我在日志中得到了这个:

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

它显示了所有正在运行的应用程序。我只想要在Dock中运行和可见的应用程序。

7 个答案:

答案 0 :(得分:12)

[NSWorkspace runningApplications]返回的数组成员的类型为NSRunningApplication。您想要挑选activationPolicy属性类型为NSApplicationActivationPolicyRegular的人。

答案 1 :(得分:6)

针对Swift 3进行了更新

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

答案 2 :(得分:2)

要访问所有正在运行的应用程序的列表,请使用runningApplications中的NSWorkspace方法。

答案 3 :(得分:1)

我在这里工作了。谢谢你的帮助!

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

答案 4 :(得分:1)

在斯威夫特。写下以下代码,您将获得所有正在运行的应用程序名称。

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

答案 5 :(得分:1)

以下代码使用NSWorkplace workspace.runningApplications.filter调用来提取应用程序。

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

答案 6 :(得分:0)

针对最佳Swift 3进行了更新

let workspace = NSWorkspace()

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

apps数组将包含当前正在运行且在屏幕上的每个应用程序。