Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'm having trouble understanding how parameters are accessed in AutoHotKey functions.

For example, I set myVar variable with the InputBox, then pass it to a function. How do I evaluate the arg in the TestFunction?

#t::
    inputbox myVar, What is your variable?
    myNewVar := TestFunction(%myVar%)
    MsgBox %myNewVar% 
    return

TestFunction(arg)
{
    MsgBox arg
    msgBox %arg%
    return %arg%
}    

What I'm looking to do is setup a hotkey that will prompt for a keyword for an app, then evaluate what is entered in the function and fire up whatever app corresponds to that keyword.

Thanks!

Chris

share|improve this question
1  
When you call the function, you don't need percent signs around the parameter: myNewVar := TestFunction(myVar) – Bavi_H Feb 8 '11 at 4:56
    
Bavi is right (he should have put his answer in an Answer): parameters called by a function need function("string") if they are strings, and just function(variable) (no percentage signs) if they are variables. It works if you just remove the percentage signs in your third line. I know how incredibly frustrating percentage signs and quotation marks can be in AHK: they kill me too from time to time. – Cerberus Feb 20 '11 at 23:40

I've corrected your script (like Bavi_H suggested) and added an example to launch an application corresponding to a keyword.

#t::
inputbox myVar, What is your variable?
myNewVar := TestFunction(myVar)
MsgBox %myNewVar% 
return

TestFunction(arg)
{
    msgBox %arg%
    if (arg = "calc")
    {
        run, calc.exe
    }
    else if (arg = "word")
    {
        run, winword.exe
    }
    return arg . "bob"
}
share|improve this answer

Basically commands, such as run, %something%, are different from functions, such as myFunction(something). Here is another example based on qwertzguy's answer

#t::
    ; get variable from message box
    inputbox myVar, What is your variable?

    ; myVar DOES NOT have percents when passed to function
    myNewVar := TestFunction(myVar)

    ; myNewVar DOES have percents when passed to command
    MsgBox %myNewVar% 

return


TestFunction(arg)
{
    ; command DOES have percents 
    MsgBox Launching: %arg%

    if (arg = "calc")
    {
        ; commands use traditional variable method
        ; traditional method example: Var = The color is %FoundColor%
        ; variables are evaluated inside quotes

        run, "%A_WinDir%\system32\calc.exe"
    }
    else if (arg = "word")
    {

        ; functions need to use expression version since percents are not evaluated
        ; expression method example: Var := "The color is " . FoundColor
        ; variables are not evaluated inside quotes

        EnvGet, ProgramFilesVar, ProgramFiles(x86)
        OfficeVersionVar := "15"

        RunFunction(ProgramFilesVar . "\Microsoft Office\Office" . OfficeVersionVar . "\WINWORD.EXE")

    }

    return "You typed: " . arg

}



RunFunction(arg)
{
    run, %arg%
}
share|improve this answer

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .