Skip to content

Instantly share code, notes, and snippets.

@dewaka
Last active Nov 23, 2021
Embed
What would you like to do?
; Launch or focus Applications
appFlapper(exePath) {
exeName := substr(exePath, instr(exePath, "\",, 0)+1)
IfWinExist, % "ahk_exe " exeName
{
IfWinActive
{
WinWait, ahk_exe %exeName%
WinMinimize
}
else
{
WinWait, ahk_exe %exeName%
WinActivate
}
}
else
{
Run, %exePath%
}
}
; This is pretty custom becuase of a couple of reasons
; - I don't want to launch IDEA unless I really want to
; - I don't want to minimise IDEA unless I really want to
; That's why both launching and flapping of IDEA is optional. More explicit
; short cut which is not easily mistakenly input would have both options as
; true, but other shorter one would have both as false.
activateIDEA(launch, flap) {
IfWinNotExist ahk_exe idea64.exe
{
if launch
{
run, "C:\Program Files\JetBrains\IntelliJ IDEA 2018.1.5\bin\idea64.exe"
WinWait, ahk_exe idea64.exe
WinActivate, ahk_exe idea64.exe
}
}
else
{
if WinActive("ahk_exe idea64.exe")
{
if flap {
WinWait, ahk_exe idea64.exe
WinMinimize
}
}
else
{
WinWait, ahk_exe idea64.exe
WinActivate, ahk_exe idea64.exe
}
}
}
activateEmacs() {
IfWinNotExist ahk_exe emacs.exe
{
run, "C:\ProgramData\chocolatey\lib\emacs\tools\emacs\bin\runemacs.exe"
WinWait, ahk_exe emacs.exe
WinActivate, ahk_exe emacs.exe
}
else
{
if WinActive("ahk_class Emacs")
{
WinWait, ahk_exe emacs.exe
WinMinimize
}
else
{
WinWait, ahk_exe emacs.exe
WinActivate, ahk_exe emacs.exe
}
}
}
;; Excel
;; ahk_class XLMAIN
;; ahk_exe EXCEL.exe
activateExcel() {
IfWinNotExist ahk_class XLMAIN
{
run, "C:\Users\ChathuraColombage\Apps\WinApps\Excel"
WinWait, ahk_class XLMAIN
WinActivate, ahk_class XLMAIN
}
else
{
if WinActive("ahk_class XLMAIN")
{
WinWait, ahk_class XLMAIN
WinMinimize
}
else
{
WinWait, ahk_class XLMAIN
WinActivate, ahk_class XLMAIN
}
}
}
activateVim() {
IfWinNotExist ahk_class Vim
{
run, "C:\Program Files (x86)\Vim\vim81\gvim.exe"
WinWait, ahk_class Vim
WinActivate, ahk_class Vim
}
else
{
if WinActive("ahk_class Vim")
{
WinWait, ahk_class Vim
WinMinimize
}
else
{
WinWait, ahk_class Vim
WinActivate, ahk_class Vim
}
}
}
toggleDoubleCmder() {
IfWinNotExist ahk_class DClass
{
run, "C:\Program Files\Double Commander\doublecmd.exe"
WinWait, ahk_class DClass
WinActivate, ahk_class DClass
}
else
{
If WinActive("ahk_class DClass")
{
WinWait, ahk_class DClass
WinMinimize
}
else
{
WinWait, ahk_class DClass
WinActivate, ahk_class DClass
}
}
}
toggleSpotify() {
IfWinNotExist ahk_exe Spotify.exe
run, "C:\Users\ChathuraColombage\AppData\Roaming\Spotify\Spotify.exe"
WinWait, ahk_exe Spotify.exe
WinActivate, ahk_exe Spotify.exe
; else
; {
; if WinActive("ahk_exe Spotify.exe")
; {
; WinWait, ahk_exe Spotify.exe
; WinMinimize
; }
; else
; {
; WinWait, ahk_exe Spotify.exe
; WinActivate, ahk_exe Spotify.exe
; }
; }
}
toggleCmder() {
IfWinNotExist ahk_exe ConEmu64.exe
{
run, "C:\Users\ChathuraColombage\Apps\cmder_mini\Cmder.exe"
WinWait, ahk_exe ConEmu64.exe
WinActivate, ahk_exe ConEmu64.exe
}
else
{
if WinActive("ahk_exe ConEmu64.exe")
{
WinWait, ahk_exe ConEmu64.exe
WinMinimize
}
else
{
WinWait, ahk_exe ConEmu64.exe
WinActivate, ahk_exe ConEmu64.exe
}
}
}
toggleGitKraken() {
IfWinNotExist ahk_exe gitkraken.exe
{
run, "C:\Users\ChathuraColombage\AppData\Local\gitkraken\app-4.0.5\gitkraken.exe"
; run, "C:\Users\ChathuraColombage\AppData\Local\gitkraken\Update.exe", , "--processStart gitkraken.exe"
WinWait, ahk_exe gitkraken.exe
WinActivate, ahk_exe gitkraken.exe
}
else
{
if WinActive("ahk_exe gitkraken.exe")
{
WinWait, ahk_exe gitkraken.exe
WinMinimize
}
else
{
WinWait, ahk_exe gitkraken.exe
WinActivate, ahk_exe gitkraken.exe
}
}
}
Firefox := "C:\Program Files\Mozilla Firefox\firefox.exe"
Chrome := "C:\Program Files (x86)\Google\Chrome\Application\chrome.exe"
DefaultBrowser := Firefox
; Chrome
#^g::
appFlapper("C:\Program Files (x86)\Google\Chrome\Application\chrome.exe")
return
; Firefox
; ahk_class MozillaWindowClass
; ahk_exe firefox.exe
#^f::
appFlapper("C:\Program Files\Mozilla Firefox\firefox.exe")
return
;; Win+Control+Space for Browser
#^Space::
appFlapper(DefaultBrowser)
return
; Earlier mappings with Alt was really problematic with the UK layout's AltGr thing
; #!Space::
; appFlapper(DefaultBrowser)
; return
;; This is for AltGr abomination on UK keyboards to match above mapping for the right side!
; #^>!Space::
; appFlapper(DefaultBrowser)
; return
; Emacs
#^e::
activateEmacs()
return
; Excel
#^+e::
activateExcel()
return
#Space::
activateEmacs()
return
; GVim
#^v::
activateVim()
return
; Run new instance of GVim
#^+v::
run, "C:\Program Files (x86)\Vim\vim81\gvim.exe"
return
; IntelliJ IDEA
#^i::
activateIDEA(true, true)
return
; I'm not sure whether this is reliable or not
#+Space::
activateIDEA(false, false)
return
; Slack
#^s::
appFlapper("C:\Users\ChathuraColombage\AppData\Local\slack\slack.exe")
return
; Cmder
#^c::
toggleCmder()
return
; Spotify
#^p::
toggleSpotify()
return
; DoubleCommander
; I want Window+# here
#+e::
toggleDoubleCmder()
return
; ; Explorer
; #^x::
; IfWinNotExist ahk_exe explorer.exe
; run, "C:\Windows\explorer.exe"
; WinWait, ahk_exe explorer.exe
; WinActivate, ahk_exe explorer.exe
; return
; Task Manager
#^x::
IfWinNotExist ahk_exe Taskmgr.exe
run, "C:\Windows\System32\Taskmgr.exe"
WinWait, ahk_exe Taskmgr.exe
WinActivate, ahk_exe Taskmgr.exe
return
; GitKraken
#^k::
toggleGitKraken()
return
; Foxit Reader
; ahk_class classFoxitReader
; ahk_exe FoxitReader.exe
#+^f::
appFlapper("FoxitReader.exe")
return
;; Ad perfmon.exe
;; %windir%\system32\perfmon.exe /res
; #^r::
; IfWinNotExist ahk_exe perfmon.exe
; run, "C:\Windows\System32\perfmon.exe"
; WinWait, ahk_exe perfmon.exe
; WinActivate, ahk_exe perfmon.exe
; return
; macOS style minimise windows
#h::
WinMinimize, A
return
;;;;; AutoHotkey Media Keys ;;;;;
#^+h::Send {Volume_Down}
#^+l::Send {Volume_Up}
#^+m::Send {Volume_Mute}
#^+p::Send {Media_Prev}
#^+n::Send {Media_Next}
#^+j::Send {Media_Play_Pause}
; System-wide Emacs org-capture
$#f9::
; The logic should be as follows
; If Emacs is in focus
; send f9 key
; Else
; if Emacs is running
; Focus
; send f9 key
; else
; Do nothing - Launching and then capturing will take a long while!
IfWinExist ahk_exe emacs.exe
{
if !WinActive("ahk_class Emacs")
{
WinWait, ahk_exe emacs.exe
WinActivate, ahk_exe emacs.exe
}
Send {f9}
}
else
{
Send {#f9}
}
return
;; Window manipulation keys
;; TODO: Write a toggle function for Max or Restore
#+f::WinMaximize, A ;
#+r::WinRestore, A ;
#+m::WinMinimize, A ;
;; Desktop workspace switching
#]::
Send #^{Right}
return
#[::
Send #^{Left}
return
#^+r::Reload ; Assign Win-Ctrl-Shift-R as a hotkey to restart the script.
;; Windows explorer and Double Commander helpers ;;
;; Open selected file in GVim
$^j:: ; ctrl+j
{
If WinActive("ahk_class CabinetWClass") or WinActive("ahk_class DClass")
{
Clipboard =
Send ^c
ClipWait ;waits for the clipboard to have content
Run, C:\Program Files (x86)\Vim\vim81\gvim.exe `"%clipboard%`"
return
}
else
{
Send, ^j
return
}
}
;; Open selected file in GVim for viewing in Read only mode
$^k:: ; ctrl+k
{
If WinActive("ahk_class CabinetWClass") or WinActive("ahk_class DClass")
{
Clipboard =
Send ^c
ClipWait ;waits for the clipboard to have content
Run, C:\Program Files (x86)\Vim\vim81\gvim.exe -R `"%clipboard%`"
return
}
else
{
Send, ^k
return
}
}
;; This might be the problematic binding which screws up other bindings
;; Open selected file in Emacs via emacsclientw
; $^+e:: ; ctrl+shift+e
; {
; If WinActive("ahk_class CabinetWClass") or WinActive("ahk_class DClass")
; {
; Clipboard =
; Send ^c
; ClipWait ;waits for the clipboard to have content
; Run, C:\ProgramData\chocolatey\lib\emacs\tools\emacs\bin\emacsclientw.exe `"%clipboard%`"
; return
; }
; else
; {
; Send, ^+e
; return
; }
; }
;; Key Mapppings ;;
Capslock::Esc
;; Esc::Capslock
;; Press both Shift keys at the same time get Capslock
LShift & Esc::Capslock
; Text Snippets
#Include C:\Users\ChathuraColombage\Documents\TextSnippets.ahk
;; Fancy CapsLock mapping
;; Pressed on its own CapsLock acts as Esc
;; Otherwise it acts as Ctrl
; #Include C:\Users\ChathuraColombage\Documents\CapsLockCtrlEscape.ahk
nextWindow() {
;; Next Window
WinGetClass, ActiveClass, A
WinSet, Bottom,, A
WinActivate, ahk_class %ActiveClass%
}
bottomWindow() {
;; Last window
WinGetClass, ActiveClass, A
WinActivateBottom, ahk_class %ActiveClass%
}
!`::
bottomWindow()
return
^>!`::
bottomWindow()
return
!+`::
nextWindow()
return
^>+!`::
nextWindow()
return
; Clipboard copying is not reliable at all, hence the reason for disabling this
; ;; GoogleSearch or Show Link with CTRL+G
; googleSearchSelectedText() {
; prevClipboard := ClipboardAll
; SendInput, ^c
; ClipWait, 2 ;; wait 1 or 1
; if !(ErrorLevel) {
; Clipboard := RegExReplace(RegExReplace(Clipboard, "\r?\n"," "), "(^\s+|\s+$)")
; If SubStr(ClipBoard,1,7)="http://"
; Run, % Clipboard
; else
; Run, % "https://www.google.com/search?hl=en&q=" Clipboard
; }
; Clipboard := prevClipboard
; }
; #g::
; googleSearchSelectedText()
; return
;; Mouse Chording
#Include C:\Users\ChathuraColombage\Documents\MouseChording.ahk
@Konfekt
Copy link

Konfekt commented Nov 17, 2021

Thank you very much for this inspiring AHK config gist!

The various activate...() and the appFlapper() function could be consolidated to a single function definition RunOrRaise() by using an optional variable for the window ID detection, like so:

RunOrRaise(exePath, winID:="") {
  if (winID = "") {
    exeName := substr(exePath, instr(exePath, "\",, 0)+1)
    winID := "ahk_exe " exeName
  }
  If not WinExist(winID) {
	  EnvGet, UserProfile, USERPROFILE
    Run, %exePath%, %UserProfile%
    WinWait, % winID
    WinActivate
  }
  else {
    If WinExist(winID) {
      If WinActive() {
          WinWait, % winID
          WinMinimize
      }
      else {
        WinWait, % winID
        WinActivate
      }
    }
  }
}

#^v::RunOrRaise("gvim", "ahk_class Vim")
#^s::RunOrRaise("C:\Users\ChathuraColombage\AppData\Local\slack\slack.exe")

@dharmaturtle
Copy link

dharmaturtle commented Nov 21, 2021

You can also replace the hardcoded C:\Users\ChathuraColombage with the %userprofile% environment variable: https://docs.microsoft.com/en-us/windows/deployment/usmt/usmt-recognized-environment-variables

@Konfekt
Copy link

Konfekt commented Nov 23, 2021

By the way, here is a function to toggle a maximized window:

#m::toggleMaxWindow()
toggleMaxWindow() {
  WinGet, WinState, MinMax, A
  if (WinState = 1) {
    WinRestore, A
  }
  else {
    WinMaximize, A
  }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment