首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
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

Stack Overflow用户

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

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

票数 1
EN
查看全部 2 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/1754160

复制
相关文章

相似问题

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