Home | Download | Documentation | Changelog | Support | Forum

Tutorial - Launch a program or document (continued)

To have a program or document start off maximized, minimized, or hidden, consider the following hotkey subroutine, which assigns the Win+Z hotkey to launch two instances of Notepad, the 1st maximized and the 2nd minimized:
#z::
Run, Notepad, , max
Run, Notepad, , min
return

To have a program use a specific folder as its working directory, consider this Win+C hotkey which creates a command prompt window in the specified directory:
#c::Run, %comspec% /k, C:\My Documents

In the above example, the %comspec% environment variable might resolve to C:\Windows\system32\cmd.exe on a typical system.

To pass parameters, add them immediately after the name of the program or document as in these examples:
Run, %comspec% /k dir, C:\My Documents
Run, Notepad.exe "C:\My Documents\Address List.txt"
Run, %ProgramFiles%\AutoHotkey\AutoHotkey.exe "C:\Scripts\Test Script.ahk" param1 "param2 with spaces" param3

In the 2nd and 3rd examples, parameters with spaces in them are enclosed in double quotes, which as a rule is the safest practice. By contrast, the working directory should not be enclosed in double quotes even if it contains spaces, such as in the 1st example above.

Certain special words known as system verbs are also supported. The first example below opens the Explorer's properties dialog for the indicated file. The second example prints the specified document:
Run, properties "C:\Address List.txt"
Run, print "C:\Address List.txt"

RunWait sets the built-in ErrorLevel variable to the exit code of the program it ran. For example, the following will display a non-zero ErrorLevel because the comspec program is indicating that a problem occurred:
RunWait, %comspec% /c dir c:\NonExistent.txt, , hide
MsgBox, %ErrorLevel%

Return to the Tutorial Table of Contents


Home | Download | Documentation | Changelog | Support | Forum