Search code examples
functionparameterscommandautohotkey

With autoHotKey code, how can I write a function that receives the autohotkey command text as a parameter and executes it?


for example I want to pass ANY ahk command text like "Click, 50, 50" or "MsgBox test",,, into a function and execute that command. Is it possible?


Solution

  • This may help you.

    #SingleInstance, Force
    
        var := "example"
        new_instance(   "msgbox " var " 1`nexitapp" )
        new_instance(   "msgbox % ""another " var " 2"""
                    .   "`nexitapp" )
        new_instance(   "msgbox another example 3`nexitapp" )
        new_instance(   "msgbox passing var`n"
                    .   "var := """ var " 4""`n"
                    .   "Msgbox % var `n"
                    .   "exitapp" )
    
    ExitApp, 0
    
    new_instance( Script )  {
    
        OutputDebug, % script   ;   for debug purpose
        shell   := ComObjCreate( "WScript.Shell" )
        exec    := shell.Exec( A_AhkPath " /ErrorStdOut *")
        exec.StdIn.Write( script )
        exec.StdIn.Close()
    
    }