USB Applications › AutoHotkey


AutoHotkey, usually abbreviated to AHK, is a great program to automate repetitive tasks in Windows. Among other things, you can use it to launch programs and to make hotstrings, i.e., auto-expanding abbreviations. AHK hotstrings work in any application that receives text input, be it a word processor, a text editor, an email client or a form in a web page.

What is more, since AutoHotkey is fully portable, you can use the same hotstrings file in any Windows computer or any Windows installation. You will never have to redefine your abbreviations again, unless you decide to switch to a different macro tool or to stop using Windows.

How is AutoHotkey portable?

AHK is a standalone program. Its installer simply adds some items to the context menu and the start menu, associates AHK files with the AutoHotkey engine (so that the system will know what program to use to execute them), and installs an uninstallation routine to undo all the above.

Portable AutoHotkey

Sample script

This is a script with simple threads, drawing on examples from the AHK Help file. You can add or remove hotkeys and hotstrings, and you can also include external scripts. See section: EXTERNAL SCRIPTS TO INCLUDE. There are many wonderfully useful scripts in the AHK forum and in the AHK Help file,made by AHK experts.

The double modifier Ctrl+Win is used to avoid overriding system hotkeys such as Win+E. In case of conflict, non-AutoHotkey hotkeys are overriden while a script runs. If you don’t use native Windows hotkeys, you can start with a single modifier, e.g., Winkey.

Lines starting with a semicolon are comments, as are strings after semicolons. Comments are ignored by AutoHotkey. Blank lines are also ignored. They were inserted for readability. Indentation is optional and can be arbitrary; consistent indentation makes a script more legible and easier to maintain.

The script below works with the paths indicated in the relevant comment, and defined in the VARIABLES section.

a4u.ahk

; HOTKEYS QUICK REFERENCE
; --------------------------------------------------------------------------

; !   Alt
; ^   Ctrl
; +   Shift
; #   Winkey

; PATHS IN THE KEYDRIVE
; --------------------------------------------------------------------------

; PROGRAMS DIRECTORY      X:\Apps
; SAMPLE PROGRAM PATH     X:\Apps\PortableFirefox\PortableFirefox.exe
; AUTOHOTKEY ENGINE       X:\Apps\AutoHotkey\AutoHotkey.exe
; AUTOHOTKEY SCRIPT       X:\Apps\AutoHotkey\a4u.ahk
; DOCUMENTS DIRECTORY     X:\Docs

; VARIABLES
; --------------------------------------------------------------------------

; A couple of variables, to avoid typing long paths and to keep the script tidy.
; The first is the Apps directory (relative to the script location).

A = %A_ScriptDir%\..
H = %A_ScriptDir%\..\PortableFirefox\PortableFirefox.exe http://

; EXTERNAL SCRIPTS TO INCLUDE
; --------------------------------------------------------------------------

; A file with many long scripts and threads can be difficult to maintain.
; You can save long scripts as separate ahk files in the same directory
; as the main script, and call them by means of the directive #Include.

; Below ISwitch.ahk is included, an excellent script for keyboard freaks
; who work with many open windows, especially many windows of the same
; program. It was submitted to the AutoHotkey forum by keyboardfreak:
; http://www.autohotkey.com/forum/viewtopic.php?t=1040

#Include %A_ScriptDir%
#Include ISwitch.ahk

; SCRIPT-SPECIFIC HOTKEYS
; --------------------------------------------------------------------------

; All commands in this section are also available from the system tray
; menu. Edit opens the script in Notepad, or in the associated editor.
!^#e::Edit     ; Edit the script by Alt+Ctrl++Win+E.
!^#s::Suspend  ; Toggle hotkeys set by the script by Alt+Ctrl++Win+S.
!^#r::Reload   ; Reload the script by Alt+Ctrl++Win+R.
!^#x::ExitApp  ; Terminate the script by Alt+Ctrl++Win+X.

