pplupo wrote: 2023-02-01, 03:52 UTC
I wrote one today, unaware of this thread. Then I came here to share it but decided to search for one first. Ended up sticking with mine, though. Why?
Because if you hit Win+E while on a Windows Explorer window, it closes the Windows Explorer window and opens TC with the same directory open on a tab.
Here goes the code:
Code: Select all
#NoTrayIcon
#NoEnv ; Recommended for performance and compatibility with future AutoHotkey releases.
; #Warn ; Enable warnings to assist with detecting common errors.
#SingleInstance Force
SendMode Input ; Recommended for new scripts due to its superior speed and reliability.
SetWorkingDir %A_ScriptDir% ; Ensures a consistent starting directory.
#e::
; Windows 7
#IfWinActive ahk_class ExploreWClass
; Windows 10
#IfWinActive ahk_class CabinetWClass
; Alt+E open current Explorer in Total Commander
WinGet, winid
Path := uriDecode(WindowsExplorerLocationByCOM(%winid%))
Run, "C:\totalcmd\TOTALCMD64.EXE" /O /T "%Path%"
if Path {
WinClose, %winid%
}
Return
;[Detect current windows explorer location - Ask for Help - AutoHotkey Community](https://www.autohotkey.com/board/topic/70960-detect-current-windows-explorer-location/)
WindowsExplorerLocationByCOM(HWnd="")
{
If ( HWnd = "" )
HWnd := WinExist("A")
WinGet Process, ProcessName, ahk_id %HWnd%
If ( Process = "explorer.exe" )
{
WinGetClass Class, ahk_id %HWnd%
If ( Class ~= "Progman|WorkerW" )
Location := A_Desktop
Else If ( Class ~= "(Cabinet|Explore)WClass" )
{
For Window In ComObjCreate("Shell.Application").Windows
If ( Window.HWnd == HWnd )
{
URL := Window.LocationURL
Break
}
StringTrimLeft, Location, URL, 8 ; remove "file:///"
StringReplace Location, Location, /, \, All
}
}
Return Location
}
;[Great AutoHotkey script to URL Encode/Decode and parse URL parameters : AutoHotkey](https://www.reddit.com/r/AutoHotkey/comments/4py9e7/great_autohotkey_script_to_url_encodedecode_and/)
;***********https://autohotkey.com/board/topic/17367-url-encoding-and-decoding-of-special-characters/*******************
uriDecode(str) {
Loop
If RegExMatch(str, "i)(?<=%)[\da-f]{1,2}", hex)
StringReplace, str, str, `%%hex%, % Chr("0x" . hex), All
Else Break
Return, str
}
I've built upon this logic & updated for v2
First, we need to schedule a task in windows task scheduler so that the script is available on launch:
- Click Task Scheduler (Local) > On the right click Create Task
- Tick Run when user is logged on
- [Optional] [Required for startup_task.ahk to run properly] Tick Run with Highest permissions
- Navigate to actions tab > add startup_task.ahk
- Navigate to triggers add new > click dropdown box > at logon > for all users or your own
startup_task.ahk:
Code: Select all
; #NoTrayIcon ; personal preference. You can enable this if you wanna but I like knowing what's running
Persistent true
#SingleInstance Force
TrayTip("The script is now active.","Script Running")
#ErrorStdOut
DetectHiddenWindows True
#Include src/ids.ahk ; Variables, Constants, ids etc
#Include src/general_functions.ahk ; general functions / helper functions etc
#Include src/win_e_key_for_tc.ahk ; Now you can add as many specialised functions as you want
#e::WindowsKeyForTC(TC_PATH) ; TC_PATH in ids.ahk
^!+9::ExitApp
^!+8::Reload
^!+7::OpenScriptLocationInTC(TC_PATH)
^!+6:: MsgBox get_ahk_class_of_currently_active_window()
^!+5::OpenWindowSpy(WINSPY_PATH)
src/ids.ahk is self explanatory. Just add your variables, IE TC_PATH := "C:\PATH\TO\TC.EXE"
src/general_functions.ahk:
Code: Select all
; Open the running script in TC
OpenScriptLocationInTC(path) {
TC_EXE := GetFileNameFromPath(path)
scriptDir := SubStr(A_ScriptDir, 1)
Run(path . " /O /T " . scriptDir)
WinWait("ahk_exe " . TC_EXE)
WinActivate("ahk_exe " . TC_EXE)
}
OpenWindowSpy(path) {
Run(path)
}
get_ahk_class_of_currently_active_window()
{
; Get the class of the active window
activeClass := WinGetClass("A")
return activeClass
}
GetFileNameFromPath(path) {
SplitPath(path, &name)
return name
}
finally your specialised functions:
src:/win_e_key_for_tc.ahk
Code: Select all
; For this to work as intended, set MinimizeOnClose=3, TrayIcon=1 and onlyonce=1 in the wincmd.ini
WindowsExplorerLocationByCOM(HWnd := "") {
if (HWnd = "") {
HWnd := WinExist("A")
}
Process := WinGetProcessName("ahk_id " . HWnd)
if (Process = "explorer.exe") {
Class := WinGetClass("ahk_id " . HWnd)
if (Class ~= "Progman|WorkerW") {
Location := A_Desktop
} else if (Class ~= "(Cabinet|Explore)WClass") {
shellApp := ComObject("Shell.Application")
for Window in shellApp.Windows() {
if (Window.HWnd = HWnd) {
URL := Window.LocationURL
break
}
}
Location := StrReplace(StrReplace(URL, "file:///", ""), "/", "\")
}
}
return Location
}
uriDecode(str) {
Loop {
match := RegExMatch(str, "i)(?<=%)[\da-f]{1,2}")
if (match) {
hex := match.Value
str := StrReplace(str, "%" . hex, Chr("0x" . hex))
} else {
break
}
}
return str
}
WindowsKeyForTC(path) {
{
TC_EXE := GetFileNameFromPath(path)
; If valid explorer window is open: close it and open TC at that location
if (WinActive("ahk_class ExploreWClass") or WinActive("ahk_class CabinetWClass"))
{
winid := WinGetID("A")
Path := uriDecode(WindowsExplorerLocationByCOM(winid))
if (Path != "") {
Run(path . " /O /T " . Path)
; WinWait("ahk_exe " . TC_EXE); This causes laggy artefacts, left as comment to explain
WinActivate("ahk_exe " . TC_EXE)
WinClose("ahk_id " . winid)
} else { ; Handles cases like "This PC" etc
Run(path)
WinActivate("ahk_exe " . TC_EXE)
WinClose("ahk_id " . winid)
}
}
; If TC is currently active: minimize it
else if (WinGetProcessName("A") == TC_EXE)
{
winid := WinGetID("A")
WinMinimize("ahk_id " . winid)
}
; Otherwise just open TC :)
else
{
Run(path)
WinActivate("ahk_exe " . TC_EXE)
}
}
}
Hope this helps some of you
