A scripting wizzard: Is this a useful tool?

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
newbieforever
Posts: 493
Joined: 24 Aug 2016, 03:34

A scripting wizzard: Is this a useful tool?

14 Dec 2018, 07:54

IniScriptWizz: The following refers to scripts that should use an IniFile.

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"
IniScriptWizzard is a little tool that automatically generates two script snippets out of such a 'properly designed' IniSettings section and inserts them as a 'IniRead section' and a 'IniCreate section' into the script:

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
If there is no IniFile when such a script is run, the IniFile is created as determined in the IniSettings section:

Code: Select all

[Settings]
Color =green
FontS =7

[Button]
ButtT =&Start &&`r`nClose
If an IniFile already exists, the script reads the IniFile and takes the values if they are set there, or it takes the default values set in the IniSettings section.

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
Edited Dec 12 2018, Dec 23 2018, Dec 24 2018
Last edited by newbieforever on 24 Dec 2018, 10:56, edited 12 times in total.
newbieforever
Posts: 493
Joined: 24 Aug 2016, 03:34

Re: A scripting wizzard: Is this a useful tool?

16 Dec 2018, 15:28

There have been no answers to my posting so far, my scripting wizzard doesn't seem to be very interesting for other AHK users...

Nevertheless: I have improved the procedure and the wizzard in the meantime and edited my original posting.

I myself use IniScriptWizz now and find it very handy.
oif2003
Posts: 214
Joined: 17 Oct 2018, 11:43
Contact:

Re: A scripting wizzard: Is this a useful tool?

17 Dec 2018, 16:04

Hi, newbieforever, I think your post should be under "Scripts and Functions" instead of Ask for Help. Things tend to get buried rather quickly here. Perhaps you can ask the mods to move it for you.
newbieforever
Posts: 493
Joined: 24 Aug 2016, 03:34

Re: A scripting wizzard: Is this a useful tool?

17 Dec 2018, 16:09

@oif2003: No, please, not yet! I'm not sure yet if the IniScriptWizzard is really flawless and optimally written. Hence my question about your comments and suggestions...

PS (edited): Especially Send ^v seems to have occasional timing problems (possibly more often when the script is compiled). I inserted now more Sleep in my script. Are there better solutions?
newbieforever
Posts: 493
Joined: 24 Aug 2016, 03:34

Re: A scripting wizzard: Is this a useful tool?

23 Dec 2018, 15:19

No comments...

A question: Is there a method to know when a Send ^v command is executed? My Sleeps in the above script seems not to be reliable enough.

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: No registered users and 71 guests