Ometis Blog

AutoHotkey: Automation and Shortcuts | Ometis - Ometis Blog

Written by Ometis | Nov 5, 2020 12:36:10 PM

What is AutoHotkey?

AutoHotkey is a free, open-source scripting language for Windows that allows users to easily create small to complex scripts for all kinds of tasks, such as form fillers, auto-clicking, macros, etc. You can find more about it here: https://www.autohotkey.com/

In other words, it’s one of the safest ways to run custom scripts to automate simple tasks and create shortcuts on a Windows machine.

I started using AHK more than six years ago. At the beginning it was only to store commonly used passwords – which I’d advise against doing now – but that’s what got me started. I started reading blog posts and forums about it and discovered the crazy things some people did with it, and that gave me ideas. Over those last six years I’ve been creating small snippets and modifying some that I found to better suit my needs. They have become second nature to me, to the point where sometimes I’ll forget I’m on another machine when I’m still hitting the key combination for one of the snippets and it doesn’t work. They are simple things, but simple things can sometimes be very useful, so now I present them to you. Here’s a link to the code so you can follow the next points in this blog: https://gist.github.com/alvaro-ometis/696b0e0a13a94c6a0321791dd24401dc.

AutoHotkey Function list

Load * Inline [];

Replaces the string l8i with the string

LOAD * INLINE [

];

and leaves your cursor in the second line.

Dollar bracket expansion – $()

Replaces the string $V (uppercase V) with $() and places the cursor in the middle of the brackets. This might only save a keystroke or two but after using it for a while I find it easier to type than the alternative.

Wrap selection in dollar bracket – $(someText)

If you select some text and hit CtrlShift4 it will wrap the selected text with the dollar bracket expansion. E.g.: vDate >> $(vDate)

Wrap selection in square brackets – [someField]

If you select some text and hit Alt + [ it will wrap the selected text with square brackets. E.g.: Field with Spaces >> [Field with Spaces]

Navigate to previous script section (Qlik Sense)

Holding Alt and scrolling up with the mouse scroll wheel will navigate to the previous (up) script section/tab in the Data Load Editor.

Navigate to next script section (Qlik Sense)

Holding Alt and scrolling down with the mouse scroll wheel will navigate to the next script (down) section/tab in the Data Load Editor.

Toggle comments in script

Holding Ctrl and pressing the / key will toggle comments on the Data Load Editor. This is currently available as Ctrl + K but since pretty much all other code editors use Ctrl + / I decided to create a shortcut for myself.

These last three are all targeted towards a specific internet browser since that’s where Qlik Sense lives for the most part. If you open the AHK script, you’ll see a commented section just above these three snippets of code where I listed the codes for the most commonly used browsers. You can edit the snippets to target your browser like this:

; [Qlik] - Hotkeys - [Alt] [Mouse Scroll Wheel Up] - Navigates to the previous script section
#IfWinActive ahk_exe chrome.exe
!WheelUp::PgUp

Where it says #IfWinActive ahk_exe chrome.exe you can replace ahk_exe chrome.exe with ahk_exe firefox.exe to target Mozilla Firefox or any other browser code on the list.

Email starter

Replaces the string ;hi; with

Hi ,



Regards,
YOUR_NAME_HERE

And places the cursor just before the comma after “Hi” so you can type the name of the person who you’re writing to. Make sure you replace “YOUR_NAME_HERE” in the code with your actual name. This just makes my life easier when responding to emails or support tickets, especially to make sure I don’t make a mistake and sign with Retards instead of Regards – it almost happened a couple of times back when I manually typed it – it’s not good for customer satisfaction.

Open VS Code or switch to it

Holding Win – the Windows key – and pressing C will open Visual Studio Code, my code editor of choice, or switch to it if it’s already open. Sure you can just pin it to your taskbar and use Win + the number for the position it has on your bar, but if you have multiple windows opened, it’ll switch to your most recent one. It’s a small detail but, again, it makes my life easier which is the point of this.

Open Windows Explorer window or switch to it

Holding Win and pressing E will open the Windows Explorer window or switch to it if there’s one open already. The first part, opening the Windows Explorer with Win + E is a Windows thing, you can try it without running AutoHotKey and it should open the Windows Explorer. The issue with it is that if you do it again, it’ll open a new window. My function attempts to fix that and switches to the Explorer window if you have one open already.

Open new Windows Explorer window

Holding Win and Shift and then pressing E will always open a new Windows Explorer window. This was necessary in order to have the ability to open a new Explorer window. I decided to use Shift since it’s the same key Windows uses when you want to open a new window of one of the programs pinned to your taskbar.

AutoHotkey Function basics

Each function has a comment above that describes the type of function, how to trigger it, and what it does. Let’s break down one of the examples to see what it does:

; [Qlik] - Text replacement - l8i - Load * Inline
; e.g.: LOAD * INLINE [
;
; ];
:*:l8i::LOAD {*} INLINE {[}{Enter 2}{]};{Left 3}{Space 2}

[Qlik] indicates the category or area of business that the code falls under. It’s more of an organising tool that helps me separate the code into areas, nothing strict. On this script you’ll find the following categories: QlikSupport, and General.

Text replacement tells us that the code below will be triggered by replacing text the user typed, in this case it’s the string l8i. The types of triggers I created are: HotkeysText selection + Hotkeys, and Text replacement.

l8i is the string of text the user has to type in order to trigger the execution of the code. When the trigger includes hotkeys it’ll be displayed like this [Ctrl] [/]. This means the trigger is holding the key Ctrl and pressing forward slash.

Load * Inline is a comment describing what the code does. In this case it generates the load * inline statement.

Finally, there might be an example section where the resulting code will be displayed. Not all the code sections have one since some open applications or interact with different windows. In this case we can see the load * inline statement:

; e.g.: LOAD * INLINE [
;
; ];

How to use AutoHotkey

  1. Navigate to https://www.autohotkey.com/ and download the current version. Follow the steps on the installation wizard and hit Exit when the installation is complete.
  2. Download the custom script that contains all the snippets described in this post. You can find it at https://gist.github.com/alvaro-ometis/696b0e0a13a94c6a0321791dd24401dc. Once it’s downloaded, place it on a folder of your choosing. I have it on the My Documents folder but it’s up to you.
  3. Right click the .ahk file you just downloaded and create a shortcut to it. Leave the Explorer window open as we’ll have to move that shortcut to another folder later on.
  4. Go to the startup folder on your PC. If you’re on Windows 10, you can do this in a few different ways:
    1. Navigate to C:\Users\YOUR_USER_HERE\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup
    2. Right click the start menu icon and click on Run. Type shell:startup and hit Enter or OK.
    3. Press Win + R and type shell:startup and hit Enter or OK.
  5. Drag the shortcut we created on step 3 to the startup folder you just opened. That way we make sure Windows launches the script every time the PC boots up.
  6. Finally, just execute the script through the shortcut or the original file. A green icon with a white H should appear on your system tray which means the program is running.

Learn more about AutoHotkey

Here’s a list of resources that helped me on my quest for automation with AHK.

I hope you enjoy using this tool as much as I do. Feel free to leave a comment and let me know what you think!

 

By Álvaro Martínez