Automate Windows Tasks with Autohotkey (Series)

Automate Windows Tasks with Autohotkey (Series)
Page content

Presenting Autohotkey

Autohotkey is a free and open source product. It is a macro programming language for Windows operating systems and works on Windows XP, Windows Vista and Windows 7. Just because it’s free does not mean that it is not useful. Autohotkey provides a powerful set of language constructs and capabilities to interact with Windows operating systems. In fact, if you have Wine installed on Linux it also works on there too.

The macro language can be used to develop autohotkey quick scripts for current tasks, or it can be developed into a full blown automation utility and left to run in the background.

In order to use and run autohotkey script, you will need to download the application from Autohotkey.com for your particular platform.

First Simple Autohotkey Script

In this example, our first autohotkey quick script will perform an operation on the clipboard and transform the output with a number of additional fields.

Image Credit: Autohotkey, https://www.autohotkey.com

The script takes a copy of the clipboard, and will output the contents into a message box dialogue and displayed like so:

Clipboard Contents:

Clipboard Time/Date:

All the items between the <> will be replaced by Autohotkey input generated automatically, such as the time and date, and user generated input.

This script will be enhanced to include new features as the series progresses. The objective of this application is to build an application to manage the clipboard in different ways.

Create the AHK Script

On your computer create the AHK script. Call it “clipboard.ahk” or anything you like. The file can be edited in a notepad or any other text editor.

Once the blank ahk file has been created we need to put some commands into it. We need to decide if the script will run immediately as soon as the ahk file is clicked or it will run when a hotkey is pressed to activate the application. The difference is that if a hotkey is used, the script is held in memory and paused until the hotkey is pressed. Once the hotkey is pressed the remainder of the code is executed. When no hotkey is used the code is executed immediately and is not paused.

Once an autohotkey script is activated, it remains in memory and if the hotkey method is used, will repeat its operation. Without a hotkey to activate it the code it will only run the first time it is clicked or reloaded by activating it again.

To activate the script using a hotkey, say of “Windows + v” the first line of the script will contain the hotkey definition as follows:

#v::

The “#” is a special code for the windows key, and this will be followed by the letter “v”. The “::” indicate to autohotkey that a hotkey combination is being used. Now every time “Windows + v” is pressed and the script has been loaded into memory the remainder of the code will be executed.

The first step is to initialize the clipboard so that it contains nothing at the outset. Autohotkey provides a special variable for the clipboard, which can be set to nothing as follows:

clipboard=

The “=” sign without nothing on the right hand side means set the variable to NULL. In this case the clipboard will be blanked out.

Gather Clipboard Data

The first step is to gather clipboard information by highlighting text. In this program we will prompt the user to do this by way of a message box. Once the text is highlighted and copied to the clipboard in the normal way the program will proceed with its remaining instructions.

Therefore prompt the user with the following message box command:

MsgBox, Please select and copy data using the mouse and “control-c” and press “Ok” on this dialogue to continue:

The script now looks like:

-—————————————–

#v::

clipboard=

MsgBox, Please select and copy data using the mouse and “control-c” and press “Ok” on this dialogue to continue:

-——————————————

Autohotkey has a built in method to wait for data being present on the clipboard. This command is called “clipwait.” If you simply use the clipwait command the program will wait forever until data is entered, so if no data becomes present on the clipboard the program will “hang.” In this example we want to avoid that situation so we will provide a timeout to the clipwait command which checks a certain length of time until the clipboard has data on it. We will give it 60 seconds, as this seems to be ample time for the user to highlight and copy text to the clipboard:

ClipWait, 60

Once the user has copied text to the clipboard, the user presses “Ok” in the original prompt and the program will continue.

Format The Clipboard Data

