Ive not used ahk for writing a script before and im trying to write one for the menu button to always execute a right click because in certain apps Press J to jump to the feed. Press question mark to learn the rest of the keyboard shortcuts. 'Compile Script' will bundle it with an AHK executable to make an EXE file you can run. 'Edit Script' will open your script in your default text editor. You can use Notepad to write AHK scripts, but we recommend using SciTE4AutoHotkey, an editor for AHK which supports syntax highlighting and debugging. SciTE4AutoHotkey is a lightweight and easy to use SciTE-based AutoHotkey script editor.
Let's discuss the loop and how to use it in AHK
In this post I try to present looping,
one of AutoHotkey's basic concepts in a non-programmers way
So I'll discuss two of the loops available in AutoHotkey.
Once you are clear about these two loop commands,
you can find more in the AutoHotkey documentation /other resources
So remember to check out other loops like:
for-loop and file-reading loop that are also available in AHK to learn how they work.
But now let's consider a situation where you want to show Hello, World! in a dialog on screen.
More than once, let's just say five times.
Here is a simple AHK script to do that −
When the above program is executed, it produces a msgbox 5 times with this content −
Hello, World!
Hello, World!
Hello, World!
Hello, World!
Hello, World!
That was simple but:
Let's considerer another situation
Now you want to write Hello, World! one thousand times.
We can certainly write MsgBox commands a thousand times
Ahk Script Writer Download
However there are many reasons why this would not be optimal.
All programming languages provide a concept called loop,
which helps in executing one or more commands a desired number of times.
All high-level programming languages even provide various forms of loops,
which can be used to execute one or more statements repeatedly.
Let's write the above AHK script with the help of a loop and later, we will discuss how other loops work
When the above program is executed, it produces the same result as seen before −
Hello, World!
Hello, World!
Hello, World!
Hello, World!
Hello, World!
The above script makes use of a simple loop, which is being used to execute a set of programming statements enclosed within {…}.
A loop statement allows us to execute a command or group of statements multiple times.
All loops in AutoHotkey also set the value of the build in variable 'A_index' with the current loop number i.e. if a loop is currently running for the 3th time then A_index will hold the number 3 and on the fourth loop it will hold 4 etc…
The while Loop
The while loop available in AutoHotkey has the following syntax −
The above code can be represented in the form of a flow diagram as shown below −
The following are important points to be noted about a while loop −
- A while loop starts with a keyword while followed by a condition enclosed in ( ).
- Following the while command and condition, you will have the body of the loop enclosed in curly braces {…}.
- A while loop body can have one or more lines of code to be executed repeatedly.
- If the body of a while loop has just one line, then its optional to use curly braces {…}.
- A while loop keeps executing its body until a given condition holds true. As soon as the condition becomes false, the while loop breaks and continues executing from the immediate next statement after the while loop body.
- Let's write the above AHK script with the help of a while loop and then we will discuss how this loop works
- A condition is usually a relational statement, which is evaluated to either true or false. A value equal to zero is treated as false and any non-zero value works like true.
When the above program is executed, it produces the same result as seen before −
Hello, World!
Hello, World!
Hello, World!
Hello, World!
Hello, World!
Here, the computer first checks whether the given condition is true, i.e., if variable 'A_index' is less than or equal to 5 or not, if the condition is true, the loop body is entered to execute the given statements.
In our current example we only have a single command in the loop body −
- The Msgbox command, which shows the string Hello World!
After executing all the lines/commands given in the loop body, the computer goes back to while ( A_index = 5) and the given condition, (A_index = 5), is checked again, and the loop is executed again if the condition holds true.
This process repeats as long as the given condition remains true which means variable 'A_index' has a value less than 6.
The Loop…Until
A while loop checks a given condition before it executes any statements given in the body part.
AutoHotkey provides other ways to use loop, like Loop…Until that allows executing a loop body first, then checking a given condition. It has the following syntax −
This process repeats as long as the given condition remains true which means variable 'A_index' has a value less than 6.
The above code can be represented in the form of a flow diagram as shown below −
If you write the above example using Loop…Until, then it looks like this −
When the above script is executed, it produces the same 5 msgbox's −
Hello, World!
Hello, World!
Hello, World!
Hello, World!
Hello, World!
The break statement
When the break statement is encountered inside a loop, the loop is immediately terminated and the program control resumes at the next statement following the loop.
The syntax for a break statement in AHK is −
A break statement can be represented in the form of a flow diagram as shown below −
Here is a variant of the above script, but it will break out of the loop after showing Hello World!
Three times −
When the above script is executed, it produces the following MsgBox's −
Hello, World!
Hello, World!
Hello, World!
The continue statement
The continue statement in AutoHotkey works somewhat like the break statement.
Instead of forcing termination, continue forces the next iteration of the loop to take place, skipping any code in between the continue and the closing brace without going through the loop. (this can be thought of as 'restarting' the loop and skipping anything that was after the continues location in the body)
The syntax for a continue statement in AHK is as follows −
A continue statement can be represented in the form of a flow diagram as shown below −
A variant of the script above with continue looks like:
This will skip showing a MsgBox when A_index has a value equal to 3 −
If you execute the above AutoHotkey script, it shows the following result −
Hello, World! Number: 1
Hello, World! Number: 2
Hello, World! Number: 4
Hello, World! Number: 5
If you'd like to know more about the loop online, I'm happy to help
Let me know what things you've used the loop command for…
AutoHotkey doesn't do anything on its own; it needs a script to tell it what to do. A script is simply a plain text file with the .ahk
filename extension containing instructions for the program, like a configuration file, but much more powerful. A script can do as little as performing a single action and then exiting, but most scripts define a number of hotkeys, with each hotkey followed by one or more actions to take when the hotkey is pressed.
Tip: If your browser supports it, you can download any code block (such as the one above) as a script file by clicking the button which appears in the top-right of the code block when you hover your mouse over it.
Table of Contents
- Installer Options
Create a Script
There are a couple of common ways to create a script file:
- In Notepad (or a text editor of your choice), save a file with the
.ahk
filename extension. On some systems you may need to enclose the name in quotes to ensure the editor does not add another extension (such as .txt).Be sure to save the file as UTF-8 with BOM if it will contain non-ASCII characters. For details, see the FAQ.
- In Explorer, right-click in empty space in the folder where you want to save the script, then select New and AutoHotkey Script. You can then type a name for the script (taking care not to erase the
.ahk
extension if it is visible).
See Scripting Language for details about how to write a script.
Edit a Script
To open a script for editing, right-click on the script file and select Edit Script. If the script is already running, you can use the Edit function or right-click the script's tray icon and select Edit This Script. By default this will open Notepad, but that can be changed by writing to the registry as shown here. Of course, you can always open your text editor first and then open the script as you would any other text file.
After editing a script, you must run or reload the script for the changes to take effect. A running script can usually be reloaded via its tray menu.
Run a Script
With AutoHotkey installed, there are several ways to run a script:
- Double-click a script file (or shortcut to a script file) in Explorer.
- Call AutoHotkey.exe on the command line and pass the script's filename as a command-line parameter.
- After creating the default script, launch AutoHotkey via the shortcut in the Start menu to run it.
- If AutoHotkey is pinned to the taskbar or Start menu on Windows 7 or later, recent or pinned scripts can be launched via the program's Jump List.
Most scripts have an effect only while they are running. Use the tray menu or the ExitApp function to exit a script. Scripts are also forced to exit when Windows shuts down. To configure a script to start automatically after the user logs in, the easiest way is to place a shortcut to the script file in the Startup folder.
Scripts can also be compiled; that is, combined together with an AutoHotkey binary file to form a self-contained executable (.exe) file.
Tray Icon
By default, each script adds its own icon to the taskbar notification area (commonly known as the tray).
The tray icon usually looks like this (but the color or letter changes when the script is paused or suspended):
Right-click the tray icon to show the tray menu, which has the following options by default:
- Open - Open the script's main window.
- Help - Open the AutoHotkey offline help file.
- Window Spy - Displays various information about a window.
- Reload This Script - See Reload.
- Edit This Script - See Edit.
- Suspend Hotkeys - Suspend or unsuspend hotkeys.
- Pause Script - Pause or unpause the script.
- Exit - Exit the script.
Autohotkey Script Writer Download
By default, double-clicking the tray icon shows the script's main window.
The behavior and appearance of the tray icon and menu can be customized:
- A_TrayMenu returns a Menu object which can be used to customise the tray menu.
- A_IconHidden or the #NoTrayIcon directive can be used to hide (or show) the tray icon.
- A_IconTip can be assigned new tooltip text for the tray icon.
- TraySetIcon can be used to change the icon.
Main Window
The script's main window is usually hidden, but can be shown via the tray icon or one of the functions listed below to gain access to information useful for debugging the script. Items under the View menu control what the main window displays:
- Lines most recently executed - See ListLines.
- Variables and their contents - See ListVars.
- Hotkeys and their methods - See ListHotkeys.
- Key history and script info - See KeyHistory.
Known issue: Keyboard shortcuts for menu items do not work while the script is displaying a MsgBox or other dialog.
The built-in variable A_ScriptHwnd contains the unique ID (HWND) of the script's main window.
The title of this window is used by the #SingleInstance and Reload mechanisms to identify other instances of the same script. Changing the title prevents the script from being identified as such. The default title is equivalent to the expression A_ScriptFullPath (A_IsCompiled ? ' : ' - AutoHotkey v' A_AhkVersion)
.
Closing this window with WinClose (even from another script) causes the script to exit, but most other methods just hide the window and leave the script running.
Command Line Usage
See Passing Command Line Parameters to a Script for command line usage, including a list of command line switches which affect the program's behavior.
Portability of AutoHotkey.exe
The file AutoHotkey.exe is all that is needed to launch any .ahk script.
Renaming AutoHotkey.exe also changes which script it runs by default, which can be an alternative to compiling a script for use on a computer without AutoHotkey installed. For instance, MyScript.exe automatically runs MyScript.ahk if a filename is not supplied, but is also capable of running other scripts.
Installer Options
Note: There is currently no standard installer for v2.0-alpha.
To silently install AutoHotkey into the default directory (which is the same directory displayed by non-silent mode), pass the parameter /S to the installer. For example:
A directory other than the default may be specified via the /D parameter (in the absence of /S, this changes the default directory displayed by the installer). For example:
you can find more in the AutoHotkey documentation /other resources
So remember to check out other loops like:
for-loop and file-reading loop that are also available in AHK to learn how they work.
But now let's consider a situation where you want to show Hello, World! in a dialog on screen.
More than once, let's just say five times.
Here is a simple AHK script to do that −
When the above program is executed, it produces a msgbox 5 times with this content −
Hello, World!
Hello, World!
Hello, World!
Hello, World!
Hello, World!
That was simple but:
Let's considerer another situation
Now you want to write Hello, World! one thousand times.
We can certainly write MsgBox commands a thousand times
Ahk Script Writer Download
However there are many reasons why this would not be optimal.
All programming languages provide a concept called loop,
which helps in executing one or more commands a desired number of times.
All high-level programming languages even provide various forms of loops,
which can be used to execute one or more statements repeatedly.
Let's write the above AHK script with the help of a loop and later, we will discuss how other loops work
When the above program is executed, it produces the same result as seen before −
Hello, World!
Hello, World!
Hello, World!
Hello, World!
Hello, World!
The above script makes use of a simple loop, which is being used to execute a set of programming statements enclosed within {…}.
A loop statement allows us to execute a command or group of statements multiple times.
All loops in AutoHotkey also set the value of the build in variable 'A_index' with the current loop number i.e. if a loop is currently running for the 3th time then A_index will hold the number 3 and on the fourth loop it will hold 4 etc…
The while Loop
The while loop available in AutoHotkey has the following syntax −
The above code can be represented in the form of a flow diagram as shown below −
The following are important points to be noted about a while loop −
- A while loop starts with a keyword while followed by a condition enclosed in ( ).
- Following the while command and condition, you will have the body of the loop enclosed in curly braces {…}.
- A while loop body can have one or more lines of code to be executed repeatedly.
- If the body of a while loop has just one line, then its optional to use curly braces {…}.
- A while loop keeps executing its body until a given condition holds true. As soon as the condition becomes false, the while loop breaks and continues executing from the immediate next statement after the while loop body.
- Let's write the above AHK script with the help of a while loop and then we will discuss how this loop works
- A condition is usually a relational statement, which is evaluated to either true or false. A value equal to zero is treated as false and any non-zero value works like true.
When the above program is executed, it produces the same result as seen before −
Hello, World!
Hello, World!
Hello, World!
Hello, World!
Hello, World!
Here, the computer first checks whether the given condition is true, i.e., if variable 'A_index' is less than or equal to 5 or not, if the condition is true, the loop body is entered to execute the given statements.
In our current example we only have a single command in the loop body −
- The Msgbox command, which shows the string Hello World!
After executing all the lines/commands given in the loop body, the computer goes back to while ( A_index = 5) and the given condition, (A_index = 5), is checked again, and the loop is executed again if the condition holds true.
This process repeats as long as the given condition remains true which means variable 'A_index' has a value less than 6.
The Loop…Until
A while loop checks a given condition before it executes any statements given in the body part.
AutoHotkey provides other ways to use loop, like Loop…Until that allows executing a loop body first, then checking a given condition. It has the following syntax −
This process repeats as long as the given condition remains true which means variable 'A_index' has a value less than 6.
The above code can be represented in the form of a flow diagram as shown below −
If you write the above example using Loop…Until, then it looks like this −
When the above script is executed, it produces the same 5 msgbox's −
Hello, World!
Hello, World!
Hello, World!
Hello, World!
Hello, World!
The break statement
When the break statement is encountered inside a loop, the loop is immediately terminated and the program control resumes at the next statement following the loop.
The syntax for a break statement in AHK is −
A break statement can be represented in the form of a flow diagram as shown below −
Here is a variant of the above script, but it will break out of the loop after showing Hello World!
Three times −
When the above script is executed, it produces the following MsgBox's −
Hello, World!
Hello, World!
Hello, World!
The continue statement
The continue statement in AutoHotkey works somewhat like the break statement.
Instead of forcing termination, continue forces the next iteration of the loop to take place, skipping any code in between the continue and the closing brace without going through the loop. (this can be thought of as 'restarting' the loop and skipping anything that was after the continues location in the body)
The syntax for a continue statement in AHK is as follows −
A continue statement can be represented in the form of a flow diagram as shown below −
A variant of the script above with continue looks like:
This will skip showing a MsgBox when A_index has a value equal to 3 −
If you execute the above AutoHotkey script, it shows the following result −
Hello, World! Number: 1
Hello, World! Number: 2
Hello, World! Number: 4
Hello, World! Number: 5
If you'd like to know more about the loop online, I'm happy to help
Let me know what things you've used the loop command for…
AutoHotkey doesn't do anything on its own; it needs a script to tell it what to do. A script is simply a plain text file with the .ahk
filename extension containing instructions for the program, like a configuration file, but much more powerful. A script can do as little as performing a single action and then exiting, but most scripts define a number of hotkeys, with each hotkey followed by one or more actions to take when the hotkey is pressed.
Tip: If your browser supports it, you can download any code block (such as the one above) as a script file by clicking the button which appears in the top-right of the code block when you hover your mouse over it.
Table of Contents
- Installer Options
Create a Script
There are a couple of common ways to create a script file:
- In Notepad (or a text editor of your choice), save a file with the
.ahk
filename extension. On some systems you may need to enclose the name in quotes to ensure the editor does not add another extension (such as .txt).Be sure to save the file as UTF-8 with BOM if it will contain non-ASCII characters. For details, see the FAQ.
- In Explorer, right-click in empty space in the folder where you want to save the script, then select New and AutoHotkey Script. You can then type a name for the script (taking care not to erase the
.ahk
extension if it is visible).
See Scripting Language for details about how to write a script.
Edit a Script
To open a script for editing, right-click on the script file and select Edit Script. If the script is already running, you can use the Edit function or right-click the script's tray icon and select Edit This Script. By default this will open Notepad, but that can be changed by writing to the registry as shown here. Of course, you can always open your text editor first and then open the script as you would any other text file.
After editing a script, you must run or reload the script for the changes to take effect. A running script can usually be reloaded via its tray menu.
Run a Script
With AutoHotkey installed, there are several ways to run a script:
- Double-click a script file (or shortcut to a script file) in Explorer.
- Call AutoHotkey.exe on the command line and pass the script's filename as a command-line parameter.
- After creating the default script, launch AutoHotkey via the shortcut in the Start menu to run it.
- If AutoHotkey is pinned to the taskbar or Start menu on Windows 7 or later, recent or pinned scripts can be launched via the program's Jump List.
Most scripts have an effect only while they are running. Use the tray menu or the ExitApp function to exit a script. Scripts are also forced to exit when Windows shuts down. To configure a script to start automatically after the user logs in, the easiest way is to place a shortcut to the script file in the Startup folder.
Scripts can also be compiled; that is, combined together with an AutoHotkey binary file to form a self-contained executable (.exe) file.
Tray Icon
By default, each script adds its own icon to the taskbar notification area (commonly known as the tray).
The tray icon usually looks like this (but the color or letter changes when the script is paused or suspended):
Right-click the tray icon to show the tray menu, which has the following options by default:
- Open - Open the script's main window.
- Help - Open the AutoHotkey offline help file.
- Window Spy - Displays various information about a window.
- Reload This Script - See Reload.
- Edit This Script - See Edit.
- Suspend Hotkeys - Suspend or unsuspend hotkeys.
- Pause Script - Pause or unpause the script.
- Exit - Exit the script.
Autohotkey Script Writer Download
By default, double-clicking the tray icon shows the script's main window.
The behavior and appearance of the tray icon and menu can be customized:
- A_TrayMenu returns a Menu object which can be used to customise the tray menu.
- A_IconHidden or the #NoTrayIcon directive can be used to hide (or show) the tray icon.
- A_IconTip can be assigned new tooltip text for the tray icon.
- TraySetIcon can be used to change the icon.
Main Window
The script's main window is usually hidden, but can be shown via the tray icon or one of the functions listed below to gain access to information useful for debugging the script. Items under the View menu control what the main window displays:
- Lines most recently executed - See ListLines.
- Variables and their contents - See ListVars.
- Hotkeys and their methods - See ListHotkeys.
- Key history and script info - See KeyHistory.
Known issue: Keyboard shortcuts for menu items do not work while the script is displaying a MsgBox or other dialog.
The built-in variable A_ScriptHwnd contains the unique ID (HWND) of the script's main window.
The title of this window is used by the #SingleInstance and Reload mechanisms to identify other instances of the same script. Changing the title prevents the script from being identified as such. The default title is equivalent to the expression A_ScriptFullPath (A_IsCompiled ? ' : ' - AutoHotkey v' A_AhkVersion)
.
Closing this window with WinClose (even from another script) causes the script to exit, but most other methods just hide the window and leave the script running.
Command Line Usage
See Passing Command Line Parameters to a Script for command line usage, including a list of command line switches which affect the program's behavior.
Portability of AutoHotkey.exe
The file AutoHotkey.exe is all that is needed to launch any .ahk script.
Renaming AutoHotkey.exe also changes which script it runs by default, which can be an alternative to compiling a script for use on a computer without AutoHotkey installed. For instance, MyScript.exe automatically runs MyScript.ahk if a filename is not supplied, but is also capable of running other scripts.
Installer Options
Note: There is currently no standard installer for v2.0-alpha.
To silently install AutoHotkey into the default directory (which is the same directory displayed by non-silent mode), pass the parameter /S to the installer. For example:
A directory other than the default may be specified via the /D parameter (in the absence of /S, this changes the default directory displayed by the installer). For example:
Version: If AutoHotkey was previously installed, the installer automatically detects which version of AutoHotkey.exe to set as the default. Otherwise, the default is Unicode 32-bit or Unicode 64-bit depending on whether the OS is 64-bit. To override which version of AutoHotkey.exe is set as the default, pass one of the following switches:
Ahk Creator
/A32
or/ANSI
: ANSI 32-bit./U64
or/x64
: Unicode 64-bit (only valid on 64-bit systems)./U32
: Unicode 32-bit.
For example, the following installs silently and sets ANSI 32-bit as the default:
Uninstall: To silently uninstall AutoHotkey, pass the /Uninstall
parameter to Installer.ahk. For example:
Note: Installer.ahk must be run as admin to work correctly.
Extract: Later versions of the installer include a link in the bottom-right corner to extract setup files without installing. If this function is present, the /E
switch can be used to invoke it from the command line. For example:
Restart scripts: In silent install/uninstall mode, running scripts are closed automatically, where necessary. Pass the /R
switch to automatically reload these scripts using whichever EXE they were running on, without command line args. Setup will attempt to launch the scripts via Explorer, so they do not run as administrator if UAC is enabled.
Taskbar buttons: On Windows 7 and later, taskbar buttons for multiple scripts are automatically grouped together or combined into one button by default. The Separate taskbar buttons option disables this by registering each AutoHotkey executable as a host app (IsHostApp).
For command-line installations, specify /IsHostApp
or /IsHostApp=1
to enable the option and /IsHostApp=0
to disable it.
Run with UI Access
The installer GUI has an option 'Add 'Run with UI Access' to context menus'. This context menu option provides a workaround for common UAC-related issues by allowing the script to automate administrative programs - without the script running as admin. To achieve this, the installer does the following:
- Copies AutoHotkeyA32.exe, AutoHotkeyU32.exe and (if present) AutoHotkeyU64.exe to AutoHotkey*_UIA.exe.
- Sets the uiAccess attribute in each UIA file's embedded manifest.
- Creates a self-signed digital certificate named 'AutoHotkey' and signs each UIA file.
- Registers the context menu option to run the appropriate exe file.
If any these UIA files are present before installation, the installer will automatically update them even if the UI Access option is not enabled.
For command-line installations, specify /uiAccess
or /uiAccess=1
to enable the option and /uiAccess=0
to disable it. By default, the installer will enable the option if UAC is enabled and the UI Access context menu option was present before installation.
Scripts which need to run other scripts with UI access can simply Run the appropriate UIA.exe file with the normal command line parameters.
Known limitations:
- UIA is only effective if the file is in a trusted location; i.e. a Program Files sub-directory.
- UIA.exe files created on one computer cannot run on other computers without first installing the digital certificate which was used to sign them.
- UIA.exe files cannot be started via CreateProcess due to security restrictions. ShellExecute can be used instead. Run tries both.
- UIA.exe files cannot be modified, as it would invalidate the file's digital signature.
- Because UIA programs run at a different 'integrity level' than other programs, they can only access objects registered by other UIA programs. For example,
ComObjActive('Word.Application')
will fail because Word is not marked for UI Access. - The script's own windows can't be automated by non-UIA programs/scripts for security reasons.
- Running a non-UIA script which uses a mouse hook (even as simple as
#InstallMouseHook
) may prevent all mouse hotkeys from working when the mouse is pointing at a window owned by a UIA script, even hotkeys implemented by the UIA script itself. A workaround is to ensure UIA scripts are loaded last.
For more details, see Enable interaction with administrative programs on the archive forum.