Hello all,
my goal is to observe a open application and read the visual text, filter for a specific value and based on that value to refresh a sidebar using an http call.
This code is working but it is my first AutoIt Coding Experience and this is why I am asking you for any potential code optimization, as I have to observe many more Windows and in order to make this clean I ask for your assistance.
So the goals are:
Run invisible in the Background
Observe open Window of Application (only do something if the Windows is open)
Read Visual Text and get specific value (Vendor Number)
If this Number changes, update the sidebar using http call
Here is the code:
#include "Array.au3"
#include "WinHttp.au3"
#include "WinAPI.au3"
#include "WindowsConstants.au3"
#include "StringConstants.au3"
Global $lieferant, $firstRun, $lieferantChanged
$firstRun = True
Func observe()
While not WinExists("Test2 Rechnungskontrolle (Verbuchung)")
Sleep(250)
WEnd
While WinExists("Test2 Rechnungskontrolle (Verbuchung)")
Sleep(250)
if($firstRun) Then
getLieferant()
$firstRun = False
EndIf
isChanged()
getLieferant()
Wend
EndFunc
Func getLieferant()
if(WinExists("Test2 Rechnungskontrolle (Verbuchung)")) Then
local $strString = WinGetText("Test2 Rechnungskontrolle (Verbuchung)")
local $arrResult = StringRegExp($strString, '(?s)&Adresse.*?\d+.*?(\d+)', $STR_REGEXPARRAYGLOBALMATCH)
If IsArray($arrResult) Then $lieferant = $arrResult[0]
Return $lieferant
Else
observe()
EndIf
EndFunc
Func isChanged()
if(WinExists("Test2 Rechnungskontrolle (Verbuchung)")) Then
local $strString = WinGetText("Test2 Rechnungskontrolle (Verbuchung)")
local $arrResult = StringRegExp($strString, '(?s)&Adresse.*?\d+.*?(\d+)', $STR_REGEXPARRAYGLOBALMATCH)
If IsArray($arrResult) Then $lieferantChanged = $arrResult[0]
If $lieferantChanged == $lieferant Then
getLieferant()
Else
;MsgBox(0,"Changed",$lieferantChanged)
ConsoleWrite($lieferantChanged)
refreshSidebar($lieferantChanged)
getLieferant()
EndIf
Else
observe()
EndIf
EndFunc
Func refreshSidebar($vendorID)
Opt("MustDeclareVars", 1)
; Initialize and get session handle
Global $hOpen = _WinHttpOpen()
; Get connection handle
Global $hConnect = _WinHttpConnect($hOpen, "http://MKSRVAPP04:25024")
Local $hRequest = _WinHttpSimpleRequest($hConnect, "GET", "api/client/sidebar/sendCommand?user=MULTIKRAFT\" & @UserName & "&call=OBJ:Vendor@@SELECT@@IDX:Vendor%20ID=" & $vendorID)
; Close handles
_WinHttpCloseHandle($hConnect)
_WinHttpCloseHandle($hOpen)
EndFunc
observe()
Thank you very much!
Best Regards
Kenan