首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何使用Objective-C获取应用程序的详细信息?

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

Stack Overflow用户
提问于 2009-11-18 15:07:25
回答 2查看 4.5K关注 0票数 2

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

代码语言:javascript
复制
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];
EN

回答 2

Stack Overflow用户

发布于 2009-11-18 17:23:15

您应该使用Process Manager API中的ProcessInformationCopyDictionary函数。给它&myPSNkProcessDictionaryIncludeAllInformationMask作为参数,你就会得到你想要的信息。

票数 1
EN

Stack Overflow用户

发布于 2010-04-13 17:46:52

我在找一些和这个话题相关的东西。我需要一个窗口或窗口部件在某个位置(鼠标位置)的WindowRef,它必须覆盖所有正在运行的应用程序的所有窗口…

我用Carbon试过了(因为我的应用完全是用C++写的),但我发现一些Carbon功能不能正常工作(MacFindWindow,FindWindow,HIWindowFindAtLocation,FindWindowOfClass,HIWindowGetCGWindowID……)

也许我做错了,很难相信这些碳函数在64位架构中不再起作用……

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

代码语言:javascript
复制
//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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/1754160

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档