-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathauto-term-anywhere.ahk
75 lines (66 loc) · 2.15 KB
/
auto-term-anywhere.ahk
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
; TO-DO: Network locations create issues
#NoEnv ; Recommended for performance and compatibility with future AutoHotkey releases.
; #Warn ; Enable warnings to assist with detecting common errors.
SendMode Input ; Recommended for new scripts due to its superior speed and reliability.
#SingleInstance, Force ; if an instance of the script is already running, replace it with this one
GetExplorerPath()
{
WinGetActiveTitle, activeTitle
explorerHwnd := WinActive("ahk_class CabinetWClass")
if (explorerHwnd)
{
for window in ComObjCreate("Shell.Application").Windows{
try
{
if (window && window.hwnd && window.hwnd == explorerHwnd && activeTitle == window.LocationName) {
pathwintmp := window.LocationURL
pathwin := RegExReplace(pathwintmp, "^file:/{2,}") ; strip the file protocol from the URL
pathwin := RegExReplace(pathwin, "%20", " ") ; replace %20 strings with spaces
return pathwin
}
}
}
}
return false
}
SwitchToWindowsTerminal()
{
windowHandleId := WinExist("ahk_exe WindowsTerminal.exe")
windowExistsAlready := windowHandleId > 0
; If the Windows Terminal is already open, determine if we should put it in focus or minimize it.
if (windowExistsAlready = true)
{
activeWindowHandleId := WinExist("A")
windowIsAlreadyActive := activeWindowHandleId == windowHandleId
if (windowIsAlreadyActive)
{
; Minimize the window.
WinMinimize, "ahk_id %windowHandleId%"
}
else
{
; Put the window in focus.
WinActivate, "ahk_id %windowHandleId%"
WinShow, "ahk_id %windowHandleId%"
}
}
; Else it's not already open, so launch it.
else
{
explorerIsOpen := GetExplorerPath()
if (explorerIsOpen)
{
Run, wt -d "%explorerIsOpen%"
}
else
{
Run, wt
}
WinWait, ahk_exe WindowsTerminal.exe,,3
WinWaitNotActive, ahk_exe WindowsTerminal.exe,,5
if ErrorLevel = 0 ; i.e. it's not blank or zero.
WinActivate
}
}
; Hotkey to use Win+C to launch/restore the Windows Terminal.
#!c::SwitchToWindowsTerminal()