找出在前台和后台运行的应用程序

时间:2015-09-08 11:12:12

标签: objective-c macos

我正在使用Mac OS X.如何在前台和后台找到正在运行的应用程序的名称?

1 个答案:

答案 0 :(得分:5)

您可以使用NSWorkSpace

获取所有正在运行的应用程序
NSArray *runningApplications = [[NSWorkspace sharedWorkspace] runningApplications];

该数组包含NSRunningApplication

的实例

假设您的意思是隐藏背景并且可以看到前景 - 相当于应用程序菜单中的⌘H快捷方式 - 您可以使用谓词过滤数组

NSPredicate *visiblePredicate = [NSPredicate predicateWithFormat:@"hidden == NO"];
NSPredicate *hiddenPredicate = [NSPredicate predicateWithFormat:@"hidden == YES"];

NSArray *visibleApplications = [runningApplications filteredArrayUsingPredicate:visiblePredicate];
NSArray *hiddenApplications = [runningApplications filteredArrayUsingPredicate:hiddenPredicate];

NSLog(@"%@", visibleApplications);
NSLog(@"%@", hiddenApplications);

要确定当前处于活动状态的应用,请使用active属性进行过滤。

NSPredicate *activePredicate = [NSPredicate predicateWithFormat:@"active == YES"];
NSArray *activeApplications = [runningApplications filteredArrayUsingPredicate:activePredicate];  // actually it's always an array containing one application
NSLog(@"%@", activeApplications[0]);