Skip to content

Instantly share code, notes, and snippets.

@nathanchan631
Created October 13, 2020 06:20
Show Gist options
  • Save nathanchan631/b01992e98d57df9e7e28d614bcef9e8d to your computer and use it in GitHub Desktop.
Save nathanchan631/b01992e98d57df9e7e28d614bcef9e8d to your computer and use it in GitHub Desktop.
Open Terminal with AutoHotKey
openWT(asAdmin:=false) {
; If Terminal is Already Open
if WinExist("ahk_exe WindowsTerminal.exe") {
WinActivate
Return
}
; If File Explorer is Active (Source: https://www.winhelponline.com/blog/open-command-prompt-current-folder-keyboard-shortcut)
if WinActive("ahk_class CabinetWClass") || WinActive("ahk_class ExploreWClass") {
WinHWND := WinActive()
For win, in ComObjCreate("Shell.Application").Windows {
if (win.HWND = WinHWND) {
dir := SubStr(win.LocationURL, 9) ; Remove "file:///"
dir := RegExReplace(dir, "%20", " ") ; Insert Spaces
Break
}
}
} else {
StringTrimRight, dir, A_Desktop, 8 ; Gets User Folder (ex. C:\Users:\<Username>)
}
if asAdmin {
Try {
Run *RunAs wt.exe, %dir%
} Catch {
MsgBox, Access Denied. Try Again.
Return
}
} else {
Run wt.exe, %dir%
}
; Make Terminal Window Active Once Opened
WinWait, ahk_exe WindowsTerminal.exe, , 3
if ErrorLevel
MsgBox, Couldn't Find Terminal
else
WinActivate
}
; Open Terminal on Windows Key + C
#c::openWT()
; Open Terminal as Admin On Windows Key + Shift + C
#+c::openWT(asAdmin:=true)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment