Automation using AHK

Disclaimer: Most of the scripts I am presenting in this post is a collection that I made searching the web.

AutoHotkey (AHK) is a scripting language that allow us to automate our daily tasks, create macros and much more.

The benefits of automation in a computing world is such a big deal, and not so many people take advantage from it, as they don't know what they could use it for.

In reality it's really simple, every time you find yourself repeating the same exact task over and over again you have a perfect indication that you should probably automate that process. Of course writing the script to automate your task will take time and testing, but in the long run it will win over doing the same actions manually.

I want to make it clear that automation is not only useful for programmers but for anyone that use a computer.

To give you an idea let's say that each and every morning I am:

  • Checking mails on the web client
  • Opening my preferred online newspaper
  • Open Microsof Excel
  • Open your favorite messaging desktop client
  • Open a note application
  • Open youtube/spotify and start listening music
  • Open Microsoft team

You could achieve this 7 actions with just one single click.

Of course this script won't give you such a massive benefit, but you get the idea why automation is a powerful tool.

Automation will save you a lot of time and money.

The sentence I use the most when dealing with automation is "Working smarter instead of harder".

AHK does require some base scripting knowledge but in my opinion it's well worth put some time into it and learn such a useful skill.

I use AHK in conjunction with Stream Deck, so I don't have to remember complex key bindings like CTRL+ALT+WIN+P

AHK scripts run with keyboard key-bindings, so you can personalize them as you wish.

Auto login (use webdriver and python3)

I wrote this script myself as I needed a way to autologin a website.

How to use: SHIFT+F2

Requirements: Python v.3.x.x

auto-login.ahk

Send c:{enter} ; Go to C drive
Send cd C:\gitea\auto-login{enter} ; Go to scripts folder
Send python login-script.py{enter} ; Execute python script

login-script.py

from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.by import By

options = Options()

options.binary_location=r'C:/Program Files/Google/Chrome/Application/chrome.exe'

options.add_experimental_option('excludeSwitches', ['enable-logging'])

s = Service('./chromedriver_win32/chromedriver.exe')

browser = webdriver.Chrome(service=s, options=options)

url='http://localhost:8080'

username = "name"

password = "pass"

browser.get(url)

browser.find_element(By.ID,"userID").send_keys(username)

browser.find_element(By.ID,"userPSW").send_keys(password)

browser.find_element(By.NAME,"btnLOGIN").click()

print("\nLogged-in succesfully...");

Open Windows terminal

How to use: CTRL+SHIFT+C

SwitchToWindowsTerminal()
{
    windowHandleId := WinExist("ahk_exe WindowsTerminal.exe")
    windowExistsAlready := windowHandleId > 0

    ; If the Windows Terminal is already open, determine if we should put it in focus or minimize it.
    if (windowExistsAlready = true)
    {
        activeWindowHandleId := WinExist("A")
        windowIsAlreadyActive := activeWindowHandleId == windowHandleId

    if (windowIsAlreadyActive)
    {
        ; Minimize the window.
        WinMinimize, "ahk_id %windowHandleId%"
    }
    else
    {
        ; Put the window in focus.
        WinActivate, "ahk_id %windowHandleId%"
        WinShow, "ahk_id %windowHandleId%"
    }
    }
    ; Else its not already open, so launch it.
    else
    {
        Run, wt
    }
}

; Hotkey to use Ctrl+Shift+C to launch/restore the Windows Terminal.
^+c::SwitchToWindowsTerminal()

Prompt in current folder

How to use: CTRL+ALT+U

^!u::     ; Use ctrl+alt+u to open cmd in current selected folder
{
  Send, !d
  Send, ^c
  Sleep 50
  Run wt, %clipboard%
  Return
}

Search current selected text

How to use: CTRL+ALT+C

^!c:: ; use ctrl+alt+c to search on google
{
 Send, ^c
 Sleep 50
 Run, http://www.google.com/search?q=%clipboard%
 Return
}

Side by side folder

How to use: CTRL+SHIFT+WIN+E

^+#e::                                  ; use ctrl+shift+win+e
{                                       ; to open new explorer window with the same selected folder
  Send, #{Left}
  Sleep 50
  Send, !d
  Sleep 50
  Send, ^c
  Sleep 100
  Run, Explorer "%clipboard%"
  Sleep 900
  Send, !{Up}
  Sleep 600
  Send, #{Right}
  Return
}

Open Websites and Apps

^#1::Run "https://gitlab.yoursite.com/dashboard/activity"   ; use ctrl+win+1 to open gitlab
^#2::Run "https://youtrack.yoursite.com/issues"   ; use ctrl+win+2 to open youtrack
^#3::Run "https://jenkins.yoursite.com/" ; use ctrl+win+3 to open Jenkins
^#4::Run "https://csstricks.com/" ; use ctrl+win+4 to open CssTricks

;^+!1::Run ""   ; use ctrl+shift+alt+1
;^+!2::Run ""   ; use ctrl+shift+alt+2
;^+!3::Run ""   ; use ctrl+shift+alt+3
;^+!4::Run ""   ; use ctrl+shift+alt+4
;^+!5::Run ""   ; use ctrl+shift+alt+5
;^+!6::Run ""

;^!t::Run cmd, C:\Users\Username         ; use ctrl+alt+t to run Cmd
;^!w::Run firefox.exe                     ; use ctrl+alt+w to run Firefox
;^!s::Run C:\Program Files\Sublime Text 3\sublime_text.exe
^!v::Run C:\Users\username\AppData\Local\Programs\Microsoft VS Code\Code.exe ;use ctrl+alt+v to run vscode

Fetch and pull

How to use: CTRL+ALT+P

^!p::
{
  SetWorkingDir, C:\repos\myrepo
  Run %comspec%  "%A_WorkingDir%"
  Sleep, 500
  Send, git fetch
  Send, {Enter}
  Send, git pull
  Send, {Enter}
  Sleep, 500
  Send, Exit,
  Send, {Enter}
}

Copy color (HEX) to clipboard

How to use: CTRL+WIN+LEFTCLICK

; Copy to clipboard the HEX color of the pixel under your cursor using CTRL+Win+LeftClick
^#LButton::
{
  MouseGetPos, MouseX, MouseY
  PixelGetColor, color, %MouseX%, %MouseY%, RGB
  StringLower, color, color
  clipboard := SubStr(color, 3)
  Return
}
Show Comments