# Get rid of repetitive actions with Autohotkey (Windows)

Picture found here.

This morning, I made a terrible mistake and managed to delete my Autohotkey script. I’ve been using it since more than a year now, and nuilt it little by little, everytime I noticed something I was tired to do repeatedly.

But deleting it, and having to rebuild it is a good occasion to dive again in the beauty of Autohotkey, and I’ll share with you the process.

What’s Autohotkey ?

Autohotkey is a quick, light and open-source automation program, that can do a LOT of things. Things I’ll teach you here will include :

• Text completion : press a short combination of letter, and get any long sentence you repeat often (e.g. : tfr > “Thanks for your response.”)
• Easy access to special characters, like Ç, É or œ. More for French people I guess.
• Music player shortcuts : make any key being the “next”, “prev” of your music player.
• And lot more : put anything fullscreen, superfast Google request, and more…

Autohotkey works with two things : the software itself, which use rarely more than 2mb of CPU, and a special script, a *.ahk file. To get the installer, you can click here or go on Autohotkey website. Install it, and let the program open a blank script for you.
You can get started !

Launching program

To launch a program, you can either use the menu bar, go on your desktop each time or click the start button… And search again. I’ll start to teach you the Autohotkey syntax with one of the simple use. Let say you want to start Firefox using the keys Alt+F. You’ll put this lign in your script :

;When I press alt+f, I want to open Firefox
!f::Run Firefox

This explained : ; starts a comment, as you should put indications in your script for latter reading. !f means “trigger this action when I press Alt (!) and f” ; you can find all the list of the code for each key (alt, ctrl, maj) here. Finally, Run Firefox will simply launch Firefox on your home page. Easy, isn’t it?

With this first technique, you now launch everything directly from the keyboard, special web pages ( !w::Run http://www.miiy.fr) or any document (!t::Run C:\Users\user\to do.txt). A little advice : try to use first the keys you use very rarely : the ², or insert, this very annoying key (actually I haven’t found a way to use it yet so I just “killed” it : Insert::  return works).

Now you made your first command, save your script and just right-click on the little “H” in the system tray, then “Reload this script”. You can run programs from your keys !

Text completion and special characters

Being slightly different, I’ll precise here how to use text completion, e.g. if you want to get a quick email ending

;When I type rgd, I want "Regards (double backspace) Loïs Boullu"
::rgd:: Regards {enter} {enter} Loïs Boullu

Really quickly, :: to say you’ll be typing something, and {enter} for the backspace. Important: using this script, Autohotkey will wait for you to press the space bar or enter to actually replace your text. For myself, I’m using ::çç::Ç, ::éé::É or the more “math specific” ::beq::\begin{{}equation{}}{enter}{enter}\end{{}equation{}}, saving a lot of time when using LaTeX.

Special tweaks

The two first categories are the basic level, easily reproducible commands. But it can go way further : with the big community behind Autohotkey (more than 80.000 topics on the support forum), there is a big chance that any of your ideas has already been implemented by one member.

Easy music control (more here):

; When I press F12, I want to play the next song
F12::
Send {Media_Next}
return

;previous song
F10::
Send {Media_Prev}
return


Basic mouse based music control :

~LButton & RButton:: ; When I hold left button and click right button, I play the next song
{
Sleep, 100
Send {Media_Next}
Return
}

~RButton & LButton:: ; When I hold left button and click right button, I play the previous
{
Sleep, 100
Send {Media_Prev}
Return
}

~Alt & WheelUp::  ; Scroll left.
Send {Volume_Up}
return

~Alt & WheelDown::  ; Scroll left.
Loop 4
Send {Volume_Down}
return

:

;When I press alt+g, I want to search Google for the highlighted text
!g::
;Copy Clipboard to prevClipboard variable, clear Clipboard.
prevClipboard := ClipboardAll
Clipboard =
;Copy current selection, continue if no errors.
SendInput, ^c
ClipWait,
if !(ErrorLevel) {
;Convert Clipboard to text, auto-trim leading and trailing spaces and tabs.
Clipboard = %Clipboard%
;Clean Clipboard: change carriage returns to spaces, change >=1 consecutive spaces to +
Clipboard := RegExReplace(RegExReplace(Clipboard, "\r?\n"," "), "\s+","+")
;Open URLs, Google non-URLs. URLs contain . but do not contain + or .. or @
if Clipboard contains +,..,@
else if Clipboard not contains .
else
Run %Clipboard%
}
;Restore Clipboard, clear prevClipboard variable.
Clipboard := prevClipboard
prevClipboard =
return

Go fullscreen for any window (found here):

; When I press Alt+Enter, I want to get the current window fullscreen
Alt & Enter::
WinGet, TempWindowID, ID, A
If (WindowID != TempWindowID)
{
WindowID:=TempWindowID
WindowState:=0
}
If (WindowState != 1)
{
WinGetPos, WinPosX, WinPosY, WindowWidth, WindowHeight, ahk_id %WindowID%
WinSet, Style, ^0xC40000, ahk_id %WindowID%
WinMove, ahk_id %WindowID%, , 0, 0, A_ScreenWidth, A_ScreenHeight
}
Else
{
WinSet, Style, ^0xC40000, ahk_id %WindowID%
WinMove, ahk_id %WindowID%, , WinPosX, WinPosY, WindowWidth, WindowHeight
}
WindowState:=!WindowState
return

Replace the printscreen tool by the Spinning tool :

PRINTSCREEN::Run, "C:\Windows\System32\SnippingTool.exe"

And finally, a very useful shortcut I just made for editing WordPress posts:

!x::
IfWinActive, ahk_class MozillaWindowClass
Send, !+a
return

Which does : if I press Alt+x, it checks if Mozilla is open. If so, it sends Alt+Maj+a, which is the shortcut to add an hyperlink on WordPress!

Create a new folder :

;When I press alt+n, I want to create a new folder
!n::send {AppsKey}wf

I stop here, and leave my actual script on Pastebin if you want to have a look. I hope this will give you an idea of how powerful Autohotkey is, and now that I got my favorite gears back, I hope you’ll adopt some as well. And never fear a small Google search for something you think is useful : this XKCD picture about automation says it all.

Picture found here.

## 5 thoughts on “Get rid of repetitive actions with Autohotkey (Windows)”

1. mmm, interesting for capitalized french letters as ç, é, è… they’re always trouble to type. And also for iTunes. Other than that I don’t really see the point. Care to share your .ahk file ?

• My script is just here, I’ll update the article as I modify it.

What do you don’t see the point of ? Text completion ? Program launching ?

• Program Launching : hit windows and a number, or windows and start typing the name of the program if it’s not in your taskbar (windows7)
Text Completion : I use Sublime Text 3 to code so it deals quite well with auto completion (called snippets there), if you’re talking about writing, I don’t see the point in auto completion, except if you’re always writing the same words/catch phrases. And you should not, it’s boring 🙂