8

I'd like to run the AHK script with arguments, like:

AutoHotkeyU64 file.ahk arg1 arg2

Then access these variables as part of the script, e.g.

MsgBox, %arg1%, %arg2%

How this can be achieved?

kenorb
  • 25,417

2 Answers2

10

The command line parameters are stored in the variables %1%, %2%, and so on. So for example:

MsgBox, %1%, %2%

Since version v1.1.27+, input parameters are stored as an array in the built-in variable A_Args as an array.

Here is the example:

for n, param in A_Args  ; For each parameter:
{
    MsgBox Parameter number %n% is %param%.
}

Legacy: The command line parameters are also stored in the variables %1%, %2%, and so on, as in versions prior to [v1.1.27]. In addition, %0% contains the number of parameters passed (0 if none). However, these variables cannot be referenced directly in an expression because they would be seen as numbers rather than variables.

Source: Passing Command Line Parameters to a Script.

kenorb
  • 25,417
1

This works for me:

In AutoHotkey, %1%, %2% and so on are the command line arguements that have been given. Lets say we have a script called script.ahk, and inside of that script we have MsgBox, %1%

if I were to type script.ahk hey, we would get a messagebox with hey in it. Demo Screenshot

Hopefully that helps :-)

  • This is not precisely true. It seems it works if you run a compiled version of the script (.exe) but not the .ahk source directly. – Thierry Dalon Mar 07 '22 at 15:53