AHK V2 AND STRINGS:
==================================================
INTRODUCTION:
suggestions:
- 'CdLn' custom function for preparing command-line parameters [EDIT: and/or pass an array to Run]
- Deref function restored as a built-in function or as a custom function [EDIT: or perhaps not]
- one-line 'continuation sections' (AHK v1/v2 compatible), code name 'Verbatim' [EDIT: 'var `= value' (for AHK v1-style assignment)]
- Format function to accept variable names inside the FormatStr parameter
[EDIT:] Summary of main ideas:
Code: Select all
vPathExe := "C:\Program Files\AutoHotkey\AutoHotkey.exe"
vPath := "C:\Program Files\AutoHotkey\AutoHotkey.ahk"
;ideally, allow AHK v1-style assignments, perhaps via an `= operator:
;note: use `s and/or `t for leading/trailing whitespace
vTarget = "%vPathExe%" "%vPath%"
MsgBox, % vTarget
vTarget = %A_ComSpec% "/c" "%vTarget%"
MsgBox, % vTarget
;a A_DQ variable could be useful:
A_DQ := Chr(34)
MsgBox, % vTarget := A_DQ vPathExe A_DQ " " A_DQ vPath A_DQ
MsgBox, % vTarget := A_ComSpec " /c " A_DQ vTarget A_DQ
;another useful approach that could also be added:
MsgBox, % vTarget := JEE_CdLn(vPathExe, vPath)
MsgBox, % vTarget := JEE_CdLn(A_ComSpec, "/c", vTarget)
;possibly also, Run to accept an array parameter:
;all 3 equivalent:
;vTarget := JEE_CdLn(vPathExe, vPath)
;Run(vTarget)
;Run(JEE_CdLn(vPathExe, vPath))
;Run([vPathExe, vPath])
return
;==================================================
;AHK v2 and strings - AutoHotkey Community
;https://autohotkey.com/boards/viewtopic.php?f=37&t=36833
JEE_CdLn(oArray*)
{
vOutput := ""
for _, vValue in oArray
{
if InStr(vValue, " ")
vOutput .= (A_Index=1?"":" ") Chr(34) vValue Chr(34)
else
vOutput .= (A_Index=1?"":" ") vValue
}
return vOutput
}
;==================================================
- Format doesn't handle variable names
- handling paths with hardcoded 'A_'/environment variables: e.g. '%AppData%\MyDir'
- improved readability gained in certain circumstances by using %%
- inconvenience of 4-line continuation sections for assigning 1 line of text
- a custom Deref function cannot identify escaped (i.e. literal) % signs
- a custom Deref function cannot distinguish between local/global variables
- a Deref function could support objects as well as variables
- %% is not just an AHK thing, it's a Windows thing
characters:
- " - paths use double quotes a lot
- % - paths use percent signs a lot (environment variables)
- {} - Send/SendInput use curly braces a lot
- () - functions use parentheses a lot
- " - functions use double quotes a lot
- , - functions use commas a lot
- " and ' - handling strings that contain both " and '
==================================================
BENEFITS OF %%:
- %% is not just an AHK thing, it's a Windows thing, people are tempted to keep it not just because they're used to it, but because it's a common notation used by Windows for paths.
- %% can be used to improve readability:
Code: Select all
text2 := "text2", text4 := "text4"
vPath := Deref("%dir%\text1 %text2% text3 %text4%.%ext%")
;versus:
vPath := dir "\text1 " text2 " text3 " text4 "." ext
vPath := A_ScriptFullPath
SplitPath, vPath, vName, vDir, vExt, vNameNoExt, vDrive
vText := Deref("
(
path: %vPath%
name: %vName%
dir: %vDir%
ext: %vExt%
name no ext: %vNameNoExt%
drive: %vDrive%
)")
Code: Select all
%A_Desktop%\MyFile.txt
%A_ScriptDir%\MyFile.txt
%vDirLogs%\MyFile.txt
%A_ScriptDir%\MyLog %A_YYYY%-%A_MM%.txt
C:\Users\%username%\AppData\Roaming\Microsoft\Windows\Recent
- Although for simple paths, environment variables do not always aid readability particularly:
vPath := Deref("%A_Desktop%\MyFile.txt")
versus:
vPath := A_Desktop "\MyFile.txt"
It is better, in an ini file, to store:
'%A_Desktop%\MyFile.txt'
versus:
'A_Desktop "\MyFile.txt"'
And it is easier to copy/paste/modify strings back and forth between Explorer and the AHK editor, if they are stored in a form that is closer to the original paths. It is also easier to manipulate and perform text operations on the environment variable form.
- The %% notation doesn't just have to be used for variables:
var := Deref("text%var%text")
it could be extended to handle objects e.g.:
var := Deref("text%obj.key%text")
- When considering notation for dereferencing:
{} - Send/SendInput use curly braces a lot
() - functions use parentheses a lot
% - % is a character that is used very rarely and is quite bold and easy to identify
==================================================
BENEFITS OF AN (AMENDED) DEREF FUNCTION:
- The Deref function traditionally used `% to specify a literal percent sign, I am not necessarily against that, but my instinct is to not use such special handling, I would rather just do this:
Code: Select all
vNum := 20, vPct := "%"
MsgBox, % Deref("roughly %vNum%%vPct% of AutoHotkey users")
- The Deref function could be made to handle objects:
Code: Select all
obj := {key:20}, vPct := "%"
MsgBox, % Deref("roughly %obj.key%%vPct% of AutoHotkey users")
Code: Select all
obj := {num:20, pct:"%"}
MsgBox, % Deref("roughly %obj.num%%obj.pct% of AutoHotkey users", obj)
- A Deref function could have a parameter with special options, e.g. to prefer environment variables where they exist, to use environment variables as a backup if no variable is found, and to change which character is used to do the dereferencing.
==================================================
ISSUES WITH COMMAND LINE PARAMETERS AND DOUBLE QUOTES:
I had thought that maybe bringing `" into AHK v1, if possible, could be a fix for string issues relating to double quotes in AHK v1/v2, but after considering many possible solutions for this issue, I don't think it would be a good solution ...
Examples of some of the possibilities re. command line parameters:
Code: Select all
;good:
Run, "%vPathExe%" "%vPath%" ;AHK v1
;all of these options are horrible:
vPathExe := "notepad.exe", vPath := A_ScriptFullPath
Run(Chr(34) vPathExe Chr(34) " " Chr(34) vPath Chr(34)) ;AHK v1/v2
Run("""" vPathExe """ """ vPath """") ;AHK v1
Run("`"" vPathExe "`" `"" vPath "`"") ;AHK v2
Run('"' vPathExe '" "' vPath '"') ;AHK v2 ;the single/double quotes merge together in certain fonts
Run(Format("`"{}`" `"{}`"", vPathExe, vPath)) ;AHK v2
Run(Format('"{}" "{}"', vPathExe, vPath)) ;AHK v2
;note: the Deref function was removed from AHK v2:
vDQ := Chr(34)
Run(Deref("%vDQ%%vPathExe%%vDQ% %vDQ%%vPath%%vDQ%")) ;AHK v1/v2
Run(Deref("""%vPathExe%"" ""%vPath%""")) ;AHK v1
Run(Deref("`"%vPathExe%`" `"%vPath%`"")) ;AHK v2
;this is the least bad option, although I won't use it
;because I want a two-way compatible option, plus
;there is no Deref function in AHK v2 at present:
Run(Deref('"%vPathExe%" "%vPath%"')) ;AHK v2
Code: Select all
;workaround:
vPathExe := "notepad.exe", vPath := A_ScriptFullPath
Run(JEE_CdLn(vPathExe,vPath))
JEE_CdLn(oArray*)
{
vOutput := ""
for _, vValue in oArray
{
if InStr(vValue, " ")
vOutput .= (A_Index=1?"":" ") Chr(34) vValue Chr(34)
else
vOutput .= (A_Index=1?"":" ") vValue
}
return vOutput
}
Code: Select all
;suggestion:
Verbatim, vOpt, vTarget, "%vPathExe%" "%vPath%" ;AHK v1/v2
Run(vTarget)
Reconsideration request? - AutoHotkey Community
https://autohotkey.com/boards/viewtopic.php?f=37&t=39778&p=182026#p182026
Code: Select all
vPathExe := "C:\Windows\System32\notepad.exe"
vPath := "C:\MyDir\MyFile.txt"
MsgBox(JEE_CdLn(vPathExe,vPath)) ;"C:\Windows\System32\notepad.exe" "C:\MyDir\MyFile.txt"
Run(JEE_CdLn(vPathExe,vPath))
Run([vPathExe,vPath]) ;perhaps Run could accept an array to achieve the same thing
BENEFITS OF A ONE-LINE CONTINUATION SECTION:
i.e. a special 'Verbatim' command:
- the inconvenience of 4-line continuation sections for assigning 1 line of text
- the avoidance of extra quote or escape characters that make text less readable
" - paths use double quotes a lot
" - functions use double quotes a lot
" and ' - single/double quotes can merge together appearance-wise, i.e. they can look unclear in many fonts, making it hard to distinguish between the characters
- text can be maintained in its original form, which is useful for copy and paste
- the convenience of special options such as an environment variable mode, so you could just use %username% instead of %A_UserName% etc
- useful for AHK v1 forwards compatibility to AHK v2, if the command was to have the same behaviour in both versions
==================================================
FORMAT FUNCTION:
- Format doesn't handle variable names (at present)
- Send/SendInput use curly braces a lot, but so does Format, this again suggests the potential usefulness of the Deref function.
- Here's how I might use Format to do a particular task from earlier. Also, if Format could handle variable names, or objects/keys, one idea might be:
Code: Select all
vNum := 20, obj := {key:20}
MsgBox, % Format("roughly {}% of AutoHotkey users", vNum)
MsgBox, % Format("roughly {1:i}% of AutoHotkey users", vNum)
MsgBox, % Format("roughly {vNum:i}% of AutoHotkey users")
MsgBox, % Format("roughly {obj.key:i}% of AutoHotkey users")
;if there was the potential for any number versus variable name clashes, unlikely since in AHK v2, variable names can't begin with a digit, you could put the variable name in brackets:
MsgBox, % Format("roughly {(vNum):i}% of AutoHotkey users")