command-line-argumentsautoit

Passing Command Line Argument to SwitchCase in AutoIt


I have a script that has a GUI and I have been running with a start button using the below code:

    Case $StartButton

I would also like to try scheduling this using Windows TaskScheduler to run every morning at 8 AM EST. What would be the best way to add a condition to either start with the start button OR when TaskScheduler runs at 8 AM EST (or any specific time)? I am hesitant to just do 8 AM condition as it may increase processing a lot always looking at the time.

Essentially what I am looking to have happen is for my computer to auto-unlock (login?) using task scheduler and run this AutoIt script which has been compiled to exe.

FilePath is: C:\Users\robert\OneDrive\Desktop\TempFile.exe

Block of relevant code is below:

While 1

$nMsg = GUIGetMsg()
Switch $nMsg
    Case $GUI_EVENT_CLOSE
        Exit

    Case $Save
        SaveOptions()

    Case $StartButton
        ;TAB1 of GUI
        
        
If WinExists("[CLASS: QT373947473845]") Then
$oBlah = WinGetHandle("[CLASS: QT373947473845]")
$BSLoc = WinGetPos ("[CLASS: QT373947473845]")

If $BSLoc[0] <> 0 or $BSLoc[1] <> 0 or $BSLoc[2] <> 800 or $BSLoc[3] <> 600 Then
WinSetState ( $oBlah, "", @SW_MINIMIZE )

sleep(500)
WinActivate($oBlah)
WinWaitActive($oBlah)
WinMove($oBlah, "", 0, 0, 800, 600)
sleep(2000)
Else
WinActivate($oBlah)
WinWaitActive($oBlah)
sleep(2000)
EndIf

Endif
EndSwitch
WEnd

I have thousands of lines of other code within the case but I tried to limit it as that portion is irrelevant

The Case $StartButton is the line where I am trying to do an OR if run by TaskManager. I have read you cannot do an OR function within a switch case but if you do 2 cases without a break it is the same thing?

I read some documentation and saw I can add a “/prim1=value” to the end of the command line and it will pass the prim1 argument to $CmdLine[0] but I can’t seem to get it working properly.


Solution

  • Review the documentation. Particularly this part:

    So if you were to use the compiled executable by passing commandline parameters:
    myProg.exe param1 "This is a string parameter" 99

    $CmdLine[0] ; this equals 3 (because there are 3 command line parameters)
    $CmdLine[1] ; This contains "param1".
    $CmdLine[2] ; This contains "This is a string parameter".
    $CmdLine[3] ; This contains 99.

    So then just modify the code so that it behaves differently if $CmdLine[1] = something.

    As for the switch case: that is inside of a message loop for the GUI and the code in the Case $StartButton part of the switch block is run when the start button is pressed and $nMsg is equal to the control ID of the start button ($StartButton).

    If you want to run this code at some other time what I would do it just move all of that code into its own function:

    Func onStartClick()
        ; start button code here
    Endfunc
    

    And then just call onStartClick() in the switch block:

    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $StartButton
            onStartClick()
        Case $someOtherButton
            ; someOtherButton code...etc
    EndSwitch
    

    And then you can also call this function if there is a particular command line param (place this code before the Switch...EndSwitch block, not inside it):

    If $CmdLine[0] >= 1 And $CmdLine[1] = "param1" then
        ; other code to run when started with "program.exe param1"
        onStartClick()
    EndIf
    

    So the entire thing would look like this:

    Func onStartClick()
        ; start button code here
    Endfunc
    
    If $CmdLine[0] >= 1 And $CmdLine[1] = "param1" then
        ; other code to run when started with "param1"
        onStartClick()
    EndIf
    
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $StartButton
            onStartClick()
        Case $someOtherButton
            ; someOtherButton code...etc
    EndSwitch