The clipboard data will now be available on the Autohotkey script clipboard so we can commence formatting the output. This is done by using Autohotkey variables. The output can be displayed in a number of ways. In this case a simple “MsgBox” dialogue will be used to output the data. The time and date fields provides a way of accessing the current time, as it holds this in a timestamp variable called “A_NOW” and is in the format “YYYYMMDDHH24MISS.” By selecting a sub-string of this format the current time and date can be output in various ways.

The clipboard information will be stored in a single dynamic variable, called “vClipout.”

Assign the first line of the message “Clipboard Time/Date: .”

This is achieved as follows:

vTimeHour := Substr(A_Now,9,2) ; Start at 9th character, and get the next 2 depicting the hour

vTimeMinutes := Substr(A_Now,11,2) ; Start at 11th character and get the next 2 depicting the minutes

vYearVal := SubStr(A_Now, 1,4) ; Start at first character and get teh next 4 for the year

vMonthVal := SubStr(A_Now,5,2) ; Start at the 5th character and get the next 2 characters for the month

vDayVal := SubStr(A_Now,7,2) ; Start at the 7th character and get the next 2 for the day

vClipout := vMonthVal . “/” . vDayVal . “/” . vYearVal . " " . vTimeHour . “:” . vTimeMinutes . “`n`n============================`n`n” ; Add end newline here.

; The above demonstrates how variables are used in Autohotkey. Dynamic (as opposed to static, explained in later articles) variables are surrounded by “%”. Variables can be concatentaed with themself via the “.=” operator

; Finally, we add the clipboard contents and output via a message box:

vClipout := “Clipboard Contents @ " . vClipout . clipboard

MsgBox, %vClipout%

Return

The Complete First Autohotkey Script

; Script to collect clipboard data and display it with a timestamp.

; The script should be saved with a .ahk suffix in a text file.

#v::

clipboard=

MsgBox, Please select and copy data using the mouse and “control-c” and press “Ok” on this dialogue to continue:

ClipWait, 60

vTimeHour := Substr(A_Now,9,2) ; Start at 9th character, and get the next 2 depicting the hour

vTimeMinutes := Substr(A_Now,11,2) ; Start at 11th character and get the next 2 depicting the minutes

vYearVal := SubStr(A_Now, 1,4) ; Start at first character and get teh next 4 for the year

vMonthVal := SubStr(A_Now,5,2) ; Start at the 5th character and get the next 2 characters for the month

vDayVal := SubStr(A_Now,7,2) ; Start at the 7th character and get the next 2 for the day

vClipout := vMonthVal . “/” . vDayVal . “/” . vYearVal . " " . vTimeHour . “:” . vTimeMinutes . “`n`n============================`n`n” ; Add end newline here.

; The above demonstrates how variables are used in Autohotkey. Dynamic (as opposed to static, explained in later articles) variables are surrounded by “%”. Variables can be concatentaed with themself via the “.=” operator

; Finally, we add the clipboard contents and output via a message box:

vClipout := “Clipboard Contents @ " . vClipout . clipboard

MsgBox, %vClipout%

Return

Autohotkey Clipboard Script Summary

To summarize what we have done here. We have created a first autohotkey script using some popular conventions and built-in variables and functions such as “A_NOW” and “clipboard.” This simple script shows how to capture the contents of the clipboard and format it with a timestamp using a set of variables to store parts of a timestamp. The output was displayed using a standard Autohotkey message box. A variable was used to increment the contents of the clipboard until it was ready for output. In future scripts, this simple script will be enhanced to take input from the keyboard to provide the clipboard contents with a name, and choose an alternative output option.

References

Autohotkey: https://www.autohotkey.com

This post is part of the series: Autohotkey Tutorials

In this series of tutorials, we look at the powerful and time saving utility Autohotkey, a free and open source package available for download. Using the tool it is possible fo non-programmers or people with a basic programming knowledge to quickly and easily automate windows tasks.

  1. Autohotkey Scripts &ldquo;By Example&rdquo;: Tutorials on Automating Windows Tasks
  2. AutohotKey Tutorials Part 2: Clipboard History