Code: Select all
F11::
SetTitleMatchMode, 2
if !hWnd := WinExist("Power BI Desktop") {
MsgBox Power BI Desktop window not found
ExitApp
}
UIAutomation_ClickButtonByName(hWnd, "Refresh")
Return
UIAutomation_ClickButtonByName(hWnd, name) {
static UIA_ControlTypePropertyId := 30003
, UIA_NamePropertyId := 30005
, UIA_ButtonControlTypeId := 50000
, UIA_InvokePatternId := 10000
, TreeScope_Descendants := 0x4
, VT_I4 := 0x3, VT_BSTR := 0x8
Automation2 := IUIAutomation2.Create()
Element := Automation2.ElementFromHandle(hWnd)
Condition1 := Automation2.CreatePropertyCondition(UIA_ControlTypePropertyId, UIA_ButtonControlTypeId, VT_I4)
pStr := DllCall("Oleaut32\SysAllocString", "WStr", name, "Ptr")
Condition2 := Automation2.CreatePropertyCondition(UIA_NamePropertyId, pStr, VT_BSTR)
AndCondition := Automation2.CreateAndCondition(Condition1.ptr, Condition2.ptr)
Element := Element.FindFirst(TreeScope_Descendants, AndCondition.ptr)
DllCall("Oleaut32\SysFreeString", "Ptr", pStr)
if !Element {
MsgBox Button not found
Return
}
Automation2.put_AutoSetFocus(false)
pInvokePattern := Element.GetCurrentPattern(UIA_InvokePatternId)
InvokePattern := new IUIAutomationInvokePattern(pInvokePattern)
InvokePattern.Invoke()
}
class IUIAutomation2 extends _InterfaceBase
{
Create() {
static CLSID_CUIAutomation8 := "{E22AD333-B25F-460C-83D0-0581107395C9}"
, IID_IUIAutomation2 := "{34723AFF-0C9D-49D0-9896-7AB52DF8CD8A}"
pIUIAutomation2 := ComObjCreate(CLSID_CUIAutomation8, IID_IUIAutomation2)
Return new IUIAutomation2(pIUIAutomation2)
}
ElementFromHandle(hwnd) {
hr := DllCall(this.VTable(6), "Ptr", this.ptr, "Ptr", hwnd, "PtrP", pIUIAutomationElement)
this.IsError(A_ThisFunc, hr)
Return new IUIAutomationElement(pIUIAutomationElement)
}
CreatePropertyCondition(propertyId, value, varType) {
if (A_PtrSize = 4)
hr := DllCall(this.VTable(23), "Ptr", this.ptr, "Int", propertyId, "Int64", varType, "Int64", value, "PtrP", pIUIAutomationCondition)
else {
VarSetCapacity(variant, 24, 0)
NumPut(varType, variant)
NumPut(value, variant, 8)
hr := DllCall(this.VTable(23), "Ptr", this.ptr, "Int", propertyId, "Ptr", &variant, "PtrP", pIUIAutomationCondition)
}
this.IsError(A_ThisFunc, hr)
Return new IUIAutomationCondition(pIUIAutomationCondition)
}
CreateAndCondition(condition1, condition2) {
hr := DllCall(this.VTable(25), "Ptr", this.ptr, "Ptr", condition1, "Ptr", condition2, "PtrP", pIUIAutomationCondition)
this.IsError(A_ThisFunc, hr)
Return new IUIAutomationCondition(pIUIAutomationCondition)
}
put_AutoSetFocus(bool) {
hr := DllCall(this.VTable(59), "Ptr", this.ptr, "UInt", bool)
this.IsError(A_ThisFunc, hr)
Return hr
}
}
class IUIAutomationElement extends _InterfaceBase
{
FindFirst(scope, condition) {
hr := DllCall(this.VTable(5), "Ptr", this.ptr, "Int", scope, "Ptr", condition, "PtrP", pIUIAutomationElement)
this.IsError(A_ThisFunc, hr)
Return pIUIAutomationElement ? new IUIAutomationElement(pIUIAutomationElement) : 0
}
GetCurrentPattern(patternId) {
hr := DllCall(this.VTable(16), "Ptr", this.ptr, "Int", patternId, "PtrP", pPatternObject)
this.IsError(A_ThisFunc, hr)
Return pPatternObject
}
}
class IUIAutomationCondition extends _InterfaceBase
{
}
class IUIAutomationInvokePattern extends _InterfaceBase
{ ; UIA_InvokePatternId := 10000
Invoke() {
hr := DllCall(this.VTable(3), "Ptr", this.ptr)
this.IsError(A_ThisFunc, hr)
Return hr
}
}
class _InterfaceBase
{
__New(ptr) {
this.ptr := ptr
}
__Delete() {
ObjRelease(this.ptr)
}
VTable(idx) {
Return NumGet(NumGet(this.ptr + 0) + A_PtrSize*idx)
}
IsError(method, result, exc := true) {
if (result = 0)
Return 0
error := StrReplace(method, ".", "::") . " failed.`nResult: "
. ( result = "" ? "No result" : this.SysErrorToText(Format("{:#x}", result & 0xFFFFFFFF)) )
. "`nErrorLevel: " . ErrorLevel
if !exc
Return error
throw error
}
SysErrorToText(ErrorNum := "")
{
static flags := (FORMAT_MESSAGE_ALLOCATE_BUFFER := 0x100) | (FORMAT_MESSAGE_FROM_SYSTEM := 0x1000)
(ErrorNum = "" && ErrorNum := A_LastError)
DllCall("FormatMessage", "UInt", flags, "UInt", 0, "UInt", ErrorNum, "UInt", 0, "PtrP", pBuff, "UInt", 512, "Str", "")
str := StrGet(pBuff), DllCall("LocalFree", "Ptr", pBuff)
Return str? str : ErrorNum
}
}
Launch the script as administrator too.
Edited.