• Optionally setting a default value once a value is entered?

    From snosniv@21:1/5 to All on Sun Nov 6 06:40:47 2022
    My script asks the user to enter a value which, if the user then ticks a checkbutton, this value is retained for the next time the script is opened.

    I'm guessing I'd have to write back to the script, or is there a cleaner/simpler way?

    TIA , Kev P.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Rich@21:1/5 to snosniv on Sun Nov 6 16:08:15 2022
    snosniv <[email protected]> wrote:
    My script asks the user to enter a value which, if the user then
    ticks a checkbutton, this value is retained for the next time the
    script is opened.

    I'm guessing I'd have to write back to the script, or is there a cleaner/simpler way?

    That is /one/ way -- but you will immediately be thrust headfirst into 'self-modifying code' territory that way, and for modern OS's that
    offer file ownership protections, if the user running the script does
    not have write access to the actual script file, the write-back will
    fail.

    And, for true multi-user setups, even if the writes were to succeed,
    writing back to the script itself would mean that user X+1 receives the
    default that user X set. And this is probably not what you want.

    The more usual way is to store this 'value' into a config file, owned
    and accessible exclusively by the user running the script, and upon
    startup the script first checks for presence of the config file, and if present, uses the value stored therein to 'pre-setup' the defaults.
    And checking the box would create the config file (if it was not
    already created at script startup anyway).

    The 'ini' package in Tcllib can assist greatly with handling (read/write/create) of the actual config file.

    Then you just have to pick a storage place. On Linux that would
    likely best be ~/.config/Your-Scripts-Name-Here/defaults.ini (assuming
    you picked to use the 'ini' package, and named the file 'defaults').

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From snosniv@21:1/5 to Rich on Sun Nov 6 11:40:09 2022
    On Sunday, 6 November 2022 at 16:08:18 UTC, Rich wrote:
    snosniv <[email protected]> wrote:
    My script asks the user to enter a value which, if the user then
    ticks a checkbutton, this value is retained for the next time the
    script is opened.

    I'm guessing I'd have to write back to the script, or is there a cleaner/simpler way?
    That is /one/ way -- but you will immediately be thrust headfirst into 'self-modifying code' territory that way, and for modern OS's that
    offer file ownership protections, if the user running the script does
    not have write access to the actual script file, the write-back will
    fail.

    And, for true multi-user setups, even if the writes were to succeed,
    writing back to the script itself would mean that user X+1 receives the default that user X set. And this is probably not what you want.

    The more usual way is to store this 'value' into a config file, owned
    and accessible exclusively by the user running the script, and upon
    startup the script first checks for presence of the config file, and if present, uses the value stored therein to 'pre-setup' the defaults.
    And checking the box would create the config file (if it was not
    already created at script startup anyway).

    The 'ini' package in Tcllib can assist greatly with handling (read/write/create) of the actual config file.

    Then you just have to pick a storage place. On Linux that would
    likely best be ~/.config/Your-Scripts-Name-Here/defaults.ini (assuming
    you picked to use the 'ini' package, and named the file 'defaults').

    Ah yes, thanks for that.
    I'm retired & this is my first return to Tcl/Tk after 5+ years of not touching it, brain dump worse than I thought. :-(

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)