Jump to content

Proper Design Of A Script


Recommended Posts

Hi all,

I just started to use AutoIt and it is SWEET :whistle:

I am tring to run a script in the background to monitor for a message box to popup. I then get the button text and verify that the button has the same text as I expected. If that is the case I click the button. Now I got all that to work by using a while loop and just poll for the window. This works but to reliably get the buttons text I have to make sure the window has focus. This creates a window that is basicly always on top. You can't do anything else with the machine because I keep haveing to give the window focus.

How can I get this to work a better way so that it is completely transparent and the machine can still be used?

Clear as mud huh?

Edited by RJP Computing

-RyanRJP Computinghttp://rjpcomputing.com/

Link to comment
Share on other sites

I presently do just that. I haven't had reliable luck getting the button text back if the window isn't the one with focus.

Yes the AutoIt Spy has been a HUGE help!

I oversimplified my problem but here is the whole script. I use ini files and such.

;
; AutoIt Version: 3.0
; Language:       English
; Platform:       Win9x/NT
; Author:         Ryan Pusztai (ryan.pusztai@jci.com)
;
; Script Function:
;   Checks for a specific window then sends a click event to the specified button
;
AutoItSetOption("TrayIconDebug", 1)

; Give the user a chance to quit
$answer = MsgBox (4, "AutoWinClose", "Run script?")

If $answer = 7 Then
    Exit   ;User didn't want to run script
EndIf


; Press Esc to terminate script, Pause/Break to pause

Global $Paused
Global $NumWindows
Global $SearchMode
Global $WinTitle[10]
Global $ButtonText[10]

HotKeySet("{PAUSE}", "TogglePause")
HotKeySet("{ESC}", "Terminate")

If FileExists(@ScriptDir & "\config.ini") Then
;MsgBox(4096, "",@ScriptDir & "\config.ini exists.")
    $NumWindows = IniRead(@ScriptDir & "\config.ini", "Setup", "NumWindows", "1")
    $SearchMode = IniRead(@ScriptDir & "\config.ini", "Setup", "SearchMode", "2")
    
    AutoItSetOption("WinTitleMatchMode", Number($SearchMode)) ; 2 = Match any substring in the title
    
    For $i = 0 To $NumWindows
  $WinTitle[$i] = IniRead(@ScriptDir & "\config.ini", "Windows", "WindowTitle" & String($i+1), "<Change>")
  $ButtonText[$i] = IniRead(@ScriptDir & "\config.ini", "Windows", "Button" & String($i+1), "<Change>")
    Next
    
Else
    IniWrite(@ScriptDir & "\config.ini", "Setup", "NumWindows", "1")
    IniWrite(@ScriptDir & "\config.ini", "Setup", "SearchMode", "2")
    
    $file = FileOpen(@ScriptDir & "\config.ini", 1)
  
    ; Check if file opened for writing OK
  If $file = -1 Then
     MsgBox(0, "Error", "Unable to open file.")
     Exit
  EndIf
  
  FileWriteLine($file, "; SearchMode alters the method that is used to match window titles." & @LF)
  FileWriteLine($file, "; 1 = Match the title from the start (default)" & @LF)
  FileWriteLine($file, "; 2 = Match any substring in the title" & @LF)
  FileWriteLine($file, "; 3 = Exact title match" & @LF)
  FileWriteLine($file, "; 4 = Advanced mode" & @LF)
  
    FileClose($file)
    
    IniWrite(@ScriptDir & "\config.ini", "Windows", "WindowTitle1", "<Change>")
    IniWrite(@ScriptDir & "\config.ini", "Windows", "Button1", "<Change>")
    
    MsgBox(4096,"", "Please edit " & @ScriptDir & "\config.ini with your proper settings.")
    Run("notepad.exe " & @ScriptDir & "\config.ini")
    Exit
EndIf



;SplashTextOn("myMsgScreen", "Running")
;WinShow("pauseSpecialScrn", "", @SW_HIDE)

While 1;infinite loop
    If $Paused then
  Sleep(1000)
    Else
  PressButton ($WinTitle[0], "Button1", $ButtonText[0])
  PressButton ($WinTitle[1], "AfxOleControl422", $ButtonText[1])
  Sleep(1)
    ; stuff to do goes here ...
    EndIf
Wend

Func TogglePause()
    $Paused = NOT $Paused
    
;If $Paused then
    ;SplashTextOn("myMsgScreen", "Paused ...")        
;Else
    ;SplashTextOn("myMsgScreen", "Running ...")
;EndIf
EndFunc

Func Terminate()    
    If MsgBox(52, "AutoWinClose", "Close AutoWinClose?") = 6 Then Exit
EndFunc

Func PressButton($Title, $Class, $ButText)
    If WinExists($Title) Then
  WinActivate($Title, "")
    ;ControlFocus($Title, "", $Class)
    $var = ControlGetText($Title, "", $Class)
    ;MsgBox(0, $Class & " Text", $ButText)
    ;MsgBox(0, $Class & " Text", $var)
  
  If StringInStr($var, $ButText) Then ControlClick($Title, "", $Class)
  
    Else
    ;MsgBox(0, $Title, "Window DOESN'T exists")
    EndIf
    
EndFunc
Thanks

-RyanRJP Computinghttp://rjpcomputing.com/

Link to comment
Share on other sites

Is there a way for us to reproduce the ControlGetText not working?

The script isn't completed but all you need to do is open some program and use that for your .ini file settings. Change those and then change the class name in the while loop. I was using message boxes to check the text and it was coming back with 1(@error).

Another quick question. Is this valid in AutoIt:

$var1 = "some cool text"
$var2 = "some cooler text"
If $var1 == $var2 then MsgBox (0, "", "They are equal")

PS. how long till the DLL version is finished?

Edited by RJP Computing

-RyanRJP Computinghttp://rjpcomputing.com/

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...