我正在使用Mac OS X.如何在前台和后台找到正在运行的应用程序的名称?
答案 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]);