Tuesday, February 5, 2013

My current base AutoHotKey script


Posting my latest AutoHotKey base script - the one I have set to load on system startup. You can run multiple scripts concurrently simply by double-clicking them, and I have a number of scripts I run for specific purposes, while I'm developing, doing graphics work, etc., but this is the one I have running all the time.

If you have AutoHotKey installed and want to try this script out, download it and double-click to run it. Most of the snippets inside have been snagged from other scripts, attributions with links are included inline.

Currently it supports the following features:

Hotkeys

You'll need to have the associated programs installed, obviously. You can update your paths in the script as necessary.

  • Pressing Ctrl/Win/Alt + P opens a new Notepad window.
  • Pressing Ctrl/Win/Alt + N opens Notepad++ (or switches to it if it's already running).
  • Pressing Ctrl/Win/Alt + S opens Sublime Text 2.
  • Pressing Ctrl/Win/Alt + C opens ConEmu.


Change text case

  • Pressing Ctrl/Win/Alt + U converts the selected text to uppercase.
  • Pressing Ctrl/Win/Alt + L converts the selected text to lowercase.
  • Pressing Ctrl/Win/Alt + K capitalizes the selected text.

Hotstrings

  • Pressing ]d followed by one of the standard character endings - a space, period, tab, etc. spits out a date/time stamp.

Window transparency

I find this useful for many situations, but especially when I'm doing layout work and want a pixel-perfect match to the design.
  • Holding Win/Alt and then either hitting the Up arrow key or scrolling up with the mouse wheel increases the transparency of the active window.
  • Holding Win/Alt and then either hitting the Down arrow key or scrolling down with the mouse wheel decreases the transparency of the active window.
  • Holding Win/Alt and tapping G displays a tooltip with current transparency value of the window under the mouse pointer.
  • Holding Win/Alt and either clicking anywhere with the left mouse button or tapping the left arrow key turns the window completely opaque.
  • Holding Win/Alt and either clicking anywhere with the right mouse button or tapping the right arrow key turns the window almost completely transparent..


Window always on top

  • Holding Win/Alt and tapping T sets the active window to Always on Top.


Window drag

This is basically the Linux feature where you can drag a window around by clicking anywhere in it, instead of having to click the title bar. I rarely use this, especially with the existing controls in Windows, as well as the new ones added with Windows 8, but it comes in handy occasionally.
  • Clicking anywhere in a window while holding down the right Alt key moves the window around with the mouse.

Volume

  • Pressing Ctrl/Win/Alt and then either tapping the Up arrow key or scrolling up with the mouse wheel increases the volume in increments of 2.
  • Pressing Ctrl/Win/Alt and then either tapping the Down arrow key or scrolling down with the mouse wheel decreases the volume in increments of 2.
  • Pressing Ctrl/Win/Alt and then hitting the Left arrow key mutes the volume.
  • Scrolling with the mouse over the taskbar increases/decreases the volume (no keyboard interaction necessary for this).

The script

You can download the script here (base.ahk), and I've posted it for quick viewing below (you can also just copy/paste the following locally and give it a .ahk extension).



; MY BASE FILE
; @threecleartones
; ==========================================================================

; NOTES
; ==========================================================================
; http://www.autohotkey.com/docs/KeyList.htm
; http://www.autohotkey.com/docs/Hotkeys.htm
; + is SHIFT
; ^ is CTRL
; # is WINDOWS
; ! is ALT


; HOTKEYS
; ==========================================================================
$Pause:: Suspend  ; Suspend/unsuspend AutoHotKey, just in case I need to.
^#!p:: Run Notepad  ; Open a new Notepad window
^#!n:: Run Notepad++  ; Open Notepad++
^#!s:: Run "C:\Program Files\Sublime Text 2\sublime_text.exe"  ; Open Sublime
^#!c:: Run "C:\Program Files\ConEmu\ConEmu64.exe"  ; Open ConEmu

; Change text case
; Snagged from http://webserver.computoredge.com/online.mvc?issue=3029&article=vista
^#!u::                             ; convert text to upper
 Clipboard:= ""
 Send, ^c ;copies selected text
 ClipWait
 StringUpper Clipboard, Clipboard
 Send %Clipboard%
return

^#!l::                            ; convert text to lower
 Clipboard:= ""
 Send, ^c ;copies selected text
 ClipWait
 StringLower Clipboard, Clipboard
 Send %Clipboard%
return

^#!k::                           ; convert text to capitalized
 Clipboard:= ""
 Send, ^c ;copies selected text
 ClipWait
 StringUpper Clipboard, Clipboard, T
 Send %Clipboard%


; HOTSTRINGS
; ==========================================================================
::]d::  ; This hotstring replaces "]d" with the current date and time via the commands below.
; FormatTime, CurrentDateTime,, yyyy.M.d h:mm tt
FormatTime, CurrentDateTime,, yyyy.M.d H:mm
SendInput %CurrentDateTime%
return


