v1 -> v2 Script Converter

Post your working scripts, libraries and tools.
guest3456
Posts: 3477
Joined: 09 Oct 2013, 10:31

Re: v1 -> v2 Script Converter

Post by guest3456 » 15 Mar 2022, 10:32

joomb wrote:
15 Mar 2022, 02:16
swagfag wrote:
14 Mar 2022, 12:13

Code: Select all

BytesRead_ += NumGet(BytesRead, A_Index-1, 'UChar') << 8*(A_Index-1) ;Bytes read in this very DllCall
if !p:=RegExMatch(j,"(?<!\\)(`"`"|')([^\1]+?)(?<!\\)(?-1)\s*:\s*((\{(?:[^{}]++|(?-1))*\})|(\[(?:[^[\]]++|(?-1))*\])|" . "(?<!\\)(`"`"|')[^\7]*?(?<!\\)(?-1)|[+\-]?\d+(?:\.\d*)?|true|false|null?)\s*(?:,|$|\})", &x, p)
thanks very much but it still can not work .
I will post the all codes v1 here for help

Code: Select all

json(Byref js, s, v:="") {  ;v2版本需要将Byref替换为&
	j := js 
	Loop Parse, s, % chr(46) ;V2版本将%去掉
	{
		p := "2"
		RegExMatch(A_LoopField, "([+\-]?)([^[]+)((?:\[\d+\])*)", q)
		Loop {
			If (!p := RegExMatch(j, "(?<!\\)(""|')([^\1]+?)(?<!\\)(?-1)\s*:\s*((\{(?:[^{}]++|(?-1))*\})|(\[(?:[^[\]]++|(?-1))*\])|" "(?<!\\)(""|')[^\7]*?(?<!\\)(?-1)|[+\-]?\d+(?:\.\d*)?|true|false|null?)\s*(?:,|$|\})", x, p))
				Return
			Else If (x2 == q2 or q2 == "*") {
				j := x3
				z += p + StrLen(x2) - 2
				If (q3 != "" and InStr(j, "[") == 1) {
					q3 := SubStr(q3, 1, -1)
					Loop Parse, q3, "]", "["
					{
						z += 1 + RegExMatch(SubStr(j, 2, -1), "^(?:\s*((\[(?:[^[\]]++|(?-1))*\])|(\{(?:[^{\}]++|(?-1))*\})|[^,]*?)\s*(?:,|$)){" . SubStr(A_LoopField, 1) + 1 . "}", x)
						j := x1
					}
				}
				Break
			}
			Else p += StrLen(x)
		}
	}
	if (v != "")
	{
		vs := chr(34)
		If (RegExMatch(v, "^\s*(?:""|')*\s*([+\-]?\d+(?:\.\d*)?|true|false|null?)\s*(?:""|')*\s*$", vx)
			and (vx1 + 0 or vx1 == 0 or vx1 == "true" or vx1 == "false" or vx1 == "null" or vx1 == "nul"))
			vs := "", v := vx1
		v := StrReplace(v, CHR(34), "\" . CHR(34))
		js := SubStr(js, 1, z := RegExMatch(js, ":\s*", zx, z) + StrLen(zx) - 1) . vs . v . vs . SubStr(js, z + StrLen(x3) + 1)
	}
	Return j == "false" ? 0 : j == "true" ? 1 : j == "null" or j == "nul"
		? "" : SubStr(j, 1, 1) == """" ? SubStr(j, 2, -1) : j
}
in v2, RegExMatch now outputs an object
so, in addition to changing the x to &x in all the RegExMatch calls like swagfag showed you, you also need to change all the x2 to x[2]. and same for the q and vx output variables, etc etc.

it is not practical for the converter to automagically handle all of this, but it tries its best to do some

for example:

ahk1:
https://github.com/mmikeww/AHK-v2-script-converter/blob/master/tests/Test_Folder/String/RegExMatch/RegExMatch_Normal-mode.ah1

ahk2:
https://github.com/mmikeww/AHK-v2-script-converter/blob/master/tests/Test_Folder/String/RegExMatch/RegExMatch_Normal-mode.ah2


AHK_user
Posts: 523
Joined: 04 Dec 2015, 14:52
Location: Belgium

Re: v1 -> v2 Script Converter

Post by AHK_user » 16 Jul 2022, 17:24

Update: Correction on the sendmessage command.

This seemed to be missing, strange...

User avatar
jNizM
Posts: 3201
Joined: 30 Sep 2013, 01:33
Contact:

Re: v1 -> v2 Script Converter

Post by jNizM » 09 Dec 2022, 02:54

In case it has not been reported yet.
Once an array is translated, it makes an array out of each word/command if the first letter matches the array.

Image
[AHK] v2.0.18 | [WIN] 11 Pro (23H2) | [GitHub] Profile

safetycar
Posts: 435
Joined: 12 Aug 2017, 04:27

Re: v1 -> v2 Script Converter

Post by safetycar » 09 Dec 2022, 07:34

For the main devs, I'll submit a PR about that later.

guest3456
Posts: 3477
Joined: 09 Oct 2013, 10:31

Re: v1 -> v2 Script Converter

Post by guest3456 » 09 Dec 2022, 12:24

jNizM wrote:
09 Dec 2022, 02:54
In case it has not been reported yet.
Once an array is translated, it makes an array out of each word/command if the first letter matches the array.
safetycar wrote:
09 Dec 2022, 07:34
For the main devs, I'll submit a PR about that later.
i merged your PR


MrDoge
Posts: 178
Joined: 27 Apr 2020, 21:29

Re: v1 -> v2 Script Converter

Post by MrDoge » 09 Dec 2022, 15:20

v1

Code: Select all

r4:=125
; r = 1,2,3
r = 1,2,3,4
StringSplit r, r, `,
MsgBox % r
MsgBox % r2
MsgBox % r0

Loop, %r0%
{
    this_color := r%A_Index%
    MsgBox, Color number %A_Index% is %this_color%.
}

myfunc(randomwordwithr) {
	return randomwordwithr
}
v2

Code: Select all

r4 := 125
r := "1,2,3,4"
r := StrSplit(r, ",")
MsgBox r
MsgBox r[2]
MsgBox r.Length
loop r.Length {
  this_color := r[A_Index]
  MsgBox "Color number " A_Index " is " this_color "."
}
myfunc(randomwordwithr) {
  return randomwordwithr
}
I only compete, fix bugs on other repos,
I say this because I haven't done work on mine, I only do work when I see bugs here
commit: StringSplit.ahk, https://github.com/FuPeiJiang/ahk_to_v2.ah2/commits/main
___

this is impossible to convert:
v1

Code: Select all

r4:=125
; r = 1,2,3
r = 1,2,3,4
StringSplit r, r, `,
MsgBox % r4
v2

Code: Select all

r4 := 125
r := "1,2,3,4"
r := StrSplit(r, ",")
MsgBox % (r.Length < 4 ? r4 : r[4])
so I don't convert it

___

when it was a website, I had so many github issues, now there are none
I think I need a GUI, or it's because website is "no download" and "autoupdate"
because it's written in ahkv2 and I don't want to translate to javascript
I'm gonna "borrow" this GUI QuickConvertorV2_scintilla.ahk

https://github.com/FuPeiJiang/ahk_to_v2.ah2#readme
about the wording... I thought it could be interpreted as pretty offensive so I added quotes to "stolen"

User avatar
jNizM
Posts: 3201
Joined: 30 Sep 2013, 01:33
Contact:

Re: v1 -> v2 Script Converter

Post by jNizM » 13 Dec 2022, 05:24

Still broken
v1

Code: Select all

class randomclass
{
	; stuff

	something(x)
	{
		loop % x
			if (RegExMatch(a_loopField, "^[cC]([a-fA-F\d]+)$", m))
				color := "0x" m1
		return color
	}

	measure(g, text := "")
	{
		DllCall("gdiplus\GdipDrawLine", "UPtr", g, "UPtr", pen
				, "Float", this.x + this.marginLeft + w
				, "Float", this.y + this.marginTop
				, "Float", this.x + this.marginLeft + w
				, "Float", this.bottom - this.marginBottom)
	}

	; more stuff
}
converted to v2:

Code: Select all

class randomclass
{
	; stuff

	something(x)
	{
		Loop x
			if (RegExMatch(a_loopField, "^[cC]([a-fA-F\d]+)$", &m))
				color := "0x" m[1]
		return color
	}

	m["easure"](g, text := "")
	{
		DllCall("gdiplus\GdipDrawLine", "UPtr", g, "UPtr", pen, "Float", this.x + this.m["arginLeft"] + w, "Float", this.y + this.m["arginTop"], "Float", this.x + this.m["arginLeft"] + w, "Float", this.bottom - this.m["arginBottom"])
	}

	; more stuff
}
[AHK] v2.0.18 | [WIN] 11 Pro (23H2) | [GitHub] Profile

AHK_user
Posts: 523
Joined: 04 Dec 2015, 14:52
Location: Belgium

Re: v1 -> v2 Script Converter

Post by AHK_user » 13 Dec 2022, 06:39

jNizM wrote:
13 Dec 2022, 05:24
Still broken
v1

Code: Select all

class randomclass
{
	; stuff

	something(x)
	{
		loop % x
			if (RegExMatch(a_loopField, "^[cC]([a-fA-F\d]+)$", m))
				color := "0x" m1
		return color
	}

	measure(g, text := "")
	{
		DllCall("gdiplus\GdipDrawLine", "UPtr", g, "UPtr", pen
				, "Float", this.x + this.marginLeft + w
				, "Float", this.y + this.marginTop
				, "Float", this.x + this.marginLeft + w
				, "Float", this.bottom - this.marginBottom)
	}

	; more stuff
}
converted to v2:

Code: Select all

class randomclass
{
	; stuff

	something(x)
	{
		Loop x
			if (RegExMatch(a_loopField, "^[cC]([a-fA-F\d]+)$", &m))
				color := "0x" m[1]
		return color
	}

	m["easure"](g, text := "")
	{
		DllCall("gdiplus\GdipDrawLine", "UPtr", g, "UPtr", pen, "Float", this.x + this.m["arginLeft"] + w, "Float", this.y + this.m["arginTop"], "Float", this.x + this.m["arginLeft"] + w, "Float", this.bottom - this.m["arginBottom"])
	}

	; more stuff
}
If I find time, I will improve it so it is not that fragile. It stays a hard thing to convert.
For now I would advice to just make your variables longer...

Feel free to propose a solution on GitHub.

User avatar
DrReflex
Posts: 42
Joined: 25 May 2015, 02:57
Location: Greer, SC

Re: v1 -> v2 Script Converter

Post by DrReflex » 24 Feb 2023, 03:54

Very helpful. I hope the community with work with guest3456 to improve the conversion process.
Here is my first input. An update for the v2converter.ahk script.

Code: Select all

{ ;FILE_NAME  My_v2converter.ahk - v2 -
; Language:       English
; Platform:       Windows11
; Author:         guest3456
; Update to v2converter.ahk (DrReflex)
; Script Function: AHK version 1 to version 2 converter
;
; Use:
;       Run the script.  Accepts arguments
;         Formats:  1. My_v2converter.ahk with parameter 1 set to "MyV1ScriptFullPath.ahk" (quotes required)
;                   2. My_v2conterter.ahk with parameter 1 set to
;       Chose the file you want to convert in the file select dialog
;       A msgbox will popup telling you the script finished converting
;       If you gave the file MyScript.ahk, the output file will be MyScript_v2new.ahk
;
; Uses format.ahk
; =============================================================================
; OTHER THINGS TO ADD:
;
}
{ ;REFERENCES:
  ; Feel free to add to this program.  Post changes here: http://www.autohotkey.com/forum/viewtopic.php?t=70266
}
{ ;DIRECTIVES AND SETTINGS
   #Requires AutoHotkey >=2.0-<2.1  ; Requires AHK v2 to run this script
   #SingleInstance Force			      ; Recommended so only one copy is runnnig at a time
   SendMode "Input"  				        ; Recommended for new scripts due to its superior speed and reliability.
   SetWorkingDir A_ScriptDir  	    ; Ensures a consistent starting directory.
}
{ ;CLASSES:
}
{ ;VARIABLES:
   global dbg:=0
}
{ ;INCLUDES:
   #Include lib/ClassOrderedMap.ahk
   #Include Convert/1Commands.ahk
   #Include Convert/2Functions.ahk
   #Include Convert/3Methods.ahk
   #Include Convert/4ArrayMethods.ahk
   #Include Convert/5Keywords.ahk
}
{ ;MAIN PROGRAM - BEGINS HERE *****************************************************************************************
;   Many changes can be made here to affect loading and processing
; =============================================================================
   MyOutExt  := "_newV2.ahk"    ;***ADDED OUTPUT EXTENSION OPTION***
   ;MyOutExt := ".c2v2.ahk"     ;***THIS IS THE OUTPUT EXTENSION OPTION THAT I USE***
   ;NOTES: 1. When I have a coverted 2 version 2 file ("FILENAME.c2v2.ahk") working, 
   ;          I change the name to "FILENAME.v2.ahk".  
   ;       2. I use the format "FILENAME.v2.ahk" for all v2 scripts and "FILNAME.v1.ahk" for all v1.1 scripts.  
   ;          This lets me distinguish the files at a glance.  
   ;       3. When I batch inserted #Requires AutoHotkey 64-bit into my v1.1 files I renamed them to ".v1.ahk" and 
   ;          when I batch inserted #Requires AutoHotkey >=2.0- <2.1 into my v2 files I renamed them to ".v2.ahk"
   ;       4. For new scripts I have SciTE4AHK abbreviations rv1=... and rv2 =... that I insert into the directives 

   FN    := ""
   FNOut := ""

   ;USE SWITCH CASE TO DEAL WITH COMMAND LINE ARGUMENTS
   switch A_Args.Length
   {
      case 0:  ;IF NO ARGUMENTS THEN LOOK UP SOURCE FILE AND USE DEFAULT OUTPUT FILE
      {
         FN := FileSelect("", A_ScriptDir, "Choose an AHK v1 file to convert to v2")
      }
      case 1: ;IF ONE ARGUMENT THEN ASSUME THE ARUGMENT IS THE SOURCE FILE (FN) AND USE DEFAULT OUTPUT FILE
      {
         FN := A_Args[1]
      }
      case 2: ;IF ONLY TWO ARGUMENTS THEN IF A_Args[1] IS NOT input THEN ERROR
      {       ;ELSE A_Args[2] IS FN
         if (A_Args[1] = "-i" || A_Args[1] = "--input")
            FN := A_Args[2]
      }
      case 4:
      {  ;IF A_Args[1] IS input AND A_Args[3] IS output
         ; THEN A_Args[2] IS FN AND A_Args[4] IS FNOut
         if ((A_Args[1] = "-i" || A_Args[1] = "--input") && (A_Args[3] = "-o" || A_Args[3] = "--output"))
         {
            FN    := A_Args[2]
            FNOut := A_Args[4]
         } else
         {
            ;IF A_Args[1] IS output AND A_Args[3] IS input
            ;   THEN A_Args[2] IS FNOut AND A_Args[4] IS FN
            if ((A_Args[1] = "-o" || A_Args[1] = "--output") && (A_Args[3] = "-i" || A_Args[3] = "--input"))
            {
               FN    := A_Args[4]
               FNOut := A_Args[2]
            }
         }
      }
   }

   FN    := Trim(FN)
   FNOut := Trim(FNOut)

   If !FN
   {
      if A_Args.Length > 0 {
         MyMgs := ""
         For args in A_Args
         {
            MyMsg .= "A_Args[" . A_Index . "]:" . A_Args[A_Index] . "`n`n"
         ]
         MyMsg := "At least one of the above passed commandline parameters is invalid.`n"
         MyMsg .= "  Please make sure the parameters are correct and try again.`n"
         MyMsg .= "  Will exit due to parameter error: Error 48"
      } else {  ;IF _A_Args.Length = 0
         MyMsg := "No source file specified.`n"
         MyMsg .= "  Will exit due to lack of source file: Error AA.`n"
      }
      MsgBox MyMsg
      ExitApp
   }

   If !FNOut
   {
      FNOut := SubStr(FN, 1, StrLen(FN)-4) . MyOutExt   ;***USE OUTPUT EXTENSION OPTION***
   }

   if (!FileExist(FN))
   {
      MyMsg := "Source source file not found.`n"
      MyMsg .= "  Will exit because source file was not found. Error BB`n"
      MsgBox MyMsg
      ExitApp
   }
   inscript := FileRead(FN)
   outscript := Convert(inscript)
   outfile   := FileOpen(FNOut, "w", "utf-8")
   outfile.Write(outscript)
   outfile.Close()

   MyMsg := "Conversion complete.`n"
   MyMsg .= "  New file saved as: " . FNOut . "`n`n"
   MyMsg .= "    Would you like to see the changes made?"
   result := MsgBox(MyMsg,"", 68)
   if (result = "Yes") {
         Run("diff\VisualDiff.exe diff\VisualDiff.ahk `"" . FN . "`" `"" . FNOut . "`"")
   }
   ExitApp
} ;MAIN PROGRAM - ENDS HERE *******************************************************************************************
;######################################################################################################################
;##### FUNCTIONS(): #####
;######################################################################################################################
#include ConvertFuncs.ahk
;######################################################################################################################
;##### HOTKEYS: #####
;######################################################################################################################
; EXIT APPLICATION; EXIT APPLICATION; EXIT APPLICATION
Esc::
{ ;Exit application - Using either <Esc> Hotkey or Goto("MyExit")
   ExitApp
   Return
}

AHK_user
Posts: 523
Joined: 04 Dec 2015, 14:52
Location: Belgium

Re: v1 -> v2 Script Converter

Post by AHK_user » 25 Feb 2023, 04:16

@DrReflex : I have uploaded it to github, thanks for your contribution.

I personlly mainly use the QuickConvertorV2.ahk.

TheBigO
Posts: 28
Joined: 17 Dec 2020, 09:11

Re: v1 -> v2 Script Converter

Post by TheBigO » 16 Mar 2023, 03:12

Little Bug:

while (version.Length < current.Length())
version.Push("0")

Converter missed the second usage of Length() on the same line...

TheBigO
Posts: 28
Joined: 17 Dec 2020, 09:11

Re: v1 -> v2 Script Converter

Post by TheBigO » 16 Mar 2023, 03:19

And another one (I think):

This:

OnMessage(0x44, Func("translateMsgBoxButtons").Bind(["Yes", "No"]))

is resolved to that:

OnMessage(0x44, %translateMsgBoxButtons.Bind(["Yes", "No"])%)

But aren't the "%" superfluous or even wrong?

TheBigO
Posts: 28
Joined: 17 Dec 2020, 09:11

Re: v1 -> v2 Script Converter

Post by TheBigO » 16 Mar 2023, 03:41

Sorry, but I will post everything, I encounter:

This:

Gui CNS:Add, DropDownList, x460 y300 w332 AltSubmit Choose%chosen% VtyrePressuresConsentDropDown, % values2String("|", map(["Yes", "No", "Ask again later..."], "translate")*)

resolves to that:

CNS.Add("DropDownList", "x460 y300 w332 AltSubmit Choose" . chosen . " VtyrePressuresConsentDropDown", [values2String(", "`", map([`"Yes`", `"No`", `"Ask again later...`"], `"translate`")*)"])

which is totally wrong.

Should be:

CNS.Add("DropDownList", "x460 y300 w332 AltSubmit Choose" . chosen . " VtyrePressuresConsentDropDown", StrSplit("|", values2String(map(["Yes", "No", "Ask again later..."], "translate")*), "|"))

Indeed it should be:

CNS.Add("DropDownList", "x460 y300 w332 AltSubmit Choose" . chosen . " VtyrePressuresConsentDropDown", map(["Yes", "No", "Ask again later..."], translate))

...but that's not a conversion, you can do automatically.

TheBigO
Posts: 28
Joined: 17 Dec 2020, 09:11

Re: v1 -> v2 Script Converter

Post by TheBigO » 16 Mar 2023, 04:38

Cosmetical:

1. Fileread => FileRead
2. map() => Map()

TheBigO
Posts: 28
Joined: 17 Dec 2020, 09:11

Re: v1 -> v2 Script Converter

Post by TheBigO » 16 Mar 2023, 04:42

Maybe the same problem as above...

This:

OnError(Func("logError").Bind(true))

resolves to:

OnError(%logError.Bind(true)%)

Should be (I think):

OnError(logError.Bind(true))

TheBigO
Posts: 28
Joined: 17 Dec 2020, 09:11

Re: v1 -> v2 Script Converter

Post by TheBigO » 16 Mar 2023, 05:08

This:

FileRemoveDir %directoryName%, %recurse%

resolves to:

DirDelete(directoryName, %recurse%)

should be:

DirDelete(directoryName, recurse)

guest3456
Posts: 3477
Joined: 09 Oct 2013, 10:31

Re: v1 -> v2 Script Converter

Post by guest3456 » 16 Mar 2023, 09:47

TheBigO wrote:
16 Mar 2023, 03:41
Sorry, but I will post everything
thanks for all of your reports, extremely useful to help people find fixes

unfortunately this project is community effort, and people contribute when they have time. if are you able to fix these bugs, please submit pull request on github and your changes will be merged


TheBigO
Posts: 28
Joined: 17 Dec 2020, 09:11

Re: v1 -> v2 Script Converter

Post by TheBigO » 16 Mar 2023, 11:32

guest3456 wrote:
16 Mar 2023, 09:47
...

unfortunately this project is community effort, and people contribute when they have time. if are you able to fix these bugs, please submit pull request on github and your changes will be merged
Will probably do, when I have finished porting my > 150.000 LoC AHK1 project to AHK2...

Ben G
Posts: 42
Joined: 22 Feb 2023, 20:53
Contact:

Re: v1 -> v2 Script Converter

Post by Ben G » 20 Mar 2023, 11:31

Doesn't quite work. This is my code:
CODE:
This is the converter's output:
CODE:
Which shows this error when I run the script:
Image
How do I fix this? :think:
-Ben G
My Website (it's still in production).
Running Windows 11 Home on a 64-bit laptop.

jsong55
Posts: 321
Joined: 30 Mar 2021, 22:02

Re: v1 -> v2 Script Converter

Post by jsong55 » 16 Apr 2023, 05:36

Are Devs still making active updates to this converter?

Post Reply

Return to “Scripts and Functions (v2)”