Jump to content

Need help with AutoIT script


Recommended Posts

Hi, this is a simple script to rotate a "List" file of graphics. The graphics change at 15 second intervals. The List.txt file can contain up to 20 graphics to rotate. The problem is that the script will only rotate 2, not the whole List.txt file. Any help will be appreciated. Here is the code.

; A simple timed OIS Window Viewer

#include <GUIConstants.au3>

#include <Array.au3>

$wintitle = "Aspen"

HotKeySet("{ESC}", "Quit")

GUICreate("OIS Window Viewer", 250, 150)

$Label = GUICtrlCreateLabel("OIS Window Viewer is Running...", 20, 20)

$Filename = "List.txt"

$file = FileOpen($Filename, 0)

; Check if file opened for reading OK

If $file = -1 Then

MsgBox(0, "Error", "Unable to open file.")

Exit

EndIf

$count=0

Dim $Line[20]

; Read in user time delay and convert to milliseconds

$delay = FileReadLine($Filename,2)*1000

If $delay = "" Then

$delay = 15000

EndIf

; Read in lines of text

For $i=0 to 19

$Line[$i]= FileReadLine($Filename,$i+3)

If $Line[$i]<>"" then $count=$count+1

Next

FileClose($file)

$Label = GUICtrlCreateLabel("Screen will update every "& $delay/1000 & " seconds", 20, 50)

$Label = GUICtrlCreateLabel("Viewing " & $count & " files (Refer to List.txt)", 20, 80)

$Label = GUICtrlCreateLabel("Hit ESC button to EXIT viewer", 20, 110)

; display the GUI

GUISetState()

Opt("WinTitleMatchMode", 4)

WinSetState("classname=Shell_TrayWnd", "", @SW_HIDE)

MouseMove(@desktopwidth,0,1)

sleep(30000)

; Close any open OIS windows

Opt("WinTitleMatchMode", 2)

while WinExists($wintitle, "")

WinClose($wintitle, "")

wend

sleep(5000)

; Open and Maximize File

Run(@ComSpec & ' /c "' & $Line[0] & '"', '', @SW_HIDE)

winwaitactive($wintitle,"")

sleep(2000)

WinActivate($wintitle,"")

sleep(1000)

WinSetState($wintitle,"", @SW_RESTORE)

WinMove($wintitle, "", 0, 0, @DesktopWidth, @DesktopHeight)

sleep(1000)

Send( "!{-}{x}")

sleep(1000)

$item = WinMenuSelectItem($wintitle, "", "&View", "&Status Bar")

ControlDisable($wintitle, "", $item)

sleep(100)

$item = WinMenuSelectItem($wintitle, "", "&View", "&Toolbar", "&Standard" )

ControlDisable($wintitle, "", $item)

sleep(100)

$item = WinMenuSelectItem($wintitle, "", "&View", "&Toolbar", "&Global Timeline" )

ControlDisable($wintitle, "", $item)

sleep(100)

$item = WinMenuSelectItem($wintitle, "", "&View", "&Toolbar", "&Visual Basic" )

ControlDisable($wintitle, "", $item)

sleep(100)

sleep(15000)

For $j=2 to $count

Run(@ComSpec & ' /c "' & $Line[$j-1] & '"', '', @SW_HIDE)

sleep(1000)

WinActivate($wintitle,"")

sleep(1000)

Send( "!{-}{x}")

Sleep(10000)

Next

; Timed cycle through screens

$start = TimerInit()

$k=1

While 1

If TimerDiff($start) > $delay Then

WinActivate($wintitle,"")

sleep(50)

Send("!{TAB}")

MouseMove(@desktopwidth,0,1)

$start = TimerInit()

If WinExists("reboot", "") Then

Shutdown(6) ;Force a reboot

EndIf

EndIf

$msg = GUIGetMsg()

Select

Case $msg = $GUI_EVENT_CLOSE

ExitLoop

Quit()

EndSelect

WEnd

Func Quit()

$item = WinMenuSelectItem($wintitle, "", "&View", "&Status Bar" )

ControlEnable($wintitle, "", $item)