; WINDOW TRANSPARENCY
; ==========================================================================
; Snagged from http://www.howtogeek.com/howto/44915/how-to-change-window-transparency-in-windows-7/
#!WheelUp::  ; Increments transparency up by 3.375% (with wrap-around)
#!Up::
    DetectHiddenWindows, on
    WinGet, curtrans, Transparent, A
    if ! curtrans
        curtrans = 255
    newtrans := curtrans + 8
    if newtrans > 0
    {
        WinSet, Transparent, %newtrans%, A
    }
    else
    {
        WinSet, Transparent, OFF, A
        WinSet, Transparent, 255, A
    }
return

#!WheelDown::  ; Increments transparency down by 3.375% (with wrap-around)
#!Down::
    DetectHiddenWindows, on
    WinGet, curtrans, Transparent, A
    if ! curtrans
        curtrans = 255
    newtrans := curtrans - 8
    if newtrans > 0
    {
        WinSet, Transparent, %newtrans%, A
    }
    ;else
    ;{
    ;    WinSet, Transparent, 255, A
    ;    WinSet, Transparent, OFF, A
    ;}
return

#!LButton::  ; Make the window 100% opaque - reset Transparency Settings
#!LEFT::
    WinSet, Transparent, 255, A
    WinSet, Transparent, OFF, A
return

#!RButton::  ; Make the window 80% transparent
#!RIGHT::
    WinSet, Transparent, 20, A
return

#!g::  ; Press Win+G to show the current settings of the window under the mouse.
    MouseGetPos,,, MouseWin
    WinGet, Transparent, Transparent, ahk_id %MouseWin%
    ToolTip Translucency:`t%Transparent%`n
Sleep 2000
ToolTip
return


; WINDOW ALWAYS ON TOP
; ==========================================================================
; Snagged from http://www.howtogeek.com/howto/13784/keep-a-window-on-top-with-a-handy-autohotkey-script/
#!t::  Winset, Alwaysontop, , A


; WINDOW DRAG
; ==========================================================================
; Snagged from http://www.howtogeek.com/howto/windows-vista/get-the-linux-altwindow-drag-functionality-in-windows/
; This script modified from the original: http://www.autohotkey.com/docs/scripts/EasyWindowDrag.htm
; by The How-To Geek
; http://www.howtogeek.com 

RAlt & LButton::
CoordMode, Mouse  ; Switch to screen/absolute coordinates.
MouseGetPos, EWD_MouseStartX, EWD_MouseStartY, EWD_MouseWin
WinGetPos, EWD_OriginalPosX, EWD_OriginalPosY,,, ahk_id %EWD_MouseWin%
WinGet, EWD_WinState, MinMax, ahk_id %EWD_MouseWin% 
if EWD_WinState = 0  ; Only if the window isn't maximized 
    SetTimer, EWD_WatchMouse, 10 ; Track the mouse as the user drags it.
return

EWD_WatchMouse:
GetKeyState, EWD_LButtonState, LButton, P
if EWD_LButtonState = U  ; Button has been released, so drag is complete.
{
    SetTimer, EWD_WatchMouse, off
    return
}
GetKeyState, EWD_EscapeState, Escape, P
if EWD_EscapeState = D  ; Escape has been pressed, so drag is cancelled.
{
    SetTimer, EWD_WatchMouse, off
    WinMove, ahk_id %EWD_MouseWin%,, %EWD_OriginalPosX%, %EWD_OriginalPosY%
    return
}
; Otherwise, reposition the window to match the change in mouse coordinates
; caused by the user having dragged the mouse:
CoordMode, Mouse
MouseGetPos, EWD_MouseX, EWD_MouseY
WinGetPos, EWD_WinX, EWD_WinY,,, ahk_id %EWD_MouseWin%
SetWinDelay, -1   ; Makes the below move faster/smoother.
WinMove, ahk_id %EWD_MouseWin%,, EWD_WinX + EWD_MouseX - EWD_MouseStartX, EWD_WinY + EWD_MouseY - EWD_MouseStartY
EWD_MouseStartX := EWD_MouseX  ; Update for the next timer-call to this subroutine.
EWD_MouseStartY := EWD_MouseY
return


; VOLUME
; ==========================================================================
; Adjusts the volume using a hotkey/mouse scroll combo
#!^Up::
#!^WheelUp::
Send {Volume_Up 2}
return
#!^Down::
#!^WheelDown::
Send {Volume_Down 2}
return
#!^Left::Send {Volume_Mute}

; Adjusts the volume when mouse scrolling over the taskbar (like volumouse)
; Snagged from http://l.autohotkey.net/docs/commands/_If.htm#Examples
#If MouseIsOver("ahk_class Shell_TrayWnd")
WheelUp::Send {Volume_Up}     ; Wheel over taskbar: increase/decrease volume.
WheelDown::Send {Volume_Down} ;


; MISC STUFF
; ==========================================================================
; Reload the script by middle clicking on the taskbar.
; This is useful when making changes to the script, but also necessary when the volume stuff further down stops working for some reason.
; Also, this has to be at the end of the file for some reason.
#If MouseIsOver("ahk_class Shell_TrayWnd")
MButton::Reload
MouseIsOver(WinTitle) {
    MouseGetPos,,, Win
    return WinExist(WinTitle . " ahk_id " . Win)
}
#SingleInstance force  ; Allows only one instance of this script to run.