mmikeww / AHK-v2-script-converter

AHK v1 -> v2 script converter
https://autohotkey.com/boards/viewtopic.php?f=6&t=25100
The Unlicense
511 stars 39 forks source link

Definition of a variable from function not correctly converted #172

Open user1823 opened 3 months ago

user1823 commented 3 months ago

v1 code:

#g::
GetText(Temp)
If NOT ERRORLEVEL
{
    ; Modify some characters that screw up the URL
    ; RFC 3986 section 2.2 Reserved Characters (January 2005):  !*'();:@&=+$,/?#[]
    StringReplace, Temp, Temp, `r`n, %A_Space%, All
    StringReplace, Temp, Temp, #, `%23, All
    StringReplace, Temp, Temp, &, `%26, All
    StringReplace, Temp, Temp, +, `%2b, All
    StringReplace, Temp, Temp, ", `%22, All

    Run, msedge.exe -inprivate "https://www.google.com/search?q=%Temp%"
}
Return

GetText(ByRef MyText = "")
{
   SavedClip := ClipboardAll ; Store full version of Clipboard
   Clipboard = ; Empty the clipboard
   Send ^c
   ClipWait 0.5   ; Waits until the clipboard contains data with a timeout of 0.5 s
   If ERRORLEVEL
   {
      Clipboard := SavedClip ; Restore Clipboard
      MyText =
      MsgBox, No text was sent to clipboard
      Return
   }
   MyText := Clipboard
   Sleep 10
   Clipboard := SavedClip
   Return MyText
}

Output:

#g::
{ ; V1toV2: Added bracket
GetText(Temp)
    ; Modify some characters that screw up the URL
    ; RFC 3986 section 2.2 Reserved Characters (January 2005):  !*'();:@&=+$,/?#[]
    Temp := StrReplace(Temp, "`r`n", A_Space)
    Temp := StrReplace(Temp, "#", "`%23")
    Temp := StrReplace(Temp, "&", "`%26")
    Temp := StrReplace(Temp, "+", "`%2b")
    Temp := StrReplace(Temp, "`"", "`%22")

    Run("msedge.exe -inprivate `"https://www.google.com/search?q=" Temp "`"")
Return
} ; V1toV2: Added Bracket before function

GetText(&MyText := "")
{
   SavedClip := ClipboardAll() ; Store full version of Clipboard
   A_Clipboard := "" ; Empty the clipboard
   Send("^c")
   Errorlevel := !ClipWait(0.5)   ; Waits until the clipboard contains data with a timeout of 0.5 s
   If ERRORLEVEL
   {
      A_Clipboard := SavedClip ; Restore Clipboard
      MyText := ""
      MsgBox("No text was sent to clipboard")
      Return
   }
   MyText := A_Clipboard
   Sleep(10)
   A_Clipboard := SavedClip
   Return MyText
}

This gives the following error when the hotkey is pressed.

Error: This variable has not been assigned a value.

Specifically: local Temp

    267: }
    273: {
▶   274: GetText(Temp)
    277: Temp := StrReplace(Temp, "
", A_Space)
    278: Temp := StrReplace(Temp, "#", "%23")

To fix this, GetText(Temp) should be replaced with Temp := GetText().

Banaanae commented 2 months ago

Correct conversion would be GetText(&Temp), your solution works because the GetText func has Return MyText despite MyText being a ByRef