Struggling with comobjects and convertion of v1 script to v2

Get help with using AutoHotkey (v2 or newer) and its commands and hotkeys
User avatar
slyfox1186
Posts: 52
Joined: 28 Oct 2018, 23:56

Struggling with comobjects and convertion of v1 script to v2

Post by slyfox1186 » 20 Mar 2023, 18:23

I am just plain struggling with this...

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
}

User avatar
mikeyww
Posts: 26602
Joined: 09 Sep 2014, 18:38

Re: Struggling with quotes on a long expression variable

Post by mikeyww » 20 Mar 2023, 19:17

Hello,

You might not like my answer, because I would like to point you to a couple of representative examples in the documentation.

First is WinActivate.

https://www.autohotkey.com/docs/v2/lib/WinActivate.htm#ExBasic

Second is Run.

https://www.autohotkey.com/docs/v2/lib/Run.htm#ExVerb

A common feature is the use of expressions for these functions. The expression here is showing quoted text. Unquoted text does not work if it is actually a literal string. Variables would be unquoted and can be inserted between quoted strings (i.e., concatenated)-- without the flanking %-- if needed.

Just as with all v2 functions, every parameter is an expression, not just the first parameter.

As noted in these examples, AHK v2 omits commas that immediately follow commands in v1.

Finally, there is a simple way to debug a Run line in v2. Replace Run with MsgBox, and then run a short script with that function call for MsgBox. You will then see the entire command line displayed in the message box, and you will be able to spot the problems immediately.

Code: Select all

#Requires AutoHotkey v2.0
myexe   := 123
_pwd    := 456
_osName := 789
MsgBox A_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'
I'm not saying that this works, but providing it as an example of how to use MsgBox to troubleshoot the Run line.

If your command line needs to use ", then you can use the single quotation mark ' to denote your strings, as shown in my example.

As noted in the changelog, ComObjCreate became ComObject in v2.

User avatar
slyfox1186
Posts: 52
Joined: 28 Oct 2018, 23:56

Re: Struggling with comobjects and convertion of v1 script to v2

Post by slyfox1186 » 20 Mar 2023, 20:44

I have this 95% working.

I am believe it or not now struggling on vetting the _pwd variable. It will either be blank or not blank.

What am I doing wrong? I am trying to use the StrLen expression and the run an If command against the results.

here is what I have now.

Code: Select all

!w::_OpenWSLHere()

_OpenWSLHere()
{
    ; Static vars so vars are not recreated each time
    Static _convert := " !#$%&'()-.*:?@[]^_``{|}~/"
    Static _osName := "Ubuntu-22.04"
    Static _wt := "C:\Users\jholl\AppData\Local\Microsoft\WindowsApps\wt.exe"
    Static _wsl := "C:\Windows\System32\wsl.exe"

    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()
        winObj := ComObject("Shell.Application").Windows
    For win in winObj
    {
            ; 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("{: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, "%", "")
                _pwd := StrReplace(_pwd, "'", "''")
     }

    ; Converted both run commands to expression format
    Length := StrLen(_pwd)
    If (Length < 0)
        MsgBox A_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'
    Else
        MsgBox A_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'
}
 

User avatar
slyfox1186
Posts: 52
Joined: 28 Oct 2018, 23:56

Re: Struggling with comobjects and convertion of v1 script to v2

Post by slyfox1186 » 20 Mar 2023, 20:45

slyfox1186 wrote: I have this 95% working.

I am believe it or not now struggling on vetting the _pwd variable. It will either be blank or not blank.

What am I doing wrong? I am trying to use the StrLen expression and the run an If command against the results.

here is what I have now.

Code: Select all

!w::_OpenWSLHere()

_OpenWSLHere()
{
    ; Static vars so vars are not recreated each time
    Static _convert := " !#$%&'()-.*:?@[]^_``{|}~/"
    Static _osName := "Ubuntu-22.04"
    Static _wt := "C:\Users\jholl\AppData\Local\Microsoft\WindowsApps\wt.exe"
    Static _wsl := "C:\Windows\System32\wsl.exe"

    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()
        winObj := ComObject("Shell.Application").Windows
    For win in winObj
    {
            ; 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("{: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, "%", "")
                _pwd := StrReplace(_pwd, "'", "''")
     }

    ; Converted both run commands to expression format
    Length := StrLen(_pwd)
    If (Length < 0)
        MsgBox A_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'
    Else
        MsgBox A_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'
}
 
I have this 95% working.

I am believe it or not now struggling on vetting the _pwd variable. It will either be blank or not blank.

What am I doing wrong? I am trying to use the StrLen expression and the run an If command against the results.

here is what I have now.

Code: Select all

!w::_OpenWSLHere()

_OpenWSLHere()
{
    ; Static vars so vars are not recreated each time
    Static _convert := " !#$%&'()-.*:?@[]^_``{|}~/"
    Static _osName := "Ubuntu-22.04"
    Static _wt := "C:\Users\jholl\AppData\Local\Microsoft\WindowsApps\wt.exe"
    Static _wsl := "C:\Windows\System32\wsl.exe"

    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()
        winObj := ComObject("Shell.Application").Windows
    For win in winObj
    {
            ; 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("{: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, "%", "")
                _pwd := StrReplace(_pwd, "'", "''")
     }

    ; Converted both run commands to expression format
    Length := StrLen(_pwd)
    If (Length > 0)
        MsgBox A_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'
    Else
        MsgBox A_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'
}

User avatar
mikeyww
Posts: 26602
Joined: 09 Sep 2014, 18:38

Re: Struggling with comobjects and convertion of v1 script to v2

Post by mikeyww » 20 Mar 2023, 20:47

Use a block. An example of a block is found on line 20 of your last script.

I'm not sure, but your indentation is suggesting that this is your intent.

You can debug your script by determining the value of each variable at each step in your script. You can display it or use one of AHK's debugging tools.

Post Reply

Return to “Ask for Help (v2)”