Refresh a GUI using AutoIt - user-interface

I am making AutoIt code and one of the items on the GUI needs to be updated every few seconds, and I can seem to get it to do it. To make it simple I have written some code that shows the problem:
$num = 0
GUICreate("Example")
$Pic1 = GUICtrlCreateLabel($num, 10, 10)
GUISetState()
While 1
sleep(1000)
$num = $num + "1"
WEnd
If the code was working then the number would change, but it does not. How do I make it refresh?

The number is updating, but your data is not. Plus, you are mixing integers with strings.
Luckily AutoIt converts them automatically. But take care, because you will have problems in other programming languages.
Here you go:
Local $num = 0
Local $hGUI = GUICreate("Example")
Local $Pic1 = GUICtrlCreateLabel($num, 10, 10)
GUISetState()
Local $hTimer = TimerInit()
While 1
;sleep(1000)
If TimerDiff($hTimer) > 1000 Then
$num += 1
GUICtrlSetData($Pic1, $num)
$hTimer = TimerInit()
EndIf
If GUIGetMsg() = -3 Then ExitLoop
WEnd
P.S.: Avoid using sleep in these situations while they will pause your script.

This is easily done with AdLibRegister passing the function name and then 1000 milliseconds (1 second).
Local $num = 0
Local $hGUI = GUICreate("Example")
Local $Pic1 = GUICtrlCreateLabel($num, 10, 10)
Local $msg
GUISetState()
AdLibRegister("addOne", 1000)
While 1
$msg = GUIGetMsg()
Switch $msg
Case $GUI_EVENT_CLOSE
Exit
EndSwitch
WEnd
Func addOne ()
$num += 1
GUICtrlSetData($Pic1, $num)
EndFunc

You need to set the new data in the GUI using GUICtrlSetData. Simply modify your loop like this:
While 1
sleep(1000)
$num += 1
GUICtrlSetData($Pic1, $num)
WEnd
Note that I removed the double-quotes so that AutoIt handles the values as integers.

Related

how to rename files in a pair with a script

