Home > front end >  Why `#f::true ? MsgBox true : Run notepad` does nothing?
Why `#f::true ? MsgBox true : Run notepad` does nothing?

Time:10-17

I expect #f::true ? MsgBox true : Run notepad will open a message box with "true" when pressing #f, but it does nothing in practice, can anyone explain the mechanism behind this behavior?

CodePudding user response:

Commands only work at the start of a line, you can't use them inline.
What you're doing, is concatenating two variables together. A variable named MsgBox and a variable named true (or Run and notepad).

You can only use expressions inline.
In AHK v1, you could, of course wrap the legacy commands in a function and then use that function inline:

#f::true ? MsgBoxFunc(true) : RunFunc("notepad")

MsgBoxFunc(Options := "", Title := "", Text := "", Timeout := "")
{
    if (Options && (!Title && !Text && !Timeout))
        MsgBox, % Options
    else
        MsgBox, % Options, % Title, % Text, % Timeout
}

RunFunc(Target, WorkingDir := "", Options := "", ByRef OutputVarPID := "")
{
    Run, % Target, % WorkingDir, % Options, pid
    OutputVarPID := pid
}

Or in AHK v2, this problem of course doesn't exist:

#f::true ? MsgBox(true) : Run("notepad")
  • Related