Skip to content

Instantly share code, notes, and snippets.

@leachdaniel
Last active November 25, 2017 18:38
Show Gist options
  • Save leachdaniel/4b9c9511bcb3ae4eb8efeac9f51dbf4b to your computer and use it in GitHub Desktop.
Save leachdaniel/4b9c9511bcb3ae4eb8efeac9f51dbf4b to your computer and use it in GitHub Desktop.
Toggle Or Run Windows in AHK
; This function is a lot like WinActivate, but allows you to pass a path to run if the window is not found
; It also uses WinActivateBottom if the matching window is already active, allowing you to toggle through windows
; Be careful with the winmatch, the case sensitivity includes ahk_class and ahk_exe because we are using RegEx match mode
; Tested with AHK Version 1.1.26.00
WinToggleOrRun(winmatch, pathOrCommand, excludeTitleRegex:=0)
{
; Every newly launched thread (such as a hotkey, custom menu item, or timed subroutine) starts off fresh with the default setting for this command.
SetTitleMatchMode, RegEx
IfWinExist, %winmatch%, , %excludeTitleRegex%
{
; if it's active, use WinActivateBottom causing the oldest to activate
IfWinActive, %winmatch%, , %excludeTitleRegex%
{
WinActivateBottom, %winmatch%, , %excludeTitleRegex%
}
else
{
WinActivate, %winmatch%, , %excludeTitleRegex%
}
}
else
{
Run, %pathOrCommand%
}
}
; Examples:
; Right Control c Starts Or Opens Visual Studio Code
>^c::WinToggleOrRun("ahk_exe i)code.exe", "code")
; Right Control d Starts Or Toggles through Visual Studio Instances
>^d::WinToggleOrRun("ahk_exe i)devenv.exe", "C:\Program Files (x86)\Microsoft Visual Studio\2017\Enterprise\Common7\IDE\devenv.exe")
; Right Control s Starts Or Toggles through SSMS Instances
>^s::WinToggleOrRun("ahk_exe i)ssms.exe", "C:\Program Files (x86)\Microsoft SQL Server\130\Tools\Binn\ManagementStudio\Ssms.exe")
; Right Control f Starts a file Explorer for d:\code or Through File Explorer Windows
>^f::WinToggleOrRun("ahk_class CabinetWClass", "d:\code\")
; Right Control \ Starts a powershell session or toggles through console sessions
>^\::WinToggleOrRun("ahk_class ConsoleWindowClass", "powershell -NoExit -ExecutionPolicy ByPass -Command ""Import-Module d:\code\Tools\tools.psm1""")
; Right control B Starts chrome or toggles through chrome browser sessions excluding some specific titles e.g. "Mail|Google Hangouts|Slack"
>^b::WinToggleOrRun("ahk_exe i)chrome.exe","C:\Program Files (x86)\Google\Chrome\Application\chrome.exe", excludeChromeTitlesRegex)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment