The Wayback Machine - http://web.archive.org/web/20080215041003/http://www.linux.com:80/articles/50847

Linux.com

Feature

Automating Windows with Autohotkey

By Dmitri Popov on January 16, 2006 (8:00:00 AM)


Printer friendly page Print
Comment on this article Comments

If you want to automate some repetitive tasks or write a simple utility that makes your daily computing easier, it's not very practical to learn a full-blown programming language. Instead, you'd be better off using a scripting tool like Autohotkey that allows you to automate virtually any task on Windows.

Autohotkey features an easy-to-learn scripting language that allows you to create useful scripts in a matter of minutes. The scripting language contains functions that allow you to create graphical interfaces, effectively turning your scripts into mini applications. Better yet, with the Smart GUI tool, you can build graphical interfaces without writing any code at all.

To write Autohotkey scripts, you don't need expensive and sophisticated development tools or environments; a simple text editor will do just fine. And any script can be compiled into an executable file that can run on any Windows machine. (Autohotkey supports Windows 95 and upwards.)

Although you can use any text editor to write scripts, you might want to choose one that can work with Autohotkey's syntax, such as UltraEdit, Textpad, Emacs, or Vim. Autohotkey's Extras folder contains scripts and plugins for many popular text editors, including the excellent Notepad++.

The best way to learn Autohotkey is to create a simple script that does something useful. For instance, WordNet is an excellent online language reference tool. However, to look up a word in WordNet, you must select the word in your currently opened document, launch WordNet, or switch to it if it's already running, paste the word, and press Enter. This is not a big deal if you do it every now and then, but if you use WordNet often, you might want to reduce all the clicking and pasting to a single keyboard shortcut. Using Autohotkey, you can easily create a script that will do just that. Here's how.

Start by downloading and installing the latest version of Autohotkey. Create a new script by right-clicking on the desktop and choosing New -> Autohotkey Script from the context menu, then open the script in your text editor.

The first thing we want the script to do is copy the currently selected word into the Clipboard when the user presses the Ctrl-Alt-W keys. This can be done using the following command:

^!w::
Send, ^c

As you might have guessed, the Send command sends the Ctrl-C (copy) key combination. Note that the command itself is placed on a new line; this tells Autohotkey that the defined shortcut starts not a single command, but a whole sequence of commands.

Next, the script must launch WordNet using the Run command:

Run, wnb.exe

There is, however, a slight problem with this command: wnb.exe can only be executed in its own directory. Therefore, you have to define it first as a working directory:

SetWorkingDir, C:\Programmer\WordNet\2.1\bin\

Since Autohotkey supports environmental variables, you can make this command more flexible, so the script can run on machines where the Program Files directory is called something else (for example, it's called Programmer in the Danish version of Windows):

SetWorkingDir, %ProgramFiles%\WordNet\2.1\bin\
Run, wnb.exe

Now, the script must wait for the WordNet window to be opened:

WinWait, WordNet

and then make it active:

WinActivate

Finally, the script must paste the word from the Clipboard into WordNet and send the Enter command:

Send, ^v{Enter}

The sequence ends with the return command, which tells Autohotkey that there are no more commands left. The final result looks like this:

^!w::
Send, ^c
SetWorkingDir, %ProgramFiles%\WordNet\2.1\bin\
Run, wnb.exe
WinWait, WordNet
WinActivate
Send, ^v{Enter}
return

Your first script is ready to go, and you can launch it by double-clicking on it. When you see the Autohotkey icon in the System Tray, you can test your script by selecting a word and pressing Ctrl-Alt-W.

More fun with scripts

Autohotkey contains many other commands and functions that you can use to improve the script. Currently, the biggest problem with the script is that it opens a new WordNet each time you run it, so you can end up with a myriad of WordNet windows on the screen. One way to solve this problem is to instruct Autohotkey to close any WordNet window before opening a new one:

IfWinExist, WordNet
{
WinClose
}

You can also add some interactivity to your script, so that it asks you whether you want to look up the word in WordNet or Wikipedia:

^!w::
Send, ^c
MsgBox, 4,, Look up the word in Wikipedia?
IfMsgBox, Yes
{
Run, http://en.wikipedia.org/w/wiki.phtml?search=%clipboard%
Exit
}
IfWinExist, WordNet
{
WinClose
}
{
SetWorkingDir, %ProgramFiles%\WordNet\2.1\bin\
Run, wnb.exe
WinWait, WordNet
WinActivate
Send, ^v{Enter}
}
return

The fun, of course, doesn't have to stop here. If you're learning a new language and have a list of words to memorize, you can turn WordNet into a "Word of the Day" tool by using the following Autohotkey script:

^!w::
Loop, Read, C:\MyWords.txt
WCount := A_Index
Random, RLine, 1, %WCount%
FileReadLine, WordOfTheDay, C:\MyWords.txt, %RLine%
Clipboard = %WordOfTheDay%
SetWorkingDir, %ProgramFiles%\WordNet\2.1\bin\
Run, wnb.exe
{
WinWait, WordNet
WinActivate
}
Send, ^v{Enter}
return

The script reads the MyWords.txt file containing a list of words, picks a random word, and sends it to WordNet.

If you want to share your script with users who don't have Autohotkey installed on their machines, you can convert the script into an executable file by right-clicking on it and choosing Compile Script from the context menu.

Conclusion

Of course, Autohotkey's capabilities reach far beyond these simple examples. More importantly, Autohotkey features a combination that makes it a tool of choice for automating Windows: It's both powerful and easy to use. Whether you're a seasoned Windows administrator or an ordinary user, Autohotkey can make your computing life easier.

Dmitri Popov is a freelance writer whose articles have appeared in Russian, British, and Danish computer magazines.

Dmitri Popov is a freelance writer whose articles have appeared in Russian, British, US, German, and Danish computer magazines.

Slashdot Slashdot it!   -   del.icio.us del.icio.us   -   Digg This!

Comments

on Automating Windows with Autohotkey

Note: Comments are owned by the poster. We are not responsible for their content.

Autoit is a better solution

Posted by: Anonymous Coward on January 16, 2006 10:20 PM
AutoIt can:<br>Provide a general-purpose scripting language for all Windows versions<br>Simulate keystrokes (supports most keyboard layouts)<br>Simulate mouse movements and clicks<br>Move, resize and manipulate windows<br>Interact directly with "controls" on a window (set/get text from edit controls, check boxes and radio buttons, select items in drop-down lists, etc.)<br>Create complex user interfaces (GUIs)<br>Work with the clipboard to cut/paste text items<br>Provide a scriptable RunAs function for Windows 2000/XP/2003<br><br>see <a href="http://www.autoitscript.com/" title="autoitscript.com">http://www.autoitscript.com/</a autoitscript.com><br><br>It is freeware, but no source code. The next version should add support for TCP

#

Re:Autoit is a better solution

Posted by: Anonymous Coward on January 17, 2006 01:07 AM
Yeah, sure...<br><br>AutoHotkey is better than AutoIt - a lot better!<br>Btw, here's a nice site with lots of free AutoHotkey scripts (called 'coding snacks'): <a href="http://www.donationcoder.com/" title="donationcoder.com">http://www.donationcoder.com/</a donationcoder.com><br><br>

#

Re:Autoit is a better solution

Posted by: redndahead on January 17, 2006 01:24 AM
It is my understanding that the reason that the source code for autoIT is not opensource is because the maker of AutoHotkey would use code from autoIT and then proceed to post on the AutoIT list on how AutoHotkey is so much superior. This was very disrespectful and the original developer decided to make the source code only available to select developers who would contribute back. Much of AutoHotkey's code is taken directly from autoIT when it was open source. If you want to see how similar they are download AutoIT version 2 and look at the syntax. AutoIT v3 is a significant improvement in syntax and functionality. This is a definite scripting language to learn for all windows users. The support you receive from the forum is beyond what you see from many other projects. You can find autoIT here www.autoitscript.com

#

Re:Autoit is a better solution

Posted by: cmallett on January 19, 2006 12:27 AM
redndahead wrote:<br>&gt; Much of AutoHotkey's code is taken directly from autoIT when it was open source.<br><br>On a lines-of-code basis, AutoHotkey.exe is over 95% the original work of its authors. However, it does use AutoIt source code for the commands listed at <a href="http://www.autohotkey.com/forum/viewtopic.php?p=19710#19710" title="autohotkey.com">http://www.autohotkey.com/forum/viewtopic.php?p=1<nobr>9<wbr></nobr> 710#19710</a autohotkey.com> . In addition, the following external tools distributed with AutoHotkey are 99% the work of AutoIt authors Jonathan Bennett and Larry Keys: ahk2exe, AutoScriptWriter, and Window Spy.<br><br>&gt; If you want to see how similar they are download AutoIT version 2 and look at the syntax.<br><br>The core syntax of AutoIt v2 (and by extension, AutoHotkey) was derived directly from Microsoft's ScriptIt, whose syntax is described at <a href="http://www.microsoft.com/technet/archive/winntas/downloads/scriptit.mspx" title="microsoft.com">http://www.microsoft.com/technet/archive/winntas/<nobr>d<wbr></nobr> ownloads/scriptit.mspx</a microsoft.com>

#

Linux equivalents

Posted by: Anonymous Coward on January 17, 2006 04:47 PM
Nice article, thank you. But is there any Linux equivalents.

#

Re:Linux equivalents

Posted by: Anonymous Coward on January 18, 2006 06:51 PM
<p>Check out <a href="http://expect.nist.gov/" title="nist.gov">expect</a nist.gov>. Free and works on both linux and mswindows.</p>

#

Re:Linux equivalents

Posted by: JelleB on January 20, 2006 09:17 PM
And only works on terminal programs? Or at least it did when I last looked at it.

#

Automating Windows with Autohotkey

Posted by: Anonymous on December 31, 2007 10:58 AM
Is there any one can tell me how to write commend to cheek if a button exist in the window or not?

#


Post a new comment

Name : Anonymous


Copy the word from the image on the left into the input box.