如何使用Objective-C获取应用程序的详细信息?

时间:2009-11-18 07:07:25

标签: objective-c cocoa

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

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];

2 个答案:

答案 0 :(得分:1)

您应该使用Process Manager API中的ProcessInformationCopyDictionary函数。将&myPSNkProcessDictionaryIncludeAllInformationMask作为参数,您将获得所需的信息。

答案 1 :(得分:0)

我正在寻找与此主题相关的内容。我需要在特定位置(鼠标位置)的窗口或窗口部分的WindowRef,它必须在所有正在运行的应用程序的所有窗口上...

我曾尝试使用Carbon('我的应用程序完全用C ++编写),但我发现有些碳功能无法正常工作(MacFindWindow,FindWindow,HIWindowFindAtLocation,FindWindowOfClass,HIWindowGetCGWindowID ......)< / p>

也许我做错了,很难相信那些Carbon功能在64位架构中不再起作用......

所以,与你的问题相关,我找到相同的代码,我尝试了这个,但它不是我需要的,我希望它能以任何方式帮助你,我会一直在寻找和尝试,直到我得到它(如果操作系统可以做到每个人都应该这样做。

//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];
        NSString* applicationName = [entry objectForKey:(id)kCGWindowOwnerName];
        NSLog(@"The current app is %@ and the window number of its front window is %@.",applicationName,windowNumber);
        CGRect bounds;
        CGRectMakeWithDictionaryRepresentation((CFDictionaryRef)[entry objectForKey:(id)kCGWindowBounds], &bounds);
        NSLog(@"WINDOW RECT BOUNDS; (x,y,width, height) = (%d,%d, %d, %d)", bounds.origin.x, bounds.origin.y, bounds.size.width, bounds.size.height);


        break;
    }

另外,请点击此链接,我不会帮到你。我确定:

http://code.google.com/p/blazingstars/source/browse/trunk/PokerHK/HKLowLevel.m?r=70