Creating Your First AutoHotkey Script
Working and living as a programmer means I am pretty much on my computer or playing around on it all the time - sometimes maybe too much. I am sure many of you out there can relate. This also means I am always looking for tools to make my life easier. As of late I have found AutoHotkey to be a really cool time saving application. Well today I am going to show you how to create an AutoHotkey script that will help reduce redundant work.
|
Now, I am guessing most of you are asking "What is AutoHotkey?". From my interaction with it the short answer is: a utility to help automate tasks. The long version is that AutoHotkey is an application that allows for scripts to be ran that can define global hotkeys or execute programs or can be used to build applications that utilize AutHotkey's functionality. There are almost endless possibilities of what can be done with the tool. AutoHotkey is limited to Windows only however, sorry mac and linux geeks. | |
Today, however, we are going to start by creating a very simple script. The script we are going to create will do a few things. The first thing we are going to do is create a hotkey that will open up Notepad++, my text editor of choice. We will then create a hotkey that will present us with an input box and then will search this site, Switch on the Code, for whatever was put in the input box. Lastly, we will add a piece of code that will be auto executed when our script starts up - in my case when Windows starts.
To get the ball rolling you first need to download and install the AutoHotkey application. This should go really quick the download is less than 2MB currently and should only take a few seconds to install. Ok now that that is out of the way we can get to coding. Open up your text editor of choice and create a new document. The very first thing we are going to build is a "Hello World" script.
Our hello world script is going to create a message box and print out "Hello World". To accomplish this we will take advantage of the MsgBox command available in AutoHotkey. This function can simply be called with some text attached. The following code demonstrates this. Notice that after the function name there is a comma.
To run this we save the file, I named mine AutoHotkey.ahk (the .ahk is important), and then just double click the script file or right click and select "Run Script". This should popup a window like below. We can click the "OK" button and continue on.
Also in your system try you will see an icon like the one to the right. This shows that the script is running, meaning that any hotkeys that defined in the script file are usable at this time. We can also right click the system try icon to pause, edit, reload, and exit the script.
An AutoHotkey script file is setup in the following manner, all the code you want automatically executed (like our "Hello World" message) goes at the top of the file and doesn't have any identifiers attached. Identifiers are what are used to create hotkeys and more. To declare a hotkey we need to put an identifier for which keys it will be bound to and then what code to run. Now, lets go ahead and clear our script file and enter the following code.
This code creates a hotkey which is tied to the combination of the "Windows key + n" (Windows key is pressed plus the 'n' key is pressed). When this hotkey is pressed the script will Run notepad++ which simply opens notepad++. So the basic hotkey syntax is key(s) to press:: code to run. The "::" separates the two items but can't have a space after the key(s) to press. As mentioned earlier the "#" symbol represents the Windows key. The other basic symbols are as follows:
| Shortcut Symbol | Key Description |
| # | Windows Key |
| ^ | Control Key |
| ! | Alt Key |
| + | Shift Key |
This is only a partial list but includes the most used ones; you can get a complete list from the AutoHotkey Symbols Documentation. So your first hotkey has been created. The next hotkey we are going to develop is multiline so the syntax is slightly different. The input key identifier is setup the same way but we now have multiple lines of code after the identifier. To end the code we add a return call. The code for the new hotkey follows. I will go over the specifics after.
InputBox, SearchTerm, Search SOTC, Please enter search term.
if not ErrorLevel
{
if SearchTerm <> ""
Run http://blog.paranoidferret.com/index.php?s=%SearchTerm%&Submit=Search
}
return
So, that looks a bit more complicated than the first hotkey we built. The code starts off by prompting the user for some input. A input box is created and shown using the InputBox command. The first parameter for this command (again after the first comma) is the output variable - where the string entered into the input box is stored. The second is the window title and third is the prompt text. You can see a image of this prompt below. The next line is an if statement to check if the ErrorLevel has been set by any faults when entering data into the input box. If no errors have occurred we check to make sure the string isn't blank. If SearchTerm is not blank we tell windows to go to the Switch on the Code blog with the GET parameters s set to the search term and Submit equal to "Search". This searches our blog with the entered search info.
The final thing we are going to do today is add an auto executing piece of script to the top of our script. This code will make the windows taskbar semi transparent. To make the taskbar or any window transparent you can use the WinSet command. The command takes a few parameters. The first parameter is the attribute to set on the window in this case we set it to Transparent. The next parameter is the value of transparency which can be any value from 0 (invisible) to 255 (fully opaque). The final parameter for this is the window to change transparency on. For our call we use a special constant, ahk_class Shell_TrayWnd, which refers to the taskbar.
That wraps up this tutorial. The only thing left to show you is the completed script file.
#n:: Run notepad++
#s::
InputBox, SearchTerm, Search SOTC, Please enter search term.
if not ErrorLevel
{
if SearchTerm <> ""
Run http://blog.paranoidferret.com/index.php?s=%SearchTerm%&Submit=Search
}
return
I hope that this tutorial helps you get up and running with AutoHotkey. If you have any questions or concerns please leave a comment. I would also love to hear whether or not people are interested in more AutoHotkey tutorials. Lastly, feel free to download the script we created today from the references list below.
References
Posted in AutoHotkey, All Tutorials by The Fattest |
