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
danharold

Rather than starting a new thread I guess I can just post additional projects to this one.

Here is a Hex/Decimal/Binary conversion dialog I made that updates while you type in any of the fields, made to better familiarize myself with how different values in each system relate to each other and to make quick conversions when necessary.

#include <GUIConstants.au3>
#include <WinAPI.au3>

Global $val = 0

$width = 200
$height = 90

GUICreate("Hex", $width, $height, -1, -1)
Global $bin = GUICtrlCreateInput ( "", 2, 2 , 194 , 20 )
Global $hex = GUICtrlCreateInput ( "", 2, 24 , 194 , 20 )
Global $dec = GUICtrlCreateInput ( "", 2, 46 , 194 , 20 )
$minus = GUICtrlCreateButton( "-", 2, 68, 97, 20)
$plus = GUICtrlCreateButton( "+", 100, 68, 97, 20)

GUIRegisterMsg($WM_COMMAND, "WM_COMMAND")
GUISetState(@SW_SHOW)

Global $command = "waiting"

Refresh()

While 1
    $msg = GUIGetMsg()
    Select
        Case $msg = $GUI_EVENT_CLOSE
            Exit
        Case $msg = $minus
            $val += -1
            Refresh()
        Case $msg = $plus
            $val += 1
            Refresh()
    EndSelect
WEnd

Func Refresh($skip = -1)
    If $skip <> $bin Then UpdateBin()
    If $skip <> $hex Then UpdateHex()
    If $skip <> $dec Then UpdateDec()
EndFunc

Func UpdateBin()
    GUICtrlSetData($bin,Encode($val,2))
EndFunc

Func UpdateHex()
    GUICtrlSetData($hex,Encode($val,16))
EndFunc

Func UpdateDec()
    GUICtrlSetData($dec,String($val))
EndFunc

Func WM_COMMAND($hWnd, $iMsg, $wParam, $lParam)
    $iNotification = _WinAPI_HiWord($wParam)
    $iControlID = _WinAPI_LoWord($wParam)
    If $iNotification = $EN_CHANGE Then
        Switch $iControlID
            Case $bin
                $val = Parse(GUICtrlRead($iControlID),2)
                Refresh($iControlID)
            Case $hex
                $val = Parse(GUICtrlRead($iControlID),16)
                Refresh($iControlID)
            Case $dec
                $val = GUICtrlRead($iControlID)
                Refresh($iControlID)
        EndSwitch
    EndIf
EndFunc

Func Parse($string,$base)
    $total = 0
    $len = StringLen($string)
    For $i = 1 to $len
        $char = StringMid($string,$i,1)
        $worth = $base ^ ($len - $i)
        If StringIsDigit($char) Then
            $value = $char
        Else
            $value = Dec($char)
        EndIf
        $total += $value * $worth
    Next
    Return $total
EndFunc

Func Encode($number,$base)
    $i = 0
    Do
        $i += 1
    Until $base ^ $i > $number
    $limit = $i - 1
    $temp = $number
    $out = ""
    For $i = $limit to 0 Step -1
        $div = Floor($temp/($base^$i))
        $temp = $temp - $div * ($base^$i)
        $out = $out & Hex($div,1)
    Next
    Return $out
EndFunc

 

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  

×