; BASIC WINDOWS MANIPULATION
; --------------------------------------------------------------------------

#Up::WinMaximize, A    ; Maximize active window by Win+UpArrow.
#Down::WinRestore, A   ; Restore active window by Win+DownArrow.
#Left::WinMinimize, A  ; Minimize active window by Win+LeftArrow.

; NOTE
; Single-line hotkeys and hotstrings like the ones above allow for a
; simplified syntax without a command to end the thread. Multi-line
; threads keep executing until a Return (or Exit) is encountered.

; SYSTEM TOOLS, UTILITES  & ENHANCEMENTS
; --------------------------------------------------------------------------

^#Numpad0::Send, {VOLUME_MUTE}    ; By Alt+Win+Numpad0 (2000/XP).
^#NumpadSub::Send, {VOLUME_DOWN}  ; By Alt+Win+Numpad- (2000/XP).
^#NumpadAdd::Send, {VOLUME_UP}    ; By Alt+Win+Numpad+ (2000/XP).

^#n::Run, sndvol32     ; Open Volume Control.
^#r::Run, regedit      ; Open the Windows Registry Editor.
^#t::Run, taskmgr      ; Open Task Manager (or, in XP: Ctrl+Shift+Esc).
^#y::Run, desk.cpl     ; Open Display Properties.
^#Right::Run, control  ; Open Control Panel.

; Eject/retract tray of main CD/DVD drive by Alt+Ctrl+Win+Space.
!^#Space::
    Drive, Eject
    If A_TimeSinceThisHotkey < 1000
        Drive, Eject, , 1
    Return 

; Make directory in Open/Save File dialogs by pressing F8.
; Elsewhere F8 sends itself (the $ is used to allow this). 
$F8::
  IfWinNotActive, ahk_class #32770
  {
    Send, {F8}
    Return
  }
  PostMessage, 0x111, 40962
  Return

; Get information for selected drive. See AHK Help for details.
^#F9::
    FileSelectFolder, folder, , 3, Pick a drive to analyze:
    If folder =
        Return
    DriveGet, list, list
    DriveGet, cap, capacity, %folder%
    DrivespaceFree, free, %folder%
    DriveGet, fs, fs, %folder%
    DriveGet, label, label, %folder%
    DriveGet, serial, serial, %folder%
    DriveGet, type, type, %folder%
    DriveGet, status, status, %folder%
    MsgBox, , Drive information,
    ( LTrim
        All Drives: %list%
        Selected Drive: %folder%
        Drive Type: %type%
        Status: %status%
        Capacity: %cap% M
        Free Space: %free% M
        Filesystem: %fs%
        Volume Label: %label%
        Serial Number: %serial%
    )

; ABBREVIATIONS & MACROS
; --------------------------------------------------------------------------

; NOTE
; Users of multiple languages or multiple keyboard layouts may have to use 
; some additional code to make this work properly. See FAQ in AHK Help.

::lm::Life is miserable. ; Type "lm" and then Space.
::lb::Life is beautiful.

; NOTE
; The pair of parentheses below defines a continuation section.
; Continuation sections preserve hard carriage returns (Enter) and tabs.

::k.net::
    Send,
    ( LTrim
        Note to self
        Don't forget to bookmark this fabulous site,
        and to send the link to friends:
        http://www.kikizas.net/en/
    )
    Return

; SPECIAL CHARACTERS
; --------------------------------------------------------------------------

^#5::Send, {ASC 0128} ;  Type Euro symbol.
^#-::Send, {ASC 0150} ;  Type en dash.
^#=::Send, {ASC 0151} ;  Type em dash.
^#6::Send, {ASC 0162} ;  Type Cent symbol.
^#4::Send, {ASC 0164} ;  Type currency sign.
^#2::Send, {ASC 0178} ;  Type superscript two.

; MISCELLANEOUS
; --------------------------------------------------------------------------

