Internet Explorer / FireFox - Activate Tab (UIA - 64bit AHK)

Post your working scripts, libraries and tools for AHK v1.1 and older
User avatar
jethrow
Posts: 188
Joined: 30 Sep 2013, 19:52
Location: Iowa

Internet Explorer / FireFox - Activate Tab (UIA - 64bit AHK)

Post by jethrow » 16 Feb 2014, 03:00

Note: UI Automation requires at least Windows XP SP3. See here for more details.
Note: The UI Automation library is currently only compatible with 64bit AHK.
I wrote: There is no direct HTML DOM method for activating a specific IE tab, that I'm aware of.
 
However, ACC (or better yet, UIA) provides a way of accomplishing this by accessing the tab element, then calling DoDefaultAction (the DefaultAction being Press). The problem with this method is finding a way to access the tab element that doesn't change with each new release of IE.
I've been wanting a UIAutomation library to accomplish such tasks, because it allows you to query an elements descendants based on a given criteria. Since AFAIK there isn't a UIA library being actively developed, I decided to write a quick function using my partially developed UIA library. I purposefully wrote this function in a noobie-friendly (relatively speaking) way to generate understanding & inspiration.

This function was developed using Internet Explorer 11, but should be compatible with any version of Internet Explorer that has tabs (7+). I would like to hear results from others using different versions of Internet Explorer.

This function requires UIA_Interface.ahk. The TabName parameter is case-sensitive:

Code: Select all

TabActivate(TabName, WinTitle) {
	;~ Set Constants / Static Variables
	static	UIA_ControlTypePropertyId := 30003
		,	UIA_NamePropertyId := 30005
		,	TabItem := 50019
		,	VT_I4 := 3
		,	VT_BSTR := 8
		,	TreeScope_Descendants := 0x4
		,	uia := UIA_Interface()
		,	Condition1

	;~ Get Internet Explorer UIA Element
	WinGet, hwnd, ID, %WinTitle%
	ie := uia.ElementFromHandle(hwnd)
	
	;~ Set Conditions to find sub-element (TabItem)
	if Not Condition1
		Condition1 := uia.CreatePropertyCondition(UIA_ControlTypePropertyId, TabItem, VT_I4)
	Condition2 := uia.CreatePropertyCondition(UIA_NamePropertyId, TabName, VT_BSTR)
	AndCondition := uia.CreateAndCondition(Condition1,Condition2)
	
	;~ Query for the first matching sub-element
	tab := ie.FindFirst(AndCondition, TreeScope_Descendants)
	
	;~ Access the Legacy Pattern
		;~ Legacy Pattern is similar to the Accessible Interface, which has the DoDefaultAction method (Press)
	legacy := tab.GetCurrentPatternAs("LegacyIAccessible")
	legacy.DoDefaultAction()
}
Note: it should be evident, but I didn't incorporate any error handling.
Note: after posting this, I realized this works with FireFox as well & updated the thread title.

User avatar
Blackholyman
Posts: 1293
Joined: 29 Sep 2013, 22:57
Location: Denmark
Contact:

