Search code examples
functioncallautoit

How to run an AutoIt script and call a function from it?


CONVERTER.au3 converts webp to png using dwebp:

Convert()

Func Convert()
    $hSearch = FileFindFirstFile(@ScriptDir & "\*.webp")
    $sFileName = FileFindNextFile($hSearch)
    $Split = StringSplit($sFileName, ".")

    ;~ MsgBox(0,'',$Split[1])

    Run("dwebp.exe " & $sFilename & " -o " & $Split[1] & ".png")
EndFunc

Func Troubleshoot()
    Convert()
    Local $hSearch
    If $hSearch = -1 Then
        $desktopCON = "supported"
    Else
        $desktopCON = "unsupported"
    EndIf
    FileClose($hSearch)
    Exit
EndFunc

I need to run CONVERTER.au3 and call a specific function from it. I tried this but doesn't seem to work:

Run("D:\SCRIPT\NEW\CONVERTER.au3 Call(Convert)")

Solution

  • Parameters are referenced with the $CmdLine array. The first element ($CmdLine[0] is the number of parameters, $CmdLine[1] is the first parameter, etc.).

    If $CmdLine[0] = 0 Then  ; standard behavour for no params
        Convert()
        Exit
    EndIf
    
    If $CmdLine[1] = "convert" Then ; param is "convert"
        Convert()
        Exit
    EndIf
    
    If $CmdLine[1] = "troubleshoot" Then ; param is "troubleshoot"
        Troubleshoot()
        Exit
    EndIf
    
    MsgBox(0, "ERRROR", "undefined function: " & $CmdLine[1])
    
    Func Convert()
        MsgBox(0, "", "your converting code here")
    EndFunc   ;==>Convert
    
    Func Troubleshoot()
        MsgBox(0, "", "your troubleshooting code here")
    EndFunc   ;==>Troubleshoot
    

    Execute it from AutoIt with Run('"Converter.au3" foobar')
    or from the Windows Command line with just converter.au3 troubleshoot

    If no parameter is given, it executes a default function (or whatever you want it to do).