objective-c - 以编程方式获取在 Safari 或 Google chrome 中打开的选项卡的标题或数量
问题描述
在这里,我想了解我的 Mac 上运行的进程或应用程序。我开发了一个可可应用程序,我可以在其中使用下面的代码获取有关当前在我的 mac 上运行的应用程序的信息。
NSArray *runningApps = [[NSWorkspace sharedWorkspace] runningApplications];
但是,现在我想获取在我的 mac 上安装的任何浏览器中打开的选项卡的编号或标题。如果您对此有任何想法,请告诉我如何实现这一目标。
以下是我在 stackoverflow 上遇到的代码以及我在其中一条评论中提到的链接。
我正在调用 logTabs()。但是它没有返回标签的标题。
static NSArray *getAXUIElements(AXUIElementRef theContainer, CFStringRef theRole)
{
// get children of theContainer
AXError error;
NSMutableArray *array = [NSMutableArray array];
CFTypeRef children;
error = AXUIElementCopyAttributeValue(theContainer, kAXChildrenAttribute, &children);
if (error != kAXErrorSuccess)
return nil;
// filter children whose role is theRole
for (CFIndex i = 0; i < CFArrayGetCount(children); i++)
{
AXUIElementRef child = CFArrayGetValueAtIndex(children, i);
CFTypeRef role;
error = AXUIElementCopyAttributeValue(child, kAXRoleAttribute, &role);
if (error == kAXErrorSuccess)
{
if (CFStringCompare(role, theRole, 0) == kCFCompareEqualTo)
[array addObject:(__bridge id)child];
CFRelease(role);
}
}
CFRelease(children);
return [NSArray arrayWithArray:array];
}
static void logTabs()
{
// get the title of every tab of every window of Safari
NSArray *appArray = [NSRunningApplication runningApplicationsWithBundleIdentifier:@"com.google.Chrome"];
AXUIElementRef SafariApp = AXUIElementCreateApplication([[appArray objectAtIndex:0] processIdentifier]);
if (SafariApp)
{
NSLog(@"titile --- 0000 is %lu", (unsigned long)appArray.count);
NSArray *windowArray = getAXUIElements(SafariApp, kAXWindowRole);
for (id window in windowArray)
{
NSArray *tabGroupArray = getAXUIElements((__bridge AXUIElementRef)(window), kAXTabGroupRole);
for (id tabGroup in tabGroupArray)
{
NSArray *radioButtonArray = getAXUIElements((__bridge AXUIElementRef)(tabGroup), kAXRadioButtonRole);
for (id radioButton in radioButtonArray)
{
CFTypeRef title = NULL;
AXError error = AXUIElementCopyAttributeValue((__bridge AXUIElementRef)radioButton, kAXTitleAttribute, &title);
if (error == kAXErrorSuccess)
{
NSLog(@"Title of the tab is %@", title);
CFRelease(title);
}
}
}
}
CFRelease(SafariApp);
}
}
解决方案
推荐阅读
- firebase - Firebase数据库安全规则,检查resource.data中的auth.email
- python - 在 Python 中获取单个字符作为输入,而无需按 Enter(类似于 C++ 中的 getch)
- php - 在 php mysql 中更新数据的问题
- tuples - Pyomo 中元组集的表示
- centos8 - centos 8上的百胜命令ModuleNotFoundError
- python - 使用 Python Windows 获取 CPU 和 GPU 温度
- visual-studio-code - 如何在带有 HashiCorp Terraform 插件的 VSCode 上使用 tflint?
- amazon-web-services - 如何让我的 CodePipeline 管道使用 GitHub 作为源来更新我的资源?
- flask - 在A上运行flask时如何实现“B ping C”?
- javascript - 如何在不影响父节点的情况下对叶节点进行更改