; Type current date and time by Ctrl+Win+F5. Format: 19991231-2359.
^#F5::Send, %A_Year%%A_Mon%%A_MDay%-%A_Hour%%A_Min%

; RUN USB APPLICATIONS
; --------------------------------------------------------------------------

; Paths below are relative to the directory of the script, (see variable
; %A% at the top). The string in the second parameter of Run is the working
; directory. Not all programs need this, but some do. Include it to be safe.

; The first hotkey starts Converber. The second starts i.Disk.
^#c::Run, %A%\Converber\Converber.exe, %A%\Converber
^#i::Run, %A%\i.Disk\i.Disk.exe, %A%\i.Disk

; OPEN PAGES IN FIREFOX
; --------------------------------------------------------------------------

; Go to Portable Firefox, Tools, Options, Advanced, to define whether
; pages will open in a new window, a new tab, or the most recent tab.
; The variable H includes the full path to the Portable Firefox executable,
; then a space, and then the following seven characters: http://, so that
; you don’t have to type each time what is common in all commands.

+#b::Run, %H%news.bbc.co.uk/2/low/
+#k::Run, %H%www.kikizas.net/en/usbapps.html

; OPEN A DIRECTORY IN THE KEYDRIVE
; --------------------------------------------------------------------------

^#d::Run, %A_ScriptDir%\..\..\Docs   ; Open X:\Docs in default file manager.
^#a::Run, %A%\A43\A43.exe ..\..\Docs ; Open X:\Docs in A43.

; RUN/ACTIVATE USB APPS
; --------------------------------------------------------------------------

; The threads below start by looking for a window of the program.
; If one is found, it is activated; if not, the program is started.
; For programs that support single-instance, you can activate this
; program option and then use a single-line Run command instead.

; NOTE
; AHK can identify most windows by a unique string in the caption
; (the uppermost bar). Windows that display no such fixed string
; (like 1by1) can be identified by window class. To get the window
; class, use Window Spy, included in AHK and accessible from
; the system tray icon of the running script.

^#1:: ; Start 1by1, or activate it if already running.
    IfWinExist, ahk_class 1by1WndClass
    {
        WinActivate
    }
    Else
    {
        Run, %A%\1by1\1by1.exe, %A%\1by1
        WinWait, ahk_class 1by1WndClass
        WinActivate
    }
    Return

; NOTE
; MatchMode 2, used in all following threads, looks for the specified
; string anywhere in the caption. MatchMode 1 looks for the string in
; the beginning of the caption. MatchMode 3 looks for a window whose
; title matches the string exactly.

^#k:: ; Start KeePass, or activate it if already running.
    SetTitleMatchMode, 2
    IfWinExist, KeePass Password Safe
    {
        WinActivate
    }
    Else
    {
        Run, %A%\KeePass\KeePass.exe, %A%\KeePass
        WinWait, KeePass Password Safe
        WinActivate
    }
    Return

^#s:: ; Start SciTE, or activate it if already running.
; The first line sets the environment variable SciTE_HOME,
; to tell SciTE to look for user-specific files in its own
; directory, instead of the default Documents and Settings.
    EnvSet, SciTE_HOME, %A%\SciTE
    SetTitleMatchMode, 2
    IfWinExist, - SciTE
    {
        WinActivate
    }
    Else
    {
        Run, %A%\SciTE\SciTE.exe, %A%\SciTE
        WinWait, - SciTE
        WinActivate
    }
    Return

^#v:: ; Start VLC with parameters, or activate it if already running. 
; The long, parametrized command was split by means of ||.
    SetTitleMatchMode, 2
    IfWinNotExist, VLC media player
        Run, %A%\VLC\vlc.exe 
        || --no-plugins-cache --config=%A%\VLC\vlcrc",
        || %A%\VLC
        WinWait, VLC media player
    WinActivate
    Return
            

Thanks

Thanks to David Engelhardt for looking at a draft of this and suggesting ideas.