sleep(100)

$item = WinMenuSelectItem($wintitle, "", "&View", "&Toolbar", "&Standard" )

ControlEnable($wintitle, "", $item)

sleep(100)

$item = WinMenuSelectItem($wintitle, "", "&View", "&Toolbar", "&Global Timeline" )

ControlEnable($wintitle, "", $item)

sleep(100)

$item = WinMenuSelectItem($wintitle, "", "&View", "&Toolbar", "&Visual Basic" )

ControlEnable($wintitle, "", $item)

sleep(100)

Opt("WinTitleMatchMode", 2)

WinClose($wintitle,"")

Opt("WinTitleMatchMode", 4)

WinSetState("classname=Shell_TrayWnd", "", @SW_SHOW)

Exit 0

EndFunc

Link to comment
Share on other sites

Cleaned it up a bit and added code tags for readibility

#include <GUIConstants.au3>
#include <Array.au3>

Global $wintitle = "Aspen"
Global $Filename = "List.txt"
Global $delay
Global $GUI_HANDLE

Opt("WinTitleMatchMode", 2)
HotKeySet("{ESC}", "Quit")

;#######File open and reading#######
Global $Line = ReadInFile ($FileName)


;#######GUI Creation#######
; The $Label variables were being sequentially overwritten, and can be removed without issue
$GUI_HANDLE = GUICreate("OIS Window Viewer", 250, 150)
GUICtrlCreateLabel("OIS Window Viewer is Running...", 20, 20)
GUICtrlCreateLabel("Screen will update every "& $delay/1000 & " seconds", 20, 50)
GUICtrlCreateLabel("Viewing " & Ubound ($Line) - 1 & " files  (Refer to List.txt)", 20, 80)
GUICtrlCreateLabel("Hit ESC button to EXIT viewer", 20, 110)
GUISetState()  

;#######Setup the desktop#######
;Hide the tray and the cursor


;Opt("WinTitleMatchMode", 4) <= This line is unnessesary
; as it will automatically match in that form if you give it the explicit 'classname' style window reference
WinSetState("classname=Shell_TrayWnd", "", @SW_HIDE)
MouseMove(@desktopwidth,0,1)

sleep(30000)

; Close any open OIS windows <= What does OIS stand for?
; Just realized that this is to make sure only one instance is running at a time. Still curious about what OIS stands for...
while WinExists($wintitle, "")
   WinClose($wintitle, "")
wend

sleep(5000)

; Open and Maximize File
Run(@ComSpec & ' /c "' & $Line[0] & '"', '', @SW_HIDE)

; Not really sure what is going on here
winwaitactive($wintitle,"")
sleep(2000)
WinActivate($wintitle,"")
sleep(1000)

;Show and resize the window
WinSetState($wintitle,"", @SW_RESTORE)
WinMove($wintitle, "", 0, 0, @DesktopWidth, @DesktopHeight)
sleep(1000)
Send( "!{-}{x}")
sleep(1000)


;#######Disable a mess of controls#######
DisableControls ()

sleep(15000)

;This is outside the main loop, so this will run only once
For $j=2 to Ubound ($Line)-1 
    Run(@ComSpec & ' /c "' & $Line[$j-1] & '"', '', @SW_HIDE)
    sleep(1000)
    WinActivate($wintitle,"")
    sleep(1000)
    Send( "!{-}{x}")
    Sleep(10000)
Next

; Timed cycle through screens
$start = TimerInit()
$k=1

While 1
    If TimerDiff($start) > $delay Then
        WinActivate($wintitle,"")
        sleep(50)
        Send("!{TAB}") ;<= PROBLEM IS HERE, Alt-tab only switches between the two most recent windows. In this case the first and second window opened
        MouseMove(@desktopwidth,0,1)
        $start = TimerInit()
            If WinExists("reboot", "") Then
                Shutdown(6)  ;Force a reboot
            EndIf
    EndIf
    
    If GUIGetMsg() = $GUI_EVENT_CLOSE Then Quit()
    ;ExitLoop Not required, Quit() has an exit in it. In this configuration it will jump over the call to Quit()
    ;Restructured as an if statement for readability. Elimintated the needless $msg variable.
