Wouldn't that be a useful procedure and an equally useful tool?
All default settings of a script should be listed in the initial part of the script. In addition, in this 'IniSettings section' comments should be added that determine the structure of the IniFile to be used. Like this:
Code: Select all
; === IniSettings section ===
;[Settings]
Color := "green"
FontS := 7
;[Button]
ButtT := "&Start &&`r`nClose"
Code: Select all
IniFile := A_ScriptDir . "\Testing.ini"
; === IniSettings section ===
;[Settings]
Color := "green"
FontS := 7
;[Button]
ButtT := "&Start &&`r`nClose"
; === Created by IniScriptWizzard ===
; If changes are made in the above IniSettings section, delete this IniScriptWizzard section
; and reapply the IniScriptWizzard!
; === IniRead section ===
IfExist, %IniFile%
Keys@ := Array("[Settings]Color", "[Settings]FontS", "[Button]ButtT")
Loop % [email protected]()
{
Itm@ := Keys@[A_Index], Pos@ := InStr(Itm@, "]"), Lab@ := SubStr(Itm@, 2, Pos@ - 2), Key@ := SubStr(Itm@, Pos@ + 1)
IniRead, Val@, %IniFile%, %Lab@%, %Key@%, %A_Space%
If InStr(Val@, "`")
Transform, Val@, deref, %Val@%
%Key@% := Val@ ? Val@ : %Key@%
}
; === IniCreate section ===
IfNotExist %IniFile%
{
Ini@ = [Settings]`r`nColor =green`r`nFontS =7`r`n`r`n[Button]`r`nButtT =&Start &&``r``nClose
FileAppend, %Ini@% , %IniFile% , UTF-16
} ; === Created by IniScriptWizzard ===
Gui, Add, Text , w150, Color: %Color%
Gui, Add, Text , w150, FontS: %FontS%
Gui, Add, Button, w150, %ButtT%
Gui, Show
Return
Code: Select all
[Settings]
Color =green
FontS =7
[Button]
ButtT =&Start &&`r`nClose
Isn't this exactly what a script of such a kind is intended to do?
Thank you in advance for your comments and suggestions!
PS: An additional feature: linefeed, literal ; and literal & can be used in the text strings! (LF = `r`n | literal ; = `; | literal & = &&)
Code: Select all
; This script must be saved as a UTF-8 (or Unicode) file!
; (It uses look-alike replacement characters in the Help text.)
; =========================================================================
; IniScriptWizzard
Version := 0.9
; =========================================================================
Help =
(
=================================
IniScriptWizzard
=================================
Design an IniSettings part in your script containing all variables
which should be included in the IniFile and insert automatically
two script snippets as IniRead part and IniCreate part
=================================
USAGE:
- Create a properly designed IniSettings part in your script.
The part should contain all variables for which default values
are set in your script and which should be included in the IniFile.
- Select the IniSettings part and press Ctr+c.
- Press Ctrl+Shift+9.
- The IniRead part and the IniCreate part will be created and
inserted below the IniSettings part.
=================================
NOTES:
- If no IniFile creation should be forced, the IniCreate part can be
removed from the script.
- In the IniSettings part sections for the IniFile must be inserted
as comment line: ;[Settings]
- It is assumed that the name of the variable containing the
IniFile path is IniFile.
- The keys in the IniFile have the same names as the variables
set in the IniSettings part of the script.
- If only ANSI characters occur in text strings, "UTF-16" in the
FileAppend line can be removed. (UTF-8 cannot be used!)
- The use of some special characters in text strings defined in the
IniSettings part (which will be reproduced in the IniFile) is supported:
LF = ˋrˋn | literal ; = ˋ; | literal & = &&
=================================
)
; Script performance etc.
; =======================
#SingleInstance, Force
#Persistent
#NoEnv
SendMode, Input
SetKeyDelay, -1, -1, Play
SetWinDelay, 0
SetWorkingDir, %A_ScriptDir%
; =========================================================================
GoSub, TrayMenu
Gui IniScriptWizz: -DPIScale
Gui IniScriptWizz: Add, Text, w530, Select the properly designed IniSettings part of your AHK script and press Ctrl+C.
Gui IniScriptWizz: Add, Text, w530, Press Ctrl+Shift+9 .
Gui IniScriptWizz: Add, Text, w530, The IniRead part and the IniCreate part snippets will be inserted below the IniSettings part of your script.`r`n`r`n
Gui IniScriptWizz: Add, Button, w100 ggExit , %A_Space%&Exit%A_Space%
Gui IniScriptWizz: Add, Button, x150 yp w100 ggHelp , %A_Space%&Help%A_Space%
Gui IniScriptWizz: Add, Button, x425 yp w120 Default ggClose, %A_Space%&Close%A_Space%
Gui IniScriptWizz: Show, w560, IniScriptWizzard (Ver. %Version%)
; =========================================================================
RETURN
; =========================================================================
^+9::
Str := Str0 := StrI := Keys@ := Itm@ := Key@ := Ini@ := Snip := Snipp := ""
; Reinsert the IniSettings part
; =============================
Str := "`r`n" . ClipBoard . "`r`n"
ClipBoard:= ClipBoard . "`r`n`r`n; === Created by IniScriptWizzard ===`r`n; NOTE: If changes are made in the above IniSettings part, delete this part`r`n; created by IniScripWizzard and reapply IniScriptWizzard!`r`n"
Send ^v
Sleep, 400 ; CRITICAL ???
; Process the IniSettings part string
; ===================================
Loop, Parse, Str, `n, `r
{
Line := A_LoopField
If !Line
Continue
Line := RegExReplace(Line, "^\s*") , Line := RegExReplace(Line, "\s*$")
If RegExMatch(Line, "^;\s*\[.*]$")
{
Line := RegExReplace(Line, ";\s*\[", "[")
Str0 := Str0 . Line . "`r`n"
Sec := Line
}
Else
{
If !InStr(Line, "=")
Continue
Line := RegExReplace(Line, ":=", " =", , 1)
Line := RegExReplace(Line, "=\s*", "=", , 1)
If InStr(Line, "=""") && (SubStr(Line, -0) = """")
{
Line := RegExReplace(Line, "=""", "=²³", , 1) , Line := RegExReplace(Line, """$", "²³")
Line := RegExReplace(Line, """""", """") , Line := RegExReplace(Line, "²³", """")
}
Str0 := Str0 . Line . "`r`n"
Key := SubStr(Line, 1, InStr(Line, "=") - 1)
Key := RegExReplace(Key, "\s*$")
Item := Sec . Key
Snip := Snip . """" . Item . """, "
}
}
StrI := RegExReplace(Str0 , "\[", " `r`n[", , , 3)
StrI := RegExReplace(StrI, "`r`n", "``r``n")
Snip := "Keys@ := Array(" . SubStr(Snip, 1, -2) . ")"
; Create and insert the IniRead part
; ==================================
ClipBoard := "; === IniRead part ===`r`nIfExist, %IniFile%`r`n " . Snip . "`r`n Loop % [email protected]()`r`n {`r`n Itm@ := Keys@[A_Index], Pos@ := InStr(Itm@, ""]""), Sec@ := SubStr(Itm@, 2, Pos@ - 2), Key@ := SubStr(Itm@, Pos@ + 1)`r`n IniRead, Val@, %IniFile%, %Sec@%, %Key@%, %A_Space%`r`n If InStr(Val@, ""````"")`r`n Transform, Val@, deref, %Val@%`r`n %Key@% := (Val@ != """") ? Val@ : %Key@%`r`n }`r`n`"
ClipWait
Send ^v
Sleep, 800 ; CRITICAL ???
; Create and insert the IniCreate part
; =======================================
Snipp := "; === IniCreate part ===`r`nIfNotExist %IniFile%`r`n{`r`n Ini@ = " . StrI . "`r`n FileAppend, %Ini@%, %IniFile%, UTF-16`r`n} `; === Created by IniScriptWizzard ==="
ClipBoard := Snipp
Send ^v
Sleep, 600 ; CRITICAL ???
Return
gExit:
GuiIniScriptWizzClose:
ExitApp
Return
gHelp:
MsgBox % Help
Return
gClose:
Gui IniScriptWizz: Hide
Return
TrayMenu:
Menu, Tray, NoStandard
Menu, Tray, Add, &Show again , Restart
Menu, Tray, Add, &Exit , Exit
Menu, Tray, Default, &Show again
Menu, Tray, Click, 1
Return
Restart:
If Blocked
Return
Blocked := 1
Sleep, 500
Blocked := 0
Reload
Return
Exit:
ExitApp
Return