Re: Internet Explorer / FireFox - Activate Tab (UI Automatio

Post by Blackholyman » 16 Feb 2014, 17:48

Just tried it on win7 64bit with ahk 1.1.14.03 32bit and IE 11 using this test script

Code: Select all

#SingleInstance, Force


^t::
msgbox % A_AhkVersion
TabActivate("Google", "ahk_class IEFrame")
ListLines
return

TabActivate(TabName, WinTitle) {
    ;~ Set Constants / Static Variables
    static  UIA_ControlTypePropertyId := 30003
        ,   UIA_NamePropertyId := 30005
        ,   TabItem := 50019
        ,   VT_I4 := 3
        ,   VT_BSTR := 8
        ,   TreeScope_Descendants := 0x4
        ,   uia := UIA_Interface()
        ,   Condition1

    ;~ Get Internet Explorer UIA Element
    WinGet, hwnd, ID, %WinTitle%
    ie := uia.ElementFromHandle(hwnd)
    
    ;~ Set Conditions to find sub-element (TabItem)
    if Not Condition1
        Condition1 := uia.CreatePropertyCondition(UIA_ControlTypePropertyId, TabItem, VT_I4)
    Condition2 := uia.CreatePropertyCondition(UIA_NamePropertyId, TabName, VT_BSTR)
    AndCondition := uia.CreateAndCondition(Condition1,Condition2)
    
    ;~ Query for the first matching sub-element
    tab := ie.FindFirst(AndCondition, TreeScope_Descendants)
    
    ;~ Access the Legacy Pattern
        ;~ Legacy Pattern is similar to the Accessible Interface, which has the DoDefaultAction method (Press)
    legacy := tab.GetCurrentPatternAs("LegacyIAccessible")
    legacy.DoDefaultAction()
}

#Include UIA_Interface.ahk
but when pressing the hotkey i get the version box but after that all the script seems to do is hang and then Exit with this Exit code: -1073741819
Also check out:
Courses on AutoHotkey

My Autohotkey Blog
:dance:

User avatar
jethrow
Posts: 188
Joined: 30 Sep 2013, 19:52
Location: Iowa

Re: Internet Explorer / FireFox - Activate Tab (UIA - 64bit

Post by jethrow » 18 Feb 2014, 01:44

:roll: meh - the UIA Library is currently only 64bit compatible.

User avatar
fump2000
Posts: 313
Joined: 04 Oct 2013, 17:31

Re: Internet Explorer / FireFox - Activate Tab (UIA - 64bit

Post by fump2000 » 18 Feb 2014, 17:25

Could you port it to 32bit or is that too much trouble?

User avatar
jethrow
Posts: 188
Joined: 30 Sep 2013, 19:52
Location: Iowa

Re: Internet Explorer / FireFox - Activate Tab (UIA - 64bit

Post by jethrow » 19 Feb 2014, 01:59

fump2000 wrote:Could you port it to 32bit or is that too much trouble?
I'd like to think it's not too much trouble to port it to 32bit - is that a good enough answer?

If anyone wants to point out where the library needs modified to make it 32bit compatible, I'd appreciate it. Such tasks are not my strong point.

Blackholyman wrote:...all the script seems to do is hang and then Exit with this Exit code: -1073741819
I believe that would be 0xc0000005, which is the code for STATUS_ACCESS_VIOLATION. In the library, for 32bit AHK, this error is occurring on the DllCall for the CreatePropertyCondition method of the UIA_Interface object. The error can be recreated with something as simple as:

Code: Select all

UIA_Interface().CreatePropertyCondition(30005, "str", 8)
This is the same for CreatePropertyConditionEx as well.

User avatar
jethrow
Posts: 188
Joined: 30 Sep 2013, 19:52
Location: Iowa

Re: Internet Explorer / FireFox - Activate Tab (UIA - 64bit

Post by jethrow » 22 Feb 2014, 01:28

Could someone test whether this issue exists on 32bit windows? Ideally WIN7 or XP SP3.

User avatar
fump2000
Posts: 313
Joined: 04 Oct 2013, 17:31

Re: Internet Explorer / FireFox - Activate Tab (UIA - 64bit

Post by fump2000 » 22 Feb 2014, 05:11

With 64 bit compatible you mean the AHK version or do you mean the OS? I have here only Win7 x64.

User avatar
haichen
Posts: 631
Joined: 09 Feb 2014, 08:24

Re: Internet Explorer / FireFox - Activate Tab (UIA - 64bit

Post by haichen » 23 Feb 2014, 09:36

Does not work on Win 8.1 - no error message ( I tried tabActivate from Blackholymans script above).
On win xp sp3 (virtual machine) i get a messagebox:
---------------------------
AutoHotkey v1.1.14.03: AutoHotkey.exe - Fehler in Anwendung
---------------------------
Die Anweisung in "0x004010e3" verweist auf Speicher in "0x00000017". Der Vorgang
"read" konnte nicht auf dem Speicher durchgeführt werden.

Klicken Sie auf "OK", um das Programm zu beenden.
---------------------------
Google translates this to
error in application
---------------------------
The instruction at "0x004010e3" referenced memory at "0x00000017". The process
"read" could not be performed on the memory.
I hope this helps

haichen

User avatar
jethrow
Posts: 188
Joined: 30 Sep 2013, 19:52
Location: Iowa

Re: Internet Explorer / FireFox - Activate Tab (UIA - 64bit

Post by jethrow » 23 Feb 2014, 16:24

fump2000 wrote:With 64 bit compatible you mean the AHK version or do you mean the OS? I have here only Win7 x64.
64bit AHK - which would then limit the user to a 64bit OS. I already know the issue exists using 32bit AHK on 64bit Win7. I've seen a page that mentions Pointer Truncation as an issue, but I'm not sure if that would be involved at all.
haichen wrote:Does not work on Win 8.1 ...
I currently have no way of developing or testing Win 8.

guest3456
Posts: 3463
Joined: 09 Oct 2013, 10:31

Re: Internet Explorer / FireFox - Activate Tab (UIA - 64bit AHK)

Post by guest3456 » 02 Oct 2020, 10:14

does anyone know what needs to be done to make this 32 bit compatible?


malcev
Posts: 1769
Joined: 12 Aug 2014, 12:37

Re: Internet Explorer / FireFox - Activate Tab (UIA - 64bit AHK)

Post by malcev » 02 Oct 2020, 10:30

First of all You have to change sending structures with dllcall.
For example here
https://docs.microsoft.com/en-us/windows/win32/api/uiautomationclient/nf-uiautomationclient-iuiautomation-createpropertycondition
structure variant in 64 bit should be send as pointer but in 32 bit as 2 int64.
https://www.autohotkey.com/boards/viewtopic.php?p=329332#p329332

guest3456
Posts: 3463
Joined: 09 Oct 2013, 10:31

Re: Internet Explorer / FireFox - Activate Tab (UIA - 64bit AHK)

Post by guest3456 » 03 Oct 2020, 09:13

malcev wrote:
02 Oct 2020, 10:30
First of all You have to change sending structures with dllcall.
For example here
https://docs.microsoft.com/en-us/windows/win32/api/uiautomationclient/nf-uiautomationclient-iuiautomation-createpropertycondition
structure variant in 64 bit should be send as pointer but in 32 bit as 2 int64.
https://www.autohotkey.com/boards/viewtopic.php?p=329332#p329332
thank you, i will investigate.. i guess i'm going to have to finally learn about this virtual table stuff

i saw you recommend the UIA library in this thread, over the other neptercn one on github... do you have your own UIA lib that you are willing to share? or do you just hack things togeter when you need it?


malcev
Posts: 1769
Joined: 12 Aug 2014, 12:37

Re: Internet Explorer / FireFox - Activate Tab (UIA - 64bit AHK)

Post by malcev » 03 Oct 2020, 23:02

I use IAccessible for my needs.
UIA I have used only several times therefore I do not know much about it.

guest3456
Posts: 3463
Joined: 09 Oct 2013, 10:31

Re: Internet Explorer / FireFox - Activate Tab (UIA - 64bit AHK)

Post by guest3456 » 05 Oct 2020, 22:46

malcev wrote:
03 Oct 2020, 23:02
I use IAccessible for my needs.
UIA I have used only several times therefore I do not know much about it.
IAccessible Acc.ahk lib is very slow because you have to traverse all child elements yourself, and then compare name/value/type... few games i tested was like 500+ ms to search for an item

i was hoping UIA would be faster by using "createPropertyCondition" to use the api itself and let the api do the filtering


dbareis
Posts: 39
Joined: 04 Feb 2022, 17:57

Re: Internet Explorer / FireFox - Activate Tab (UIA - 64bit AHK)

Post by dbareis » 02 May 2022, 20:27

32 bit support seems like a waste of time to me given that Windows 11 is 64 bit (only).

User avatar
iilabs
Posts: 296
Joined: 07 Jun 2020, 16:57

Re: Internet Explorer / FireFox - Activate Tab (UIA - 64bit AHK)

Post by iilabs » 25 Jun 2023, 14:43

Hi UI Automators,

Ive been able to access below controls using regular AHK controlclick etc, but having trouble accessing this list box (EPListWnd1). Could I attempt such using UI Automation. My problem now is I am using Mouse and Mouse Click which becomes unreliable at times.

Code: Select all

Send, {F7}  ;Toggles this GUI with controls in 3rd Party program 
BlockInput, MouseMoveOn
Sleep, 200
WinGetPos, X, Y,,, ahk_class EPFloatingWindow
Coordmode, Mouse, Screen
SetControlDelay -1
ControlClick % DicomEditBox, % DicomTitle,,,, NA
sleep, 50
Sendinput,(0040,2017)
Sleep, 550
;ControlClick, EPListWnd1, % DicomTitle,,,, NA   ;DOES NOT WORK
Click, % X + 124 "," Y + 132  ; WORKS but unreliable
sleep, 550
ControlClick % ClipboardDicomButton, % DicomTitle,,,, NA
Sleep, 200
ER := Clipboard
;trim the value and add to variable
ERNumber := SubStr(ER,52,11)  ;Start and total length
SetControlDelay -1
ControlClick % CloseDicomButton, % DicomTitle,,,, NA
sleep, 200
Tooltip, % "Pert #" ERNumber
Clipboard := ""
Clipboard := "Pert #" ERNumber
Clipwait, 1
SetTimer, RemoveToolTip, -1000
BlockInput, MouseMoveOff
Return
Attachments
ClassNN.png
ClassNN.png (34.71 KiB) Viewed 1465 times

Post Reply

Return to “Scripts and Functions (v1)”