0

First of all my code:

#t::
IfWinActive, ahk_class CabinetWClass ; If explorer is active
{
    path := GetActiveExplorerPath()
    Run wt.exe -d "%path%"
}
else 
{
    Run wt.exe
}
Return

; https://stackoverflow.com/questions/39253268/autohotkey-and-windows-10-how-to-get-current-explorer-path 
GetActiveExplorerPath() {
    explorerHwnd := WinActive("ahk_class CabinetWClass")
    if (explorerHwnd)
    {
        for window in ComObjCreate("Shell.Application").Windows
        {
            if (window.hwnd==explorerHwnd)
                return window.Document.Folder.Self.Path
        }
    } 
}

Now if I press [Superkey] + [t] then the terminal opens as intended. However, a bug occurs if there is a space in the path variable, i.e. C:\Users\user\AHK Macros.

Fixing this should be easy (wrapping it in double quotes) and it works. The problem now is that opening the terminal at the C:\ directory results in an error. I think it has something to do with C:\ ending in a "\", thus escaping the quotation mark. Not sure though. I have tried wrapping it in 3/4 quotes. Printing it with MsgBox does give the desired "C:/" result and I am able to open C:/ if I leave out the quotation marks. What am I overlooking?

ckarakoc
  • 45
  • 4

1 Answers1

2

You would indeed need to escape the last backslash.
For example
Run, % "wt.exe -d ""C:\""" (in legacy syntax Run, wt.exe -d "C:\") is not fine
but
Run, % "wt.exe -d ""C:\\""" (in legacy syntax Run, wt.exe -d "C:\\") is fine.

You can for example check if the path ends in \ (should only happen in drive roots, I think) and then add an extra backslash:

#t::
    if (WinActive("ahk_exe Explorer.EXE ahk_class CabinetWClass"))
    {
        path := GetActiveExplorerPath()
        if (SubStr(path, 0) == "\") ;get last character
            path .= "\"
        path := StrReplace(path, ";", "\;") ;semicolon needs to be escaped
        Run, % "wt.exe -d """ path """"
    }
    else 
    {
        Run, % "wt.exe"
    }
return

And you'll notice that I also escaped ;, that needs to be done as well because it separates commands in wt launch parameters.

SubStr()(docs)
StrReplace()(docs)

0x464e
  • 5,948
  • 1
  • 12
  • 17