I need to convert this v1 script (which works well in v1) to its v2 counterpart:
I am going cross-eyed trying to get it to work. lol. If anyone can spare a second to give me some advice it would make my whole day! =)
This script will activate on hotkey press and then
1. if explorer window is the active window, capture the path and open WindowsTerminal.exe to the path in LINUX format. so for example. C:\Users\ would be /mnt/c/Users
2. If explorer is NOT the active window then open WindowsTerminal.exe to the users home folder AKA '~'
Here is the whole script below:
Code: Select all
!w::_OpenWSLHere()
_OpenWSLHere()
{
; Static vars so vars are not recreated each time
Static _convert := " !#$%&'()-.*:?@[]^_``{|}~/"
Static _osName := "Ubuntu-22.04"
If FileExist(A_ProgramFiles . "\PowerShell\7\pwsh.exe")
_myexe := A_ProgramFiles . "\PowerShell\7\pwsh.exe"
Else
_myexe := A_windir . "\System32\WindowsPowerShell\v1.0\powershell.exe"
If WinExist("ahk_class CabinetWClass ahk_exe explorer.exe")
_winHwnd := WinActive()
For win in ComObjCreate("Shell.Application").Windows
If (win.HWND = _winHwnd)
{
; Get the string
_pwd := SubStr(win.LocationURL, 9)
; Loop through the convert characters
Loop, Parse, % _convert
; Create a %hex token using ord value of convert chars
_hex := "%" Format("{1:X}", Ord(A_LoopField))
; Replace any hex tokens with their actual chars
,_pwd := StrReplace(_pwd, _hex, A_LoopField)
; Single quotes must be doubled for the command line below to work properly
_pwd := StrReplace(_pwd, "'", "''")
}
; Converted both run commands to expression format
If (_pwd = "")
Run, %ComSpec% /D /C START "" "%_myexe%" -NoP -W Hidden -C "Start-Process wt.exe -Args '-w new-tab -M -d \"~\" wsl.exe -d \"%_osName%\"' -Verb RunAs",, Hide, _wPID
Else
Run, %ComSpec% /D /C START "" "%_myexe%" -NoP -W Hidden -C "Start-Process wt.exe -Args '-w new-tab -M -d \"%_pwd%\" wsl.exe -d \"%_osName%\"' -Verb RunAs",, Hide, _wPID
WinActivate, ahk_exe WindowsTerminal.exe
}