I have 10 pdf files in a folder and im trying to find a way to rename them as pairs using a script for it to look like something like this
file131.pdf = order_receipt_1000.pdf
file304.pdf = invoice_1000.pdf
file542.pdf = order_receipt_1001.pdf
file194.pdf = invoice_1001.pdf
and so on for the next 8 ones
maybe this gives you a good starting point. The language is Autoit. (www.autoitscript.com)
Currently the script looks into the folder where it is saved for *pdf files. Then an new array with the files found is created an the second column contains the time when the file has been created. After that the last For-loop renames the files in chunks of two files each.
#include <Array.au3>
#include <File.au3>
#include <MsgBoxConstants.au3>
Example()
Func Example()
Local $aFileList = _FileListToArray(#ScriptDir, '*.pdf', Default, True) ; change #scriptDir to the path you need
If #error = 1 Then
MsgBox($MB_SYSTEMMODAL, "", "Path was invalid.")
Exit
EndIf
If #error = 4 Then
MsgBox($MB_SYSTEMMODAL, "", "No file(s) were found.")
Exit
EndIf
_ArrayDisplay($aFileList, "Files FOUND")
Local $a[UBound($aFileList)][2]
For $i = 0 To UBound($aFileList) - 1
$a[$i][0] = $aFileList[$i]
$a[$i][1] = FileGetTime($aFileList[$i], $FT_CREATED, $FT_STRING) ; change FT_CREATED to what you need (created, modified, ...)
Next
_ArraySort($a, 0, 0, 0, 1)
_ArrayDisplay($a, "Sorted FileList")
Local $startcount = 1000, $y = 0
For $i = 1 To UBound($a) - 1 Step 2
FileMove($a[$i][0], 'order_receipt_' & $startcount + $y & '.pdf')
FileMove($a[$i + 1][0], 'invoice_' & $startcount + $y & '.pdf')
$y += 1
Next
EndFunc ;==>Example

Reading from check boxes doesn't work

I want to iterate through check boxes to get the caption text from each one. I have this code but it is not working. Could someone tell me whats wrong?
Is that because later in the For loop I am using $i to iterate through other things? But it doesn't even run the Send() command. Does AutoIt increment the $i variable automatically?
For $i = 1 to 64
If GUICtrlRead("$Checkbox" & $i,0) = $GUI_CHECKED Then
Local $checkboxtext = GUICtrlRead($Checkbox[$i], 1)
Local $checkboxtextsplit = StringSplit( $checkboxtext, "/")
$instanz = $checkboxtextsplit[1]
$favorite = "F" & $checkboxtextsplit[2]
$position = $checkboxtextsplit[3]
;Select actual Instance from Checkbox Name.
If $instanz = "1" Then
WinActivate($handle1)
Else
WinActivate($handle2)
EndIf
Send("{" & $favorite & "}")
;...
EndIf
Next
I was providing GUICtrlRead() its parameters the wrong way. Instead of:
If GUICtrlRead("$Checkbox" & $i, 0) = $GUI_CHECKED Then
Local $checkboxtext = GUICtrlRead($Checkbox[$i], 1)
It should be:
If GUICtrlRead($Checkbox & $i, 0) = $GUI_CHECKED Then
Local $checkboxtext = GUICtrlRead($Checkbox & $i, 1)
To retrieve a Checkbox checked/un-checked state use:
If GUICtrlRead($Checkbox & $i, 0) = $GUI_CHECKED Then ...
To read text of a Checkbox use:
$checkboxtext = GUICtrlRead($Checkbox & $i, 1)

Looping through WinList() result takes much time

I get a list of open windows and check if it contains a certain title. It is working but takes more than 10 seconds. Why does it take so long, what is wrong with my code?
Looks like WinList() doesn't list only visible windows.
$title = 0
$begintime = TimerInit()
MsgBox($MB_OK, "Timer", "Timer inicialized")
While $title = 0
$aList = WinList()
For $x = 1 To $aList[0][0]
;Check if a window with this title exists.
if $aList[$x][0] = "WindowTitle" Then
If $lastruntitle = "WindowTitle" Then
$title = 1
ExitLoop(2)
Else
SendMail4()
$lastruntitle = "WindowTitle"
$title = 1
ExitLoop(2)
EndIf
EndIf
Next
WEnd
Simple solution for your task is:
#include <Array.au3>
While 1
$aList = WinList()
_ArraySearch($aList, "WindowTitle", 0, 0, 0, 0, 1, 0)
If Not #error Then
MsgBox(0,"","Window found!")
Exit
EndIf
Sleep(100)
WEnd

Button = Website Link in AutoIT

I have made a button in AutoIT for this program we are making for work we will call it $okmystery and I want $okmystery to like to my company website. Here is a snippet of the code I have so far:
Dim $msg
GUISetState()
While 1
$msg = GUIGetMsg()
Select
Case $msg = $GUI_EVENT_CLOSE
ExitLoop
Case $msg = $okbutton
; Minimize Current Window
WinSetState( $WINTITLE, "", #SW_MINIMIZE)
While Not BitAND(WinGetState($WINTITLE, ""), 16)
sleep( 250 )
WEnd
; Take Screen Shots and Logs
ScreenShotAndLogs()
; Compress Artifacts
If FileExists( $ZIPFILEPATH ) Then FileDelete( $ZIPFILEPATH )
_Zip_Create( $ZIPFILEPATH )
_Zip_AddFolderContents( $ZIPFILEPATH, $OUTPUTROOT )
DeleteOriginals()
; Restore main window
WinSetState( $WINTITLE, "", #SW_RESTORE)
;------------ Screen Shot
Case $msg = $okshot
; Minimize Current Window
WinSetState( $WINTITLE, "", #SW_MINIMIZE)
While Not BitAND(WinGetState($WINTITLE, ""), 16)
sleep( 250 )
WEnd
ScreenShot()
; Restore main window
WinSetState( $WINTITLE, "", #SW_RESTORE)
;----------------------------------
$okmystery = ShellExecute ("basic")
Run("Http://www.IT-Networks.org")
Case Default
; Do Nothing
EndSelect
WEnd
Exit( 0 )
It looks like you need to change the "$okmystery" case statement to match the other case statements (if those are all working like they're supposed to).
You can then try to ShellExecute() the url.
Case $msg = $okmystery
ShellExecute("Http://www.IT-Networks.org")
Here's a working example of a GUI with a button that opens your company website in your default web browser:
#include <GUIConstantsEx.au3>
Global $Button_1, $msg
GUICreate("Test GUI Button")
$okmystery = GUICtrlCreateButton("okmystery Button", 10, 30, 100)
GUISetState()
While 1
$msg = GUIGetMsg()
Select
Case $msg = $GUI_EVENT_CLOSE
ExitLoop
Case $msg = $okmystery
ShellExecute("Http://www.IT-Networks.org")
EndSelect
WEnd

How to Winwait for two windows simultaneously in AutoIt?

I would like to know if its possible to WinWaitActive for WindowWithThisTitle and WindowWithThatTitle at the same time. I'm executing a command and there could be a window telling me that the connection failed or a user/pass dialog coming up.
Is there another way doing it as this?
WinWaitActive("Title1", "", 5)
If(WinExists("Title1")) Then
MsgBox(0, "", "Do something")
Else
If(WinExists("Title2")) Then
MsgBox(0, "", "Do something else")
EndIf
EndIf
Because I don't want to have the timeout which could be more than 15 seconds.
A simpler solution might be to use a REGEX title in your WinWaitActive as defined here
You would then have something like this:
$hWnd = WinWaitActive("[REGEXPTITLE:(WindowWithThisTitle|WindowWithThatTitle)]")
If WinGetTitle($hWnd) = "WindowWithThisTitle" then
DoSomething()
Else
DoSomethingElse()
EndIf
How about something like this.
$stillLooking = True
While $stillLooking
$activeWindowTitle = WinGetTitle(WinActive(""))
If $activeWindowTitle == "Title1" Then
MsgBox(0, "", "Do something")
$stillLooking = False
ElseIf $activeWindowTitle == "Title2" Then
MsgBox(0, "", "Do something else")
$stillLooking = False
EndIf
sleep(5)
WEnd
Because I don't want to have the
timeout which could be more than 15
seconds.
WinWaitActive() doesn't have a timeout unless you specify one. You gave it a five second timeout but you could leave that off and it would wait forever.
You can use this Functions for two windows ..
; #FUNCTION# ====================================================================================================================
; Name...........: _2WinWait
; Description ...: Wait For Tow Windows .
; Syntax.........: _2WinWait ($FirstTitle,$SecondTitle,[$FirstText = "" ,[$SecondText = ""]] )
; Parameters ....: $FirstTitle - Title Of First Wondow
; $SecondTitle - Title Of Second Wondow
; $FirstText - Text Of First Wondow
; $SecondText - Text Of Second Wondow
; Return values .: Success - None
; Failure - Returns a 0 => If Your Titles Is Wrong
; Author ........: Ashalshaikh : Ahmad Alshaikh
; Remarks .......:
; Related .......:
; Link ..........;
; Example .......; No
; ===============================================================================================================================
Func _2WinWait ($FirstTitle,$SecondTitle,$FirstText = "" ,$SecondText = "" )
If $FirstTitle = "" Or $SecondTitle = "" Then
Return 0
Else
Do
Until WinExists ($FirstTitle,$FirstText) Or WinExists ($SecondTitle,$SecondText)
EndIf
EndFunc
; #FUNCTION# ====================================================================================================================
; Name...........: _2WinWait_Any
; Description ...: Wait For Tow Windows And Return Any Window Id Exists .
; Syntax.........: _2WinWait_Any ($FirstTitle,$SecondTitle,[$FirstText = "" ,[$SecondText = ""]] )
; Parameters ....: $FirstTitle - Title Of First Wondow
; $SecondTitle - Title Of Second Wondow
; $FirstText - Text Of First Wondow
; $SecondText - Text Of Second Wondow
; Return values .: Success - Number Of Window ==> 1= First Window , 2= Second Window
; Failure - Returns a 0 => If Your Titles Is Wrong
; Author ........: Ashalshaikh : Ahmad Alshaikh
; Remarks .......:
; Related .......:
; Link ..........;
; Example .......; No
; ===============================================================================================================================
Func _2WinWait_Any ($FirstTitle,$SecondTitle,$FirstText = "" ,$SecondText = "" )
If $FirstTitle = "" Or $SecondTitle = "" Then
Return 0
Else
Do
Until WinExists ($FirstTitle,$FirstText) Or WinExists ($SecondTitle,$SecondText)
If WinExists ($FirstTitle,$FirstTexit) Then
Return 1
Else
Return 2
EndIf
EndIf
EndFunc
for more with examples
I'm fairly new to autoit and the programming world in general and I had this same dilemma. Luckily I figured out a straight fwd way to do it:
Do
$var1 = 0
If WinGetState("Document Reference","") Then
$var1 = 1
ElseIf WinGetState("Customer Search","") Then
$var1 = 1
EndIf
Until $var1 = 1
So it'll stay in the loop until it finds the window and sets $var1 to 1. There's probably easier ways (I'm sure developers are gasping at this) but this is straight fwd enough for me.
You can create an infinite while loop with if statements in there:
#include <MsgBoxConstants.au3>
Example()
Func Example()
While 1
; Test if the window exists and display the results.
If WinExists("Windows Security") Then
Local $hWnd = WinWaitActive("Windows Security", "", 2000)
ControlSetText($hWnd, "", "[CLASS:Edit; INSTANCE:1]", "hel233")
ControlClick("Windows Security","","[CLASS:Button; INSTANCE:2]")
Sleep(5000)
EndIf
; Test if the window exists and display the results.
If WinExists("Spread the Word") Then
'The line below will wait until the window is active, but we don't need that
'Local $hWnd = WinWaitActive("Spread the Word", "", 2000)
WinClose("Spread the Word")
Sleep(5000)
EndIf
wend
EndFunc

Resources