OSX cocoa app - Get safari tab info

只谈情不闲聊 提交于 2020-07-05 10:18:07

问题


I'm wondering if it's possible to get any tab/window info from safari programmatically?

Is there a library to do it?

I'd prefer not applescript, as I've found that - I'd like to know if, and how it's possible in the Cocoa framework.


回答1:


You can do this with Scripting Bridge, which is like AppleScript translated into Objective-C, or with accessibility objects, which you can inspect with Developer Tool Accessibility Inspector. Both technogies have their quirks and aren't documented very well.

Edit:

Scripting Bridge example:

SafariApplication *SafariApp = [SBApplication applicationWithBundleIdentifier:@"com.apple.Safari"];
for (SafariWindow *window in SafariApp.windows)
{
    for (SafariTab *tab in window.tabs)
        NSLog(@"%@", tab.name);
}

The hierarchy of accessibility objects in Safari is

AXApplication
 AXWindow
  AXTabGroup
   AXRadioButton

Example (doesn't win a prize in a beauty contest):

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.apple.Safari"];
    AXUIElementRef SafariApp = AXUIElementCreateApplication([[appArray objectAtIndex:0] processIdentifier]);
    if (SafariApp)
    {
        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);
                        CFRelease(title);
                    }
                }
            }
        }
        CFRelease(SafariApp);
    }
}


来源:https://stackoverflow.com/questions/33440157/osx-cocoa-app-get-safari-tab-info

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!