How do I find information on other applications running on the computer on Mac/Cocoa/Obj-C?

前端 未结 3 487
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-12-09 07:16

I want to find which applications are running, particularly I want to know which one has a window that has focus. So at any given time I want the app to know which applicati

相关标签:
3条回答
  • 2020-12-09 07:34

    Have a look at GetFrontProcess and CopyProcessName (or GetProcessInformation).

    Or have a look at NSWorkspace::activeApplication.

    EDIT: the reference documentation says

    It is strongly suggested that you use the NSRunningApplication methods currentApplication or active to retrieve this information in post Mac OS X v10.6 targeted applications.

    0 讨论(0)
  • 2020-12-09 07:34

    To get a list of running applications:

    NSArray *appNames = [[NSWorkspace sharedWorkspace] runningApplications];
    

    To get the active application:

    NSRunningApplication *currentApp = [NSRunningApplication currentApplication];
    

    Here's a sample project from Apple:

    http://developer.apple.com/mac/library/samplecode/AppList/index.html#//apple_ref/doc/uid/DTS40008859

    These all require 10.6, according to the documentation.

    0 讨论(0)
  • 2020-12-09 07:39

    Apologies, I found what I needed. If anyone is looking to do this same thing, this snippet should help:

    NSDictionary *activeApp = [[NSWorkspace sharedWorkspace] activeApplication];
    NSLog(@"Active application is: %@", (NSString *)[activeApp objectForKey:@"NSApplicationName"] );
    
    0 讨论(0)
提交回复
热议问题