AutoHotkey Scripts for Developers

Autohotkey is an open-source scripting language for Windows that can be very useful to automate repetitive tasks. In this post, we will look at some example AutoHotkey scripts that might be useful particularly for developers. You might find one of the scripts helpful or you might get an idea about how AutoHotkey can be used in your developer setup.

KeyBoard Hotkeys

You can use AutoHotkey to map frequently used programs with keyboard keys. This example uses F5 to launch Chrome and F6 to launch Visual Studio Code. If they are already open, both keys can still be used to bring the program to front making switching between then easy which is particularly helpful when you have many apps open.

		f5::chrome()
		f6::vscode()

		chrome() {
			Process, Exist, chrome.exe
			{
				If ! errorLevel {
					Run, chrome.exe
				}
				else {	
					SetTitleMatchMode RegEx
					WinActivate Google Chrome
				}
			}
		}
		
		vscode() {
			Process, Exist, Code.exe
			{
				If ! errorLevel {
					Run, C:\Users\userName\AppData\Local\Programs\Microsoft VS Code\code.exe
				}
				else {	
					SetTitleMatchMode RegEx
					WinActivate Visual Studio Code
				}
			}
		}
	

We can also extend this script to open a specific project folder in visual studio code or run specific commands after opening code.

Automatic Build

If you need to rebuild your application after every a file is changed in the project folder, then AutoHotkey can save you some time there. This following example keeps an eye on the project folder and all its subfolders and whenever a file is changed, it rebuilds the application. In this instance, we are using a simple Python HTTP server to demonstrate.

		#Persistent
		; ALT + h to start the application
		!h::realtime_python_http_server()

		; Download WatchFolder.ahk and put it in the same folder as the script
		#Include WatchFolder.ahk

		; Start the application and listen for changes in project folder and subfolders
		realtime_python_http_server() {
			FolderPath := "C:\Users\userName\Dropbox\Personal\Dev\AutoHotKey"
			Run, python -m http.server 8000,,min
			WatchFolder(FolderPath, "rebuild_python_http_server", SubTree := True, Watch := 0x016)
		}
		
		; When a file is updated, Close existing process and run it again
		rebuild_python_http_server(Folder, Changes) {
			Process, Close, python.exe
			Run, python -m http.server 8000,,min
		}
	

WatchFolder.ahk is available to download from AutoHotkey Forum.

Custom Search

Select any text and press ALT + S or ALT + Left Click to trigger the script. When triggered, the script will copy the text and search it on google. You can choose any keyboard or mouse combination and search the text on any search engine.

		!s::search()
		!LButton::search()

		search() {
			Clipboard := ""
			Send, ^c
			ClipWait, 2
			Run, https://www.google.com/
			WinWaitActive, Google, ,10
			if ErrorLevel {
				MsgBox, Seems like google.com did not open.
				return
			}
			Send, ^v 
			Send, {Enter}
			return
		}
	

I use this script especially to search for errors and exceptions while I am writing code. I can also use custom search query. For example, let's say you are getting an error and the app says that it cannot concatenate string with int. What I like to do is, I would select that text and add the name of the language I am developing the app in, so the query would become, "cannot concatenate string with int python".

Files Filter

I download diagnostic log files on a regular basis at work and often they get lost in my downloads folder. The following script helps me better organize the files. It loops through all the CSV files in downloads folder that contain the word diag in the filename and moves them into the logs folder. Also, it runs every time a new file is added in the downloads folder.

		#Persistent

		; Download WatchFolder.ahk and put it in the same folder as the script
		#Include WatchFolder.ahk
		
		WatchFolder("C:\Users\userName\Downloads", "move_log_files", SubTree := False, Watch := 0x01)
		
		move_log_files(Folder, Changes) {
			FormatTime, CurrentDateTime,, dd-MM-yyyy-HHmmss
			Loop, Files, C:\Users\userName\Downloads\*diag*.csv
			{	
				; Move and rename the file with current date and time.
				; For example. diag.csv will become 07-09-2019-203332_diag.csv
				FileMove, %A_LoopFileLongPath%, %A_MyDocuments%\Logs\%CurrentDateTime%_%A_LoopFileName%
			}
		}
	

WatchFolder.ahk is available to download from AutoHotkey Forum.

What else can AutoHotkey do?

Virtually anything you can do with your keyboard or mouse. It can be used to simulate mouse clicks and key presses. You can also make HTTP requests, simple Graphical User Interface and file I/O with it. AutoHotKey Docs is a good place to start.

Running the Scripts

AutoHotkey files have an extension of .ahk. To run a script, just double click it. Please note that Autohotkey should be installed on your Windows computer and can be downloaded from autohotkey.com. The examples in this post are written in version 1.1.30.03. They are also available on Github.

Thanks for Reading, I hope you found this article useful. 🙂