- See also the 3 posts above.
GENERAL: VAR = VALUE -> VAR := VALUE
- You have to be careful with numbers with leading/trailing zeros e.g.:
Code: Select all
var = 06
;goes to:
var := "06"
;not:
var := 06
GENERAL: BLANK PARAMETERS: COMMANDS -> FUNCTIONS
- With commands, blank parameters and omitted parameters look identical.
- So parameters that I perceived as being omissible, were in fact compulsory, e.g. FileAppend (Text), RegWrite (Value). (Note: RegWrite's Value parameter is stated as compulsory but appears to be omissible.)
blanks in ternary operator - AutoHotkey Community
https://autohotkey.com/boards/viewtopic ... 56#p189956
GENERAL: CONVERTING FUNCTIONS
- Let's say you have a script, and you've converted some lines, but not others. It will be easy to identify which lines are AHK v2 ready, and which lines still need to be converted.
- An exception is functions where the functionality has changed i.e. InStr/SubStr/RegExMatch/RegExReplace.
- E.g.
Code: Select all
InStr(vPath, "\", 0, 1) ;not ambiguous: works the same in AHK v1/v2
InStr(vPath, "\", 0, -1) ;ambiguous: works differently in AHK v1/v2
InStr(vPath, "\", 0, vPos) ;ambiguous: works differently in AHK v1/v2 if vPos is non-positive
- So, if you want to be sure how the code is supposed to work for ambiguous cases of StartingPos, you should leave a comment (i.e. to say that it was written for AHK v1/v2). Although there could be multiple functions with ambiguities on the same line making this less feasible.
- What I do instead, is, whenever InStr is ambiguous, I replace it with JEE_InStr, which has the AHK v2 behaviour in both AHK v1/v2.
- I fully support the changes to InStr/SubStr/RegExMatch/RegExReplace. But I would warn that any *changes* to functions, that aren't simply extending the functionality, can cause a lot of complications and awkwardness. And that this should not be underestimated.
- A possible solution is to convert instances with a non-positive StartingPos, to (subtract 1 and) use InStr2 (AHK v2 behaviour on AHK v1/v2), and any instances that have a variable for StartingPos to use InStr1 temporarily (AHK v1 behaviour on AHK v1/v2) (which then have to be manually converted to use InStr2).
- I would also suggest a mode for AHK v1, where, if set, the 4 functions behave like they do in AHK v2. So that people can write more forwards compatible code that won't have to be converted manually later, and so that code can be more easily reused between AHK v1/v2, and because the StartingPos values are more logical/easier to use (more newbie-friendly). This is not an argument primarily inspired by two-way compatibility, the JEE_InStr idea can achieve that.
GENERAL: FUNCTIONS WITHIN FUNCTIONS
- When converting from commands to functions, some things become clear, like the benefit of not having subcommands.
- One other thing worth considering, is avoiding, where possible, functions within functions. These are the sorts of things you discover while playing around in AHK v2.
Code: Select all
;e.g. proposal:
DateAdd(DateTime, Time, TimeUnits, Format)
;versus:
FormatTime(DateAdd(DateTime, Time, TimeUnits), Format)
- I made some comments here, at the bottom of this post:
why was LoopParse (no space) removed? - Page 2 - AutoHotkey Community
https://autohotkey.com/boards/viewtopic ... 42#p190742
GENERAL: XXXGETLIST FUNCTIONS
- Window list: WinGet-List -> WinGetList.
- Control list: WinGet-ControlList -> WinGetControls/WinGetControlsHwnd.
- Item list (items within control): - ControlGet-List -> ControlGetList (perhaps ControlGetItems).
- Re. counting items:
- WinGet-Count -> WinGetCount.
- WinGet-ControlList: There is no 'window get control count' option (unless you retrieve the pseudo-array).
- ControlGet-List has a count option.
CONTROLGETLIST (CONTROLGETITEMS?): STRING -> ARRAY
- Although returning an array as the default, instead of an LF-delimited string, makes sense (e.g. theoretically an item could contains LFs). It can be slow to build a string from an array, and to search for an item within an array. Thus an option to return a string may be sensible.
jeeswg's benchmark tests - AutoHotkey Community
https://autohotkey.com/boards/viewtopic ... 29#p192029
- Also, this suggests the need for a StrJoin function, which many have already talked about. Here's *a* version, one question is whether a capacity parameter should be present or not (I would suppose not, and, perhaps AHK can roughly estimate the lengths of all strings in an array, and so the parameter wouldn't be necessary).
Code: Select all
JEE_StrJoin(vSep, oArray*)
{
vOutput := ""
VarSetCapacity(vOutput, 1000000*2)
Loop, % oArray.MaxIndex()-1
vOutput .= oArray[A_Index] vSep
vOutput .= oArray[oArray.MaxIndex()]
return vOutput
}
- Pretty awkward to convert. (This is not a complaint, just an interesting curio.)
Code: Select all
;before:
Control, TabLeft, 3, SysTabControl321, A
Control, TabRight, 3, SysTabControl321, A
;after:
ControlSetTab(-3+ControlGetTab("SysTabControl321", "A"), "SysTabControl321", "A")
ControlSetTab(3+ControlGetTab("SysTabControl321", "A"), "SysTabControl321", "A")
- I have had a lot of problems in my scripts relating to Deref not being available. One use is to handle entries of the form '%dir%\MyFile.txt' loaded from ini files, or from the selected text.
- I think that this function, (including possible options to change the deref character(s), and support for environment variables, and perhaps for objects with keys, that I've previously mentioned,) would be very useful.
- I think it would be good to have Deref available and backport it to AHK v1 (which currently has it as a subcommand of Transform).
- This function cannot be recreated as a custom user-defined function.
FILEINSTALL
- I don't personally use FileInstall, but the FileInstall function does seem like a good candidate for being backported to AHK v1. Perhaps the only function that would *need* backporting, other than Deref.
FileInstall
https://autohotkey.com/docs/commands/FileInstall.htm
Extract file that was added with fileinstall - AutoHotkey Community
https://autohotkey.com/boards/viewtopic ... 35#p188235
PIXELGETCOLOR/PIXELSEARCH
- If these two functions allowed you to specify 'BGR', it would make automatic conversion a lot easier. Also RGB and BGR both appear commonly with regard to image scripts.
- The silliest (most nuanced) part of my converter so far, checks whether the pixel colour is the same in RGB and BGR, if it can't be sure, then it cannot safely convert that line, and so it omits it from the conversion, and adds it to the error list.
Code: Select all
JEE_ColRGBIsBGR(vNum)
{
if RegExMatch(vNum, "^0x\d{6}$")
return (SubStr(vNum, 3, 2) = SubStr(vNum, 7, 2))
else if RegExMatch(vNum, "^\d+$")
return ((vNum && 0xFF0000 >> 16) = (vNum && 0xFF))
else
return 0
}
- These are due to be removed from AHK v2.
- I am going to try to recreate these 2 commands as custom functions.
- AHK v2 doesn't let you specify the class of a GUI window, so there's at least that aspect that cannot be recreated currently, and so some of my scripts, that depend on the class name to identify the Progress windows, would have to be altered.
- I do feel that there will be other obstacles, perhaps some of them insurmountable, in trying to exactly recreate these commands with no loss of functionality, for example, to avoid making a script #Persistent.
- I am not sure, but I think that there are subtle optimisations and advantages that AHK GUI-like notification commands have, that a custom user-defined function can't reproduce.
- I have found these commands very useful when sharing scripts on forums, and doing diagnostic image/text/font tests. Once you have to manually include a custom function inside a script, and copy and paste it every time you want to use it on the forum, it's no longer newbie-friendly. Also, even if I did copy and paste the recreated Progress function, it might be exceedingly long and complicated.
REGEXMATCH: PSEUDO-ARRAY -> ARRAY
- RegExMatch will return an object in AHK v2, never a pseudo-array.
- However, the RegExMatch object is read-only, and so you can't edit the array items.
- I attempted to create a function that approximately recreates it, partly to understand everything that's in the object. I also provided an example script to recreate part of the object.
RegExMatch
https://autohotkey.com/docs/commands/Re ... atchObject
RegEx: edit RegEx match object - AutoHotkey Community
https://autohotkey.com/boards/viewtopic.php?f=5&t=40679
STRINGSPLIT -> STRSPLIT: PSEUDO-ARRAY -> ARRAY
- There are two options re. conversion with StringSplit. Create a pseudo-array manually, or replace the pseudo-array with an array (via StrSplit).
- Although I would recommend converting to StrSplit, and using a proper array, it can be very time-consuming and awkward to make all of the necessary adjustments, and this would have to be done manually. So for one of my big scripts with around 100 uses of StringSplit, I converted it to still use a pseudo-array, until I have the time to manually review the (very old) code.
- StringSplit can be replaced automatically (without the need for manual conversion to StrSplit) like so:
Code: Select all
;before
StringSplit, vPseudoArray, vText, % vSep
;after:
Loop, Parse, vText, % vSep ;string split
vPseudoArray%A_Index% := A_LoopField, vPseudoArray0 := A_Index
;more generally:
;before
StringSplit, vPseudoArray, vText, % vSep
MsgBox, % vPseudoArray0
MsgBox, % vPseudoArray1
;after (replace with StrSplit):
oArray := StrSplit(vText, vSep)
MsgBox, % oArray.Length()
MsgBox, % oArray.1
;after (keep using pseudo-array):
Loop, Parse, vText, % vSep ;string split
vPseudoArray%A_Index% := A_LoopField, vPseudoArray0 := A_Index
- The following function could be useful for some circumstances:
Code: Select all
; ;e.g.
; vText := "abc,def,ghi,jkl,mno,pqr,stu,vwx,yz"
; v0 := JEE_StrSplitToVars(vText, ",",, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20)
; MsgBox, % v0 " " v1 " " v2
; ;e.g.
; vText := "abcdefghijklmnopqrstuvwxyz"
; v0 := JEE_StrSplitToVars(vText,,, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20)
; MsgBox, % v0 " " v1 " " v2
JEE_StrSplitToVars(vText, vDelims:="", vOmitChars:="", ByRef v1:="", ByRef v2:="", ByRef v3:="", ByRef v4:="", ByRef v5:="", ByRef v6:="", ByRef v7:="", ByRef v8:="", ByRef v9:="", ByRef v10:="", ByRef v11:="", ByRef v12:="", ByRef v13:="", ByRef v14:="", ByRef v15:="", ByRef v16:="", ByRef v17:="", ByRef v18:="", ByRef v19:="", ByRef v20:="")
{
global vJeeStrSplitToVarsWarn
oArray := StrSplit(vText, vDelims, vOmitChars)
if (oArray.Length() > 20) && vJeeStrSplitToVarsWarn
MsgBox, % A_ThisFunc ": warning:`r`n" "item count (" oArray.Length() ") exceeds limit (20)"
Loop, % (oArray.Length() > 20) ? 20 : oArray.Length()
v%A_Index% := oArray[A_Index]
return oArray.Length()
}
- The use of + and ^ presents a subtle problem in the move to function-style parameters.
Code: Select all
;before:
WinSet, Style, 0x1000000, % "ahk_id " hWnd
WinSet, Style, +0x1000000, % "ahk_id " hWnd
WinSet, Style, -0x1000000, % "ahk_id " hWnd
WinSet, Style, ^0x1000000, % "ahk_id " hWnd
;after:
WinSetStyle(0x1000000, "ahk_id " hWnd)
WinSetStyle("+" 0x1000000, "ahk_id " hWnd)
WinSetStyle(-0x1000000, "ahk_id " hWnd)
WinSetStyle("^" 0x1000000, "ahk_id " hWnd)
WinSet
https://lexikos.github.io/v2/docs/comma ... .htm#Style
Control Functions
https://lexikos.github.io/v2/docs/comma ... m#SetStyle
GENERAL COMMENTS
- It's curious, instead of just manually fixing a particular code line, intellectualising it, in order to automate its conversion.
- Combining registry keys and subkeys is a bit awkward, after having already experienced most conversion problems, I suddenly found the need to 'clean' a function parameter, e.g. "a" "b" "c" -> "abc" (e.g. "HKEY_CLASSES_ROOT" "\" "txtfile" -> "HKEY_CLASSES_ROOT\txtfile").
- Some changes really make the simplicity clearer:
Code: Select all
;before:
ControlGetFocus, vCtlClassNN, A
;after:
vCtlClassNN := ControlGetFocus("A")
- I have already had this problem a few times, that of 'unconverting', e.g. to share an AHK v2 script in AHK v1 form on the forum, I will have to do work on an 'unconverter'. Luckily, my work on two-way compatibility has made this easier.
- For using AHK v1 and AHK v2 simultaneously for different scripts, an /inc switch, and also allowing A_AhkPath with #Include, would be incredibly useful.
- Perhaps every function that lacks a return value at present, should return 1 if there are no errors. And WinWait/WinWaitActive, could return an hWnd.
- 'GuiCtrlFromHwnd', 'Ctl'? 'Ctrl' seems a bit odd, and more reminiscent of keyboard keys.
- There has been mention that ahk_dhw/ahk_mode wouldn't break existing scripts so could be added at a later date, but I've always found the use of things like Last Found Window/A_StringCaseSense/A_DetectHiddenWindows/A_TitleMatchMode most unsatisfactory, and a huge amount of my code could be improved by WinWait/WinWaitActive returning an hWnd, ahk_dhw s/c(ontains)/(e)x(act)/e(nds)/r and ahk_mode vh/v/h being available, and a case sensitive StrReplace parameter being available (although in that case, RegExReplace could be used instead). There is also this issue:
DetectHiddenWindows bug? - AutoHotkey Community
https://autohotkey.com/boards/viewtopic ... 14&t=30010
- I am reminded that, if feasible, having a way to redefine a built-in function, whilst having access to the original built-in function, could have advantages, allowing users to backport certain functionality manually. Otherwise, AHK users are completely dependent on changes to the source code.
- I would consider having ControlGetChoice as part of ControlGetList (aka ControlGetItems). Or as part of 'LBGetItems'/'CBGetItems', if they were separate functions. I am not against ControlChoose/ControlChooseString being one function per se, however, realistically this will add '+0' workaround awkwardness to yet another function. Sometimes the item strings are integers. (Note: I am quite neutral, and don't have strong opinions re. the topics in this bullet point.)
- It's interesting how ControlGet and Control have been combined in the AHK v2 help.
Control Functions
https://lexikos.github.io/v2/docs/commands/Control.htm
- What happened to the colour table from the Progress/SplashImage page?
Progress/SplashImage
https://autohotkey.com/docs/commands/Pr ... ect_Colors
Color Names
https://lexikos.github.io/v2/docs/misc/Colors.htm
- I have posted comments about parsing issues here:
parsing AutoHotkey scripts - AutoHotkey Community
https://autohotkey.com/boards/viewtopic.php?f=5&t=40471
- Thanks for reading.