Jump to content
Sign in to follow this  
danharold

Simple Script Runner

Recommended Posts

danharold

I have a few scripts that I like to run on a periodic basis, and I'm also interested in viewing their console outs to make sure that everything is running smoothly.

I got a little tired of keeping multiple Scite windows open to monitor their console outs and rather than develop GUIs for all my scripts or compile them as console applications I made a general purpose GUI that I can point to any of my scripts.

I thought I would post it in case it would be useful to others. In my setup I have it compiled to EXE so I can just run multiples of them and point them to each script that they need to run.

As a bonus you can edit your scripts on the fly and they will continue to run on schedule without having to re-compile/etc.

#include <Date.au3>
#include <GUIEdit.au3>
#include <GUIConstantsEx.au3>
#include <ScrollBarConstants.au3>
#include <WindowsConstants.au3>
#include <ButtonConstants.au3>

$autoitexepath = "C:\Program Files (x86)\AutoIt3\autoit3.exe"

$width = 600
$height = 300

$path = ""
$minutes = 120
$wait = $minutes * 60000

GUICreate("Log", $width, $height, -1, -1,$WS_MAXIMIZEBOX+$WS_MINIMIZEBOX+$WS_SIZEBOX)
Global $log = GUICtrlCreateEdit("",0,25,$width-2,$height-50,$ES_MULTILINE+$ES_WANTRETURN+$ES_AUTOVSCROLL+$WS_VSCROLL,0)
GUICtrlSetResizing(-1, $GUI_DOCKBORDERS )
GUICtrlSetFont(-1,10,0,0,"Consolas")
_GUICtrlEdit_SetLimitText($log, 100000000)
$pathbutton = GUICtrlCreateButton(" < Click to select AU3 Script >",0,0,$width-62,25,$BS_LEFT)
GUICtrlSetResizing(-1, $GUI_DOCKLEFT + $GUI_DOCKTOP + $GUI_DOCKHEIGHT)
$waitbutton = GUICtrlCreateButton($minutes,$width-62,0,60,25)
GUICtrlSetResizing(-1, $GUI_DOCKRIGHT + $GUI_DOCKTOP + $GUI_DOCKHEIGHT)
GUISetState(@SW_SHOW)

Global $stream = ""
Global $status = "stopped"
Global $command = "waiting"
Global $timer = TimerInit()

While $command <> "leave"
    $command = "waiting"
    While $command = "waiting"
        $msg = GUIGetMsg()
        If $msg = $GUI_EVENT_CLOSE Then $command = "leave"
        If $msg = $pathbutton And $status = "stopped" Then
            $path = FileOpenDialog("Select an AU3 Script","","AutoIT Scripts (*.au3)",1)
            If $path <> "" Then
                GUICtrlSetData($pathbutton,$path)
                $dir = SplitAt($path,"\",-1,"left",true)
                $exec = '"' & $autoitexepath &'" "' & $path & '"'
                $stream = Run($exec,$dir,@SW_HIDE,$STDOUT_CHILD)
                $status = "running"
            EndIf
        EndIf
        If $msg = $waitbutton Then
            $minutes = InputBox("Schedule","Enter time in minutes",$minutes)
            If @error = 0 Then
                $wait = $minutes * 60000
                GUICtrlSetData($waitbutton,$minutes)
            EndIf
        EndIf
        If $status = "running" Then
            $blob = StdoutRead($stream)
            If @error = 2 Then
                $status = "stopped"
                $timer = TimerInit()
                $stamp = _Now()
                Append($stamp)
            EndIf
            If $blob <> "" Then Append($blob)
        Else
            If TimerDiff($timer) > $wait Then
                $stream = Run($exec,$dir,@SW_HIDE,$STDOUT_CHILD)
                $status = "running"
                GUICtrlSetData($log,"")
            EndIf
        EndIf
    WEnd
WEnd

Func SplitAt($string, $delim, $count = 1, $direction = "right", $include = False)
    $split = StringInStr($string, $delim, 0, $count)
    If $direction = "right" Then
        If $include Then
            Return StringMid($string, $split)
        Else
            Return StringMid($string, $split + 1)
        EndIf
    Else
        If $include Then
            Return StringLeft($string, $split)
        Else
            Return StringLeft($string, $split - 1)
        EndIf
    EndIf
EndFunc

Func Append($string)
    $end = StringLen(GUICtrlRead($log))
    _GUICtrlEdit_SetSel($log, $end, $end)
    _GUICtrlEdit_Scroll($log, $SB_SCROLLCARET)
    GUICtrlSetData($log, $string, 1)
EndFunc

The GUI has two buttons, one to select the path to the script you want to run and the other to specify how long it will wait before running the script again.

Share this post


Link to post
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
Sign in to follow this  

×