如何使用Objective-C获取应用程序的详细信息? [英] How do I get the details of an application using Objective-C?

查看:198
本文介绍了如何使用Objective-C获取应用程序的详细信息?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码来检测当前窗口。我如何获得1)应用程序内部名称,2)位置,3)发布者和4)窗口/应用程序的描述?

I have the following code to detect the current window. How can I get 1) application internal name, 2) location, 3) publisher and 4) description of the window/application?

NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];

//Get info about the currently active application.
NSWorkspace* workspace            = [NSWorkspace sharedWorkspace];
NSDictionary* currentAppInfo      = [workspace activeApplication];

//Get the PSN of the current application.
UInt32 lowLong                    = [[currentAppInfo objectForKey:@"NSApplicationProcessSerialNumberLow"] longValue];
UInt32 highLong                   = [[currentAppInfo objectForKey:@"NSApplicationProcessSerialNumberHigh"] longValue];
ProcessSerialNumber currentAppPSN = {highLong,lowLong};

//Grab window information from the window server.
CFArrayRef windowList             = CGWindowListCopyWindowInfo(kCGWindowListOptionOnScreenOnly, kCGNullWindowID);
ProcessSerialNumber myPSN         = {kNoProcess, kNoProcess};

//Loop through the windows, the window list is ordered from front to back.
for (NSMutableDictionary* entry in (NSArray*) windowList)
{
    int pid = [[entry objectForKey:(id)kCGWindowOwnerPID] intValue];
    GetProcessForPID(pid, &myPSN);

    //If the process of the current window in the list matches our process, get the front window number.
    if(myPSN.lowLongOfPSN == currentAppPSN.lowLongOfPSN && myPSN.highLongOfPSN == currentAppPSN.highLongOfPSN)
    {
        NSNumber *windowNumber    = [entry objectForKey:(id)kCGWindowNumber];
        windowNumber = [entry objectForKey:(id)kCGWindowNumber];
        NSString* applicationName = [entry objectForKey:(id)kCGWindowOwnerName];
        NSLog(@"Capture the window: %@ with window ID: %@.",applicationName,windowNumber);
        return applicationName;

        //Break because we only want the front window.
        break;
    }
}
CFRelease(windowList);
[pool release];


推荐答案

您应该使用 ProcessInformationCopyDictionary function from Process Manager API。将& myPSN kProcessDictionaryIncludeAllInformationMask 作为参数,您将获得您要查找的信息。

You should use the ProcessInformationCopyDictionary function from the Process Manager API. Give it &myPSN and kProcessDictionaryIncludeAllInformationMask as arguments and you will get the information you are looking for.

这篇关于如何使用Objective-C获取应用程序的详细信息?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