How to retrieve active window URL using Mac OS X accessibility API









up vote
2
down vote

favorite












I was wondering if there was a way to retrieve the URL of the active window, if it has one. The following code should work, but it doesn't, the title variable becomes "", and I don't get any new information.



I'm positive that that's because I'm using kAXURLAttributes of the wrong object, but I have no clue which one should I copy it from... Apple's documentation has very few explanations.



AXUIElementRef appElem = AXUIElementCreateApplication(pid), window = nullptr;
CFStringRef title = nullptr;
if (!appElem) return;
if (AXUIElementCopyAttributeValue (appElem, kAXFocusedWindowAttribute,
reinterpret_cast<CFTypeRef*>(&window)) != kAXErrorSuccess && appElem)
CFRelease(appElem);
if(AXUIElementCopyAttributeValue (window, kAXTitleAttribute,
reinterpret_cast<CFTypeRef*>(&title))!=kAXErrorSuccess && window)
CFRelease(window);
focusedAppTitle = QString::fromCFString(title);
if(AXUIElementCopyAttributeValue (window, kAXURLAttribute,
reinterpret_cast<CFTypeRef*>(&title))!=kAXErrorSuccess)
CFRelease(window);
}









share|improve this question



























    up vote
    2
    down vote

    favorite












    I was wondering if there was a way to retrieve the URL of the active window, if it has one. The following code should work, but it doesn't, the title variable becomes "", and I don't get any new information.



    I'm positive that that's because I'm using kAXURLAttributes of the wrong object, but I have no clue which one should I copy it from... Apple's documentation has very few explanations.



    AXUIElementRef appElem = AXUIElementCreateApplication(pid), window = nullptr;
    CFStringRef title = nullptr;
    if (!appElem) return;
    if (AXUIElementCopyAttributeValue (appElem, kAXFocusedWindowAttribute,
    reinterpret_cast<CFTypeRef*>(&window)) != kAXErrorSuccess && appElem)
    CFRelease(appElem);
    if(AXUIElementCopyAttributeValue (window, kAXTitleAttribute,
    reinterpret_cast<CFTypeRef*>(&title))!=kAXErrorSuccess && window)
    CFRelease(window);
    focusedAppTitle = QString::fromCFString(title);
    if(AXUIElementCopyAttributeValue (window, kAXURLAttribute,
    reinterpret_cast<CFTypeRef*>(&title))!=kAXErrorSuccess)
    CFRelease(window);
    }









    share|improve this question

























      up vote
      2
      down vote

      favorite









      up vote
      2
      down vote

      favorite











      I was wondering if there was a way to retrieve the URL of the active window, if it has one. The following code should work, but it doesn't, the title variable becomes "", and I don't get any new information.



      I'm positive that that's because I'm using kAXURLAttributes of the wrong object, but I have no clue which one should I copy it from... Apple's documentation has very few explanations.



      AXUIElementRef appElem = AXUIElementCreateApplication(pid), window = nullptr;
      CFStringRef title = nullptr;
      if (!appElem) return;
      if (AXUIElementCopyAttributeValue (appElem, kAXFocusedWindowAttribute,
      reinterpret_cast<CFTypeRef*>(&window)) != kAXErrorSuccess && appElem)
      CFRelease(appElem);
      if(AXUIElementCopyAttributeValue (window, kAXTitleAttribute,
      reinterpret_cast<CFTypeRef*>(&title))!=kAXErrorSuccess && window)
      CFRelease(window);
      focusedAppTitle = QString::fromCFString(title);
      if(AXUIElementCopyAttributeValue (window, kAXURLAttribute,
      reinterpret_cast<CFTypeRef*>(&title))!=kAXErrorSuccess)
      CFRelease(window);
      }









      share|improve this question















      I was wondering if there was a way to retrieve the URL of the active window, if it has one. The following code should work, but it doesn't, the title variable becomes "", and I don't get any new information.



      I'm positive that that's because I'm using kAXURLAttributes of the wrong object, but I have no clue which one should I copy it from... Apple's documentation has very few explanations.



      AXUIElementRef appElem = AXUIElementCreateApplication(pid), window = nullptr;
      CFStringRef title = nullptr;
      if (!appElem) return;
      if (AXUIElementCopyAttributeValue (appElem, kAXFocusedWindowAttribute,
      reinterpret_cast<CFTypeRef*>(&window)) != kAXErrorSuccess && appElem)
      CFRelease(appElem);
      if(AXUIElementCopyAttributeValue (window, kAXTitleAttribute,
      reinterpret_cast<CFTypeRef*>(&title))!=kAXErrorSuccess && window)
      CFRelease(window);
      focusedAppTitle = QString::fromCFString(title);
      if(AXUIElementCopyAttributeValue (window, kAXURLAttribute,
      reinterpret_cast<CFTypeRef*>(&title))!=kAXErrorSuccess)
      CFRelease(window);
      }






      macos accessibility objective-c++






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 9 at 16:48









      Mark Setchell

      83.5k568167




      83.5k568167










      asked Nov 9 at 16:43









      Alex Petrosyan

      10411




      10411






















          1 Answer
          1






          active

          oldest

          votes

















          up vote
          1
          down vote



          accepted










          There doesn't appear to be any general way to do this. For example, Firefox does not expose anything about its windows through the Accessibility API.



          With Safari, I was able to find the URL, but it was a bit buried within the UI Element hierarchy. For example, in AppleScript object specifier syntax, the object is:



          UI element 2 of group 2 of toolbar 1 of window 1 of ¬
          application process "Safari" of application "System Events"


          The element's role is "AXSafariAddressAndSearchField" and its value is the URL. If the user is part-way through editing the content of that field, its value may show the edit and not the URL of whatever's actually displayed. I don't know.



          There's also:



          UI element 1 of scroll area 1 of group 1 of group 1 of ¬
          tab group 1 of splitter group 1 of window 1 of ¬
          application process "Safari" of application "System Events"


          It's the element whose role is "AXWebArea". It has an "AXURL" (kAXURLAttribute) attribute whose value is the URL of the page.



          Those accessibility paths may differ for other versions of Safari or different UI configurations. (For example, my window had only one tab open.)



          You'll probably have to write code to enumerate the whole element hierarchy looking for the one you want by role.



          I haven't tested with Chrome. I did see a reference to "AXWebArea" in its sources, though, so the same strategy may work for it.






          share|improve this answer






















          • I really do need the URL of the active window. I.e. the URL of the active browser Tab. Besides, the document attribute returns a blank line too.
            – Alex Petrosyan
            Nov 9 at 19:14











          • You should have mentioned that you're only interested in browsers in your question. Anyway, updated my answer.
            – Ken Thomases
            Nov 9 at 21:48










          • Ok, this still has the question of how to navigate the hierarchy...
            – Alex Petrosyan
            Nov 9 at 22:57










          • You get the Children attribute, get its value which will be an array, enumerate that array, etc.
            – Ken Thomases
            Nov 9 at 23:48










          Your Answer






          StackExchange.ifUsing("editor", function ()
          StackExchange.using("externalEditor", function ()
          StackExchange.using("snippets", function ()
          StackExchange.snippets.init();
          );
          );
          , "code-snippets");

          StackExchange.ready(function()
          var channelOptions =
          tags: "".split(" "),
          id: "1"
          ;
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function()
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled)
          StackExchange.using("snippets", function()
          createEditor();
          );

          else
          createEditor();

          );

          function createEditor()
          StackExchange.prepareEditor(
          heartbeatType: 'answer',
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader:
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          ,
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          );



          );













           

          draft saved


          draft discarded


















          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53229924%2fhow-to-retrieve-active-window-url-using-mac-os-x-accessibility-api%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes








          up vote
          1
          down vote



          accepted










          There doesn't appear to be any general way to do this. For example, Firefox does not expose anything about its windows through the Accessibility API.



          With Safari, I was able to find the URL, but it was a bit buried within the UI Element hierarchy. For example, in AppleScript object specifier syntax, the object is:



          UI element 2 of group 2 of toolbar 1 of window 1 of ¬
          application process "Safari" of application "System Events"


          The element's role is "AXSafariAddressAndSearchField" and its value is the URL. If the user is part-way through editing the content of that field, its value may show the edit and not the URL of whatever's actually displayed. I don't know.



          There's also:



          UI element 1 of scroll area 1 of group 1 of group 1 of ¬
          tab group 1 of splitter group 1 of window 1 of ¬
          application process "Safari" of application "System Events"


          It's the element whose role is "AXWebArea". It has an "AXURL" (kAXURLAttribute) attribute whose value is the URL of the page.



          Those accessibility paths may differ for other versions of Safari or different UI configurations. (For example, my window had only one tab open.)



          You'll probably have to write code to enumerate the whole element hierarchy looking for the one you want by role.



          I haven't tested with Chrome. I did see a reference to "AXWebArea" in its sources, though, so the same strategy may work for it.






          share|improve this answer






















          • I really do need the URL of the active window. I.e. the URL of the active browser Tab. Besides, the document attribute returns a blank line too.
            – Alex Petrosyan
            Nov 9 at 19:14











          • You should have mentioned that you're only interested in browsers in your question. Anyway, updated my answer.
            – Ken Thomases
            Nov 9 at 21:48










          • Ok, this still has the question of how to navigate the hierarchy...
            – Alex Petrosyan
            Nov 9 at 22:57










          • You get the Children attribute, get its value which will be an array, enumerate that array, etc.
            – Ken Thomases
            Nov 9 at 23:48














          up vote
          1
          down vote



          accepted










          There doesn't appear to be any general way to do this. For example, Firefox does not expose anything about its windows through the Accessibility API.



          With Safari, I was able to find the URL, but it was a bit buried within the UI Element hierarchy. For example, in AppleScript object specifier syntax, the object is:



          UI element 2 of group 2 of toolbar 1 of window 1 of ¬
          application process "Safari" of application "System Events"


          The element's role is "AXSafariAddressAndSearchField" and its value is the URL. If the user is part-way through editing the content of that field, its value may show the edit and not the URL of whatever's actually displayed. I don't know.



          There's also:



          UI element 1 of scroll area 1 of group 1 of group 1 of ¬
          tab group 1 of splitter group 1 of window 1 of ¬
          application process "Safari" of application "System Events"


          It's the element whose role is "AXWebArea". It has an "AXURL" (kAXURLAttribute) attribute whose value is the URL of the page.



          Those accessibility paths may differ for other versions of Safari or different UI configurations. (For example, my window had only one tab open.)



          You'll probably have to write code to enumerate the whole element hierarchy looking for the one you want by role.



          I haven't tested with Chrome. I did see a reference to "AXWebArea" in its sources, though, so the same strategy may work for it.






          share|improve this answer






















          • I really do need the URL of the active window. I.e. the URL of the active browser Tab. Besides, the document attribute returns a blank line too.
            – Alex Petrosyan
            Nov 9 at 19:14











          • You should have mentioned that you're only interested in browsers in your question. Anyway, updated my answer.
            – Ken Thomases
            Nov 9 at 21:48










          • Ok, this still has the question of how to navigate the hierarchy...
            – Alex Petrosyan
            Nov 9 at 22:57










          • You get the Children attribute, get its value which will be an array, enumerate that array, etc.
            – Ken Thomases
            Nov 9 at 23:48












          up vote
          1
          down vote



          accepted







          up vote
          1
          down vote



          accepted






          There doesn't appear to be any general way to do this. For example, Firefox does not expose anything about its windows through the Accessibility API.



          With Safari, I was able to find the URL, but it was a bit buried within the UI Element hierarchy. For example, in AppleScript object specifier syntax, the object is:



          UI element 2 of group 2 of toolbar 1 of window 1 of ¬
          application process "Safari" of application "System Events"


          The element's role is "AXSafariAddressAndSearchField" and its value is the URL. If the user is part-way through editing the content of that field, its value may show the edit and not the URL of whatever's actually displayed. I don't know.



          There's also:



          UI element 1 of scroll area 1 of group 1 of group 1 of ¬
          tab group 1 of splitter group 1 of window 1 of ¬
          application process "Safari" of application "System Events"


          It's the element whose role is "AXWebArea". It has an "AXURL" (kAXURLAttribute) attribute whose value is the URL of the page.



          Those accessibility paths may differ for other versions of Safari or different UI configurations. (For example, my window had only one tab open.)



          You'll probably have to write code to enumerate the whole element hierarchy looking for the one you want by role.



          I haven't tested with Chrome. I did see a reference to "AXWebArea" in its sources, though, so the same strategy may work for it.






          share|improve this answer














          There doesn't appear to be any general way to do this. For example, Firefox does not expose anything about its windows through the Accessibility API.



          With Safari, I was able to find the URL, but it was a bit buried within the UI Element hierarchy. For example, in AppleScript object specifier syntax, the object is:



          UI element 2 of group 2 of toolbar 1 of window 1 of ¬
          application process "Safari" of application "System Events"


          The element's role is "AXSafariAddressAndSearchField" and its value is the URL. If the user is part-way through editing the content of that field, its value may show the edit and not the URL of whatever's actually displayed. I don't know.



          There's also:



          UI element 1 of scroll area 1 of group 1 of group 1 of ¬
          tab group 1 of splitter group 1 of window 1 of ¬
          application process "Safari" of application "System Events"


          It's the element whose role is "AXWebArea". It has an "AXURL" (kAXURLAttribute) attribute whose value is the URL of the page.



          Those accessibility paths may differ for other versions of Safari or different UI configurations. (For example, my window had only one tab open.)



          You'll probably have to write code to enumerate the whole element hierarchy looking for the one you want by role.



          I haven't tested with Chrome. I did see a reference to "AXWebArea" in its sources, though, so the same strategy may work for it.







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 9 at 21:47

























          answered Nov 9 at 18:05









          Ken Thomases

          68.3k668104




          68.3k668104











          • I really do need the URL of the active window. I.e. the URL of the active browser Tab. Besides, the document attribute returns a blank line too.
            – Alex Petrosyan
            Nov 9 at 19:14











          • You should have mentioned that you're only interested in browsers in your question. Anyway, updated my answer.
            – Ken Thomases
            Nov 9 at 21:48










          • Ok, this still has the question of how to navigate the hierarchy...
            – Alex Petrosyan
            Nov 9 at 22:57










          • You get the Children attribute, get its value which will be an array, enumerate that array, etc.
            – Ken Thomases
            Nov 9 at 23:48
















          • I really do need the URL of the active window. I.e. the URL of the active browser Tab. Besides, the document attribute returns a blank line too.
            – Alex Petrosyan
            Nov 9 at 19:14











          • You should have mentioned that you're only interested in browsers in your question. Anyway, updated my answer.
            – Ken Thomases
            Nov 9 at 21:48










          • Ok, this still has the question of how to navigate the hierarchy...
            – Alex Petrosyan
            Nov 9 at 22:57










          • You get the Children attribute, get its value which will be an array, enumerate that array, etc.
            – Ken Thomases
            Nov 9 at 23:48















          I really do need the URL of the active window. I.e. the URL of the active browser Tab. Besides, the document attribute returns a blank line too.
          – Alex Petrosyan
          Nov 9 at 19:14





          I really do need the URL of the active window. I.e. the URL of the active browser Tab. Besides, the document attribute returns a blank line too.
          – Alex Petrosyan
          Nov 9 at 19:14













          You should have mentioned that you're only interested in browsers in your question. Anyway, updated my answer.
          – Ken Thomases
          Nov 9 at 21:48




          You should have mentioned that you're only interested in browsers in your question. Anyway, updated my answer.
          – Ken Thomases
          Nov 9 at 21:48












          Ok, this still has the question of how to navigate the hierarchy...
          – Alex Petrosyan
          Nov 9 at 22:57




          Ok, this still has the question of how to navigate the hierarchy...
          – Alex Petrosyan
          Nov 9 at 22:57












          You get the Children attribute, get its value which will be an array, enumerate that array, etc.
          – Ken Thomases
          Nov 9 at 23:48




          You get the Children attribute, get its value which will be an array, enumerate that array, etc.
          – Ken Thomases
          Nov 9 at 23:48

















           

          draft saved


          draft discarded















































           


          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53229924%2fhow-to-retrieve-active-window-url-using-mac-os-x-accessibility-api%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Use pre created SQLite database for Android project in kotlin

          Darth Vader #20

          Ondo