Run / RunWait

Homepage
Command List

Runs an external program. Unlike Run, RunWait will wait until the program finishes before continuing.

Run, Target [, WorkingDir, Max|Min|Hide|UseErrorLevel, OutputVarPID]

Parameters

Target

A document, URL, executable file (.exe, .com, .bat, etc.), shortcut (.lnk), or system verb to launch (see remarks). If Target is a local file and no path was specified with it, A_WorkingDir will be searched first. If no matching file is found there, the system will search for and launch the file if it is integrated ("known"), e.g. by being contained in one of the PATH folders.

To pass parameters, add them immediately after the program or document name. If a parameter contains spaces, it is safest to enclose it in double quotes (even though it may work without them in some cases).

WorkingDir The working directory for the launched item. Do not enclose the name in double quotes even if it contains spaces. If omitted, the script's own working directory (A_WorkingDir) will be used.
Max|Min|Hide
UseErrorLevel

If omitted, Target will be launched normally. Alternatively, it can contain one or more of these words:

Max: launch maximized

Min: launch minimized

Hide: launch hidden (cannot be used in combination with either of the above)

Note: Some applications (e.g. Calc.exe) do not obey the requested startup state and thus Max/Min/Hide will have no effect.

UseErrorLevel: UseErrorLevel can be specified alone or in addition to one of the above words (by separating it from the other word with a space). If the launch fails, this option skips the warning dialog, sets ErrorLevel to the word ERROR, and allows the current thread to continue. If the launch succeeds, RunWait sets ErrorLevel to the program's exit code, and Run sets it to 0.

When UseErrorLevel is specified, the variable A_LastError is set to the result of the operating system's GetLastError() function. A_LastError is a number between 0 and 4294967295 (always formatted as decimal, not hexadecimal). Zero (0) means success, but any other number means the launch failed. Each number corresponds to a specific error condition (to get a list, search www.microsoft.com for "system error codes"). Like ErrorLevel, A_LastError is a per-thread setting; that is, interruptions by other threads cannot change it. However, A_LastError is also set by DllCall.

OutputVarPID

The name of the variable in which to store the newly launched program's unique Process ID (PID). The variable will be made blank if the PID could not be determined, which usually happens if a system verb, document, or shortcut is launched rather than a direct executable file. RunWait also supports this parameter, though its OuputVarPID must be checked in another thread (otherwise, the PID will be invalid because the process will have terminated by the time the line following RunWait executes).

After the Run command retrieves a PID, any windows to be created by the process might not exist yet. To wait for at least one window to be created, use WinWait ahk_pid %OutputVarPID%

ErrorLevel

Run: Does not set ErrorLevel unless UseErrorLevel (above) is in effect, in which case ErrorLevel is set to the word ERROR upon failure or 0 upon success.

RunWait: Sets ErrorLevel to the program's exit code (a signed 32-bit integer). If UseErrorLevel is in effect and the launch failed, the word ERROR is stored.

Remarks

Unlike Run, RunWait will wait until Target is closed or exits, at which time ErrorLevel will be set to the program's exit code (as a signed 32-bit integer). Some programs will appear to return immediately even though they are still running; these programs spawn another process.

If Target contains any commas, they must be escaped as shown three times in the following example:

Run rundll32.exe shell32.dll`,Control_RunDLL desk.cpl`,`, 3  ; Opens Control Panel > Display Properties > Settings

When running a program via Comspec (cmd.exe) -- perhaps because you need to redirect the program's input or output -- if the path or name of the executable contains spaces, the entire string should be enclosed in an outer pair of quotes. In the following example, the outer quotes are shown in red and all the inner quotes are shown in black:
Run %comspec% /c ""C:\My Utility.exe" "param 1" "second param" >"C:\My File.txt""

If Target cannot be launched, an error window is displayed, after which the script will behave as though it encountered an Exit command. To prevent this, include the string UseErrorLevel in the third parameter.

Performance may be slightly improved if Target is an exact path, e.g. Run, C:\Windows\Notepad.exe "C:\My Documents\Test.txt" rather than Run, C:\My Documents\Test.txt

Special CLSID folders may be opened via Run. For example:

Run ::{20d04fe0-3aea-1069-a2d8-08002b30309d}  ; Opens the "My Computer" folder.
Run ::{645ff040-5081-101b-9f08-00aa002f954e}  ; Opens the Recycle Bin.

System verbs correspond to actions available in a file's right-click menu in the Explorer. If a file is launched without a verb, the default verb (usually "open") for that particular file type will be used. If specified, the verb should be followed by the name of the target file. The following verbs are currently supported:

properties Displays the Explorer's properties window for the indicated file. For example: Run, properties "C:\My File.txt"
Note: The properties window will automatically close when the script terminates. To prevent this, use WinWait to wait for the window to appear, then use WinWaitClose to wait for the user to close it.
find Opens an instance of the Explorer's Search Companion or Find File window at the indicated folder. For example: Run, find D:\
explore Opens an instance of Explorer at the indicated folder. For example: Run, explore %A_ProgramFiles%
edit Opens the indicated file for editing. It might not work if the indicated file's type does not have an "edit" action associated with it. For example: Run, edit "C:\My File.txt"
open Opens the indicated file (normally not needed because it is the default action for most file types). For example: Run, open "My File.txt"
print Prints the indicated file with the associated application, if any. For example: Run, print "My File.txt"

While RunWait is in a waiting state, new threads can be launched via hotkey, custom menu item, or timer.

Related

RunAs, Process, Exit, CLSID List, DllCall

Examples

Run, Notepad.exe, C:\My Documents, max

Run, mailto:someone@domain.com?subject=This is the subject line&body=This is the message body's text.
Run, ReadMe.doc, , Max UseErrorLevel  ; Launch maximized and don't display dialog if it fails.
if ErrorLevel = ERROR
    MsgBox The document could not be launched.

RunWait, %comspec% /c dir c:\ >>c:\DirTest.txt, , min
Run, c:\DirTest.txt
Run, properties c:\DirTest.txt

Run, www.autohotkey.com ; i.e. any URL can be launched.
Run, mailto:someone@somedomain.com  ; This should open the default e-mail application.

Run ::{20d04fe0-3aea-1069-a2d8-08002b30309d}  ; Opens the "My Computer" folder.
Run ::{645ff040-5081-101b-9f08-00aa002f954e}  ; Opens the Recycle Bin.

; To run multiple commands consecutively, use "&&" between each:
Run, %comspec% /c dir /b > C:\list.txt && type C:\list.txt && pause

Homepage  |  Command List