WEnd

;####### FUNCTIONS #######
Func ReadInFile ($file)
    ;Reads in the file, not limited to 20 lines.
    Local $fileHandle = FileOpen($file, 0)

    ; Check if file opened for reading OK
    If $fileHandle = -1 Then
        MsgBox(0, "Error", "Unable to open file.")
        Exit
    EndIf

    ; Read in user time delay and convert to milliseconds
    $delay = FileReadLine($fileHandle, 2) * 1000
    If $delay = "" Then
        $delay = 15000
    EndIf

    ; Read in lines of text
    Local $currentLine = 0
    Local $lines[20] ;Initial size guess for the file
    While @error <> -1 ;Read until you get to the EOF
        Local $thisLine = FileReadLine($fileHandle) ;FileReadLine will automatically read the next line, so you don't have to offset it. Also: better performance

        If $thisLine <> "" then ;Only add lines with stuff on them
            ;Resize if we've run out of room
            if $currentLine >= Ubound ($lines) Then ReDim $lines [$currentLine + 10]
        
            ;Add it to the end of the list
            $lines [$currentLine] = $thisLine
            $currentLine = $currentLine + 1
        EndIf
    WEnd
    FileClose($fileHandle)

    ReDim $lines [$currentLine] ;Trim off the empty stuff at the end of the array
    Return  $lines
EndFunc

Func DisableControls ()
    Local $item = WinMenuSelectItem($wintitle, "", "&View", "&Status Bar")
    ControlDisable($wintitle, "", $item)

    $item = WinMenuSelectItem($wintitle, "", "&View", "&Toolbar", "&Standard" )
    ControlDisable($wintitle, "", $item)

    $item = WinMenuSelectItem($wintitle, "", "&View", "&Toolbar", "&Global Timeline" )
    ControlDisable($wintitle, "", $item)

    $item = WinMenuSelectItem($wintitle, "", "&View", "&Toolbar", "&Visual Basic" )
    ControlDisable($wintitle, "", $item)
EndFunc

;Handles re-enabling the controls affected DisableControls(), closes the GUI, and exits the script
Func Quit()
    ;Deleted the sleep (100)s, for setting this kinda stuff it shouldn't make any difference
    Local $item = WinMenuSelectItem($wintitle, "", "&View", "&Status Bar" )
    ControlEnable($wintitle, "", $item)

    $item = WinMenuSelectItem($wintitle, "", "&View", "&Toolbar", "&Standard" )
    ControlEnable($wintitle, "", $item)

    $item = WinMenuSelectItem($wintitle, "", "&View", "&Toolbar", "&Global Timeline" )
    ControlEnable($wintitle, "", $item)

    $item = WinMenuSelectItem($wintitle, "", "&View", "&Toolbar", "&Visual Basic" )
    ControlEnable($wintitle, "", $item)
    
    WinClose($wintitle,"")
    WinWaitClose ($wintitle, "") ;Added this, in case the sleeps were to prevent premature exiting of the script
    
    WinSetState("classname=Shell_TrayWnd", "", @SW_SHOW)
    GUIDelete ($GUI_HANDLE )
    Exit 0
EndFunc

Edit: Slowly cleaning it up as I try and grok the problem.

Edit: Chunked out some functions to make it easier to read

Edit: Fixed the GUIDelete error (For some reason I was passing the GUICreate function, and not the handle to the GUI)

Edited by Fulano

#fgpkerw4kcmnq2mns1ax7ilndopen (Q, $0); while ($l = <Q>){if ($l =~ m/^#.*/){$l =~ tr/a-z1-9#/Huh, Junketeer's Alternate Pro Ace /; print $l;}}close (Q);[code] tag ninja!

Link to comment
Share on other sites

Found the issue. It's on line 84 if you copy the modified version into Scite.

Took me a bit to figure out what was going on, and I still might be wrong, but it looks like your script opens all of the various files in the background and tries to cycle through them with alt-tab.

