Open any Linux WSL Distro to the active explorer.exe's path in Windows Terminal

Post your working scripts, libraries and tools.
User avatar
slyfox1186
Posts: 53
Joined: 28 Oct 2018, 23:56

Open any Linux WSL Distro to the active explorer.exe's path in Windows Terminal

07 Apr 2024, 17:15

This script will do the following:

If a hotkey is activated on an active explorer.exe window it will open the Linux version the hotkey corresponds to in the Windows Terminal to the path in the active explorer window.
If a hotkey is activated on any other window besides explorer.exe it will open the Linux version the hotkey corresponds to in the user's $HOME (~) directory.

You can open the following Linux Distros:
  • Ubuntu
  • Debian
  • Arch Linux
To execute run the script and open the downloads folder in explorer.exe and press a hotkey of your choosing for the Linux Distro you want to open to that folder path in Windows Terminal.

https://github.com/slyfox1186/script-repo/blob/main/AHK-v2/OpenWSLHere.ahk

Code: Select all

/*____________________________________________________________________________________
    OpenWSLHere.ahk

    GitHub:
    - https://github.com/slyfox1186/script-repo/blob/main/AHK-v2/OpenWSLHere.ahk

    Pastebin:
    - https://pastebin.com/u/slyfox1186

    Purpose:
    - This will open Windows' WSL terminal to the active file explorer folder or if no active explorer window is found, ~

    Instructions:
    - You need to replace the below variable 'osName' with the wsl distribution of your choosing.
    - To find the available distros run 'wsl.exe -l --all' using PowerShell to get a list of available options

    Updated:
    - 04.07.24

    Big Update:
        Combined all major versions of Windows WSL OS's into a single function greatly reducing
        the size of the overall code, and adding the ability to choose the OS you wish to use.

*/

!w Up::OpenWSLHere("Debian")
^!w Up::OpenWSLHere("Ubuntu")
^!+w Up::OpenWSLHere("Arch")

OpenWSLHere(osName) {
    static wt := "C:\Users\" A_UserName "\AppData\Local\Microsoft\WindowsApps\wt.exe"
    static wsl := "C:\Windows\System32\wsl.exe"
    static win := "ahk_class CASCADIA_HOSTING_WINDOW_CLASS ahk_exe WindowsTerminal.exe"
    static pshell := FileExist("C:\Program Files\PowerShell\7\pwsh.exe") ? "C:\Program Files\PowerShell\7\pwsh.exe" : "C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe"

    if !WinActive("ahk_class CabinetWClass ahk_exe explorer.exe") {
        Run(pshell ' -NoP -W Hidden -C "Start-Process -WindowStyle Max ' . wt . ' -Args `'-w new-tab ' . wsl . ' -d ' . osName . ' --cd ~ `' -Verb RunAs"',, "Hide")
        if WinWait(win,, 2)
            WinActivate
        return
    }

    hwnd := WinExist("A")
    winObj := ComObject("Shell.Application").Windows
    try activeTab := ControlGetHwnd("ShellTabWindowClass1", hwnd)

    winObj := ComObject("Shell.Application").Windows
    for win in winObj {
        if (win.hwnd = hwnd) {
            if (activeTab) {
                shellBrowser := ComObjQuery(win, "{4C96BE40-915C-11CF-99D3-00AA004AE837}", "{000214E2-0000-0000-C000-000000000046}")
                if !shellBrowser
                    continue
                ComCall(3, shellBrowser, "uint*", &currentTab:=0)
                if (currentTab != activeTab)
                    continue
            }
            pwd := win.Document.Folder.Self.Path
            pwd := StrReplace(pwd, "'", "''")
            pwd := StrReplace(pwd, "\\wsl.localhost", "")
            pwd := RegExReplace(pwd, "\\(Arch|Debian|Ubuntu)", "/")
            pwd := StrReplace(pwd, "\", "/")
            break
        }
    }

    Run(pshell ' -NoP -W Hidden -C "Start-Process -WindowStyle Max ' . wt . ' -Args `'-w new-tab ' . wsl . ' -d ' . osName . ' --cd \"' . pwd . '\" `' -Verb RunAs"',, "Hide")
    if WinWait(win,, 2)
        WinActivate
}
Comments, suggestions, improvements, ideas are what I want most. I love learning better and more efficient ways of doing things. Let me know if any of you try this out and let me know if you think it was a good idea!

Return to “Scripts and Functions (v2)”

Who is online

Users browsing this forum: burque505, Descolada, metallizer and 50 guests