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

link|improve this question

43% accept rate
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
feedback

1 Answer

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"
}
link|improve this answer
feedback

Your Answer

 
or
required, but never shown

Not the answer you're looking for? Browse other questions tagged or ask your own question.