The problem that you are having is that alt-tab only cycles through the last two windows, which is why you are getting the odd behavior.

I would probably recommend modifying the approach so you only open 1 window initially, the when it's time to switch, open the next with @SW_HIDE, maximize it and bring it to the front. After that you are free to close the other window behind it and you save some memory as well.

Hope this helps :idea:

Edit: The script I posted above still has the problem, since fixing it would require a major rewrite I leave that to you. If you run into problems with any particular part I'd love to help :)

Edited by Fulano

#fgpkerw4kcmnq2mns1ax7ilndopen (Q, $0); while ($l = <Q>){if ($l =~ m/^#.*/){$l =~ tr/a-z1-9#/Huh, Junketeer's Alternate Pro Ace /; print $l;}}close (Q);[code] tag ninja!

Link to comment
Share on other sites

Thanks, Fulano, for your help. I'm getting an error when I try to run the script. Here's the message:

---------------------------

AutoIt Error

---------------------------

Line 168 (File "C:\Documents and Settings\trherzig\Desktop\OIS Window Viewer\OIS Window Viewer.au3"):

GUIDelete (GUICreate)

GUIDelete (GUICreate^ ERROR

Error: Missing separator character after keyword.

---------------------------

OK

---------------------------

I'm so new to AutoIT that I don't know what this means.

BTW, OIS is my company's version of Aspentech software hereMy link

We use it to view our control system's data. The graphics are proprietary and contain live data.

Randy

Link to comment
Share on other sites

Just a follow up to the above post, here is the result of syntax checking...

>AutoIt3 Syntax Checker v1.54.8 Copyright © Tylo 2007

C:\Documents and Settings\trherzig\Desktop\OIS Window Viewer\OIS Window Viewer.au3(21,40) : WARNING: $count: possibly used before declaration.

GUICtrlCreateLabel("Viewing " & $count &

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^

C:\Documents and Settings\trherzig\Desktop\OIS Window Viewer\OIS Window Viewer.au3(168,25) : ERROR: syntax error

GUIDelete (GUICreate)

~~~~~~~~~~~~~~~~~~~~~~~~^

C:\Documents and Settings\trherzig\Desktop\OIS Window Viewer\OIS Window Viewer.au3(169,11) : ERROR: syntax error

Exit 0EndFunc

~~~~~~~~~~^

C:\Documents and Settings\trherzig\Desktop\OIS Window Viewer\OIS Window Viewer.au3(21,40) : ERROR: $count: undeclared global variable.

GUICtrlCreateLabel("Viewing " & $count &

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^

C:\Documents and Settings\trherzig\Desktop\OIS Window Viewer\OIS Window Viewer.au3 - 3 error(s), 1 warning(s)

>Exit code: 2 Time: 0.246

Link to comment
Share on other sites

Fixed:

C:\Documents and Settings\trherzig\Desktop\OIS Window Viewer\OIS Window Viewer.au3(21,40) : WARNING: $count: possibly used before declaration.

GUICtrlCreateLabel("Viewing " & $count &

Fixed:

C:\Documents and Settings\trherzig\Desktop\OIS Window Viewer\OIS Window Viewer.au3(168,25) : ERROR: syntax error

GUIDelete (GUICreate)

Fixed (Not sure why, but this one seems to happen to me alot when posting my scripts to the forum - source code does not have this error):

C:\Documents and Settings\trherzig\Desktop\OIS Window Viewer\OIS Window Viewer.au3(169,11) : ERROR: syntax error

Exit 0EndFunc

Fixed:

C:\Documents and Settings\trherzig\Desktop\OIS Window Viewer\OIS Window Viewer.au3(21,40) : ERROR: $count: undeclared global variable.

GUICtrlCreateLabel("Viewing " & $count &

Edited by Fulano

#fgpkerw4kcmnq2mns1ax7ilndopen (Q, $0); while ($l = <Q>){if ($l =~ m/^#.*/){$l =~ tr/a-z1-9#/Huh, Junketeer's Alternate Pro Ace /; print $l;}}close (Q);[code] tag ninja!

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...