python获取mac窗口程序内容_获取Mac OS X中当前活动窗口/文档的标题

在Objective-C中,简单的答案是,使用一点可可粉,主要是Carbon Accessibility API:// Get the process ID of the frontmost application.

NSRunningApplication* app = [[NSWorkspace sharedWorkspace]

frontmostApplication];

pid_t pid = [app processIdentifier];

// See if we have accessibility permissions, and if not, prompt the user to

// visit System Preferences.

NSDictionary *options = @{(id)kAXTrustedCheckOptionPrompt: @YES};

Boolean appHasPermission = AXIsProcessTrustedWithOptions(

(__bridge CFDictionaryRef)options);

if (!appHasPermission) {

return; // we don't have accessibility permissions

// Get the accessibility element corresponding to the frontmost application.

AXUIElementRef appElem = AXUIElementCreateApplication(pid);

if (!appElem) {

return;

}

// Get the accessibility element corresponding to the frontmost window

// of the frontmost application.

AXUIElementRef window = NULL;

if (AXUIElementCopyAttributeValue(appElem,

kAXFocusedWindowAttribute, (CFTypeRef*)&window) != kAXErrorSuccess) {

CFRelease(appElem);

return;

}

// Finally, get the title of the frontmost window.

CFStringRef title = NULL;

AXError result = AXUIElementCopyAttributeValue(window, kAXTitleAttribute,

(CFTypeRef*)&title);

// At this point, we don't need window and appElem anymore.

CFRelease(window);

CFRelease(appElem);

if (result != kAXErrorSuccess) {

// Failed to get the window title.

return;

}

// Success! Now, do something with the title, e.g. copy it somewhere.

// Once we're done with the title, release it.

CFRelease(title);

或者,如this StackOverflow answer中所述,使用CGWindow API可能更简单。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值