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);
}
macos accessibility objective-c++
add a comment |
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);
}
macos accessibility objective-c++
add a comment |
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);
}
macos accessibility objective-c++
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++
macos accessibility objective-c++
edited Nov 9 at 16:48
Mark Setchell
83.5k568167
83.5k568167
asked Nov 9 at 16:43
Alex Petrosyan
10411
10411
add a comment |
add a comment |
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.
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
add a comment |
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.
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
add a comment |
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.
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
add a comment |
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.
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.
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
add a comment |
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
add a comment |
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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