noframes
Autostartmanager - Step 3

run a script on buttonclick


We need the new entry, to be able to change the values in the list.
As the list reads its values from "autostart.cfg", we must update this file with the value from the entry, and then reload the list.

So I add a button "set".
It will run the script "autostart_updatelist.wxbs".
Note the parameters that will be passed to the script:
$LIST1°°°$ENTRY1

Normally, parameters are seperated by spaces.
But imagine, you want to replace a value "my program" in the list with a value "my OWN program" from the entry.
You will not know, where the first value "stops" and the second one begins.
This forbids to use a space as delimiter, so I use "°°°", that is very uncommon.

autostart.tpl
Autostart_Manager='


<frame existing entries>

    <list>
      <variable>LIST1</variable>
      <input>cat autostart.cfg</input>
      <action>refresh:ENTRY1</action>
      <width>400</width>
      <height>300</height>
    </list>

      <entry>
        <variable>ENTRY1</variable>
        <input>echo $LIST1</input>
      </entry>

    <button>
      <label>set</label>
      <action>./autostart_updatelist.wxbs $LIST1°°°$ENTRY1</action>
      <action>clear:LIST1</action>
      <action>refresh:LIST1</action>
    </button>

    <button>
      <label>Save and Exit</label>
      <action>./autostart_save.wxbs</action>
      <action>EXIT=EXIT</action>
    </button>

    <button>
      <label>Exit</label>
      <action>EXIT=EXIT</action>
    </button>


</frame>
'

autostart.wxbs remains unchanged.


So how does "autostart_updatelist.wxbs" looks like?

autostart_updatelist.wxbs
#!/usr/bin/wxbasicscript
include "/usr/lib/wxbasicscript/basefunctions.inc"


args = argvtostring()

mylist = explode( args , "°°°" )
torep = mylist[0]
rep = mylist[1]


alllines = readfiletolist( STARTDIR & "/autostart.cfg" )


for i=0 to count( alllines ) -1

  if alllines[i] = torep then
     alllines[i] =  rep
  end if

next

writelisttofile( STARTDIR & "/autostart.cfg" , alllines )



Ok...
First I read in the parameters with a new function "argvtostring()".
Normally, parameters are seperated by spaces, so you can obtain them as command(3) command(4) ...
The first 2 command() hold the name of the interpreter (wxbasic), and of the script itself.

Using argvtostring(), we have a string like
beaver°°°beaver2

So for example someone wants to replace "beaver" with "beaver2".
We must seperate the values, you can use "explode()" for this.

explode returns a list.
mylist[0] will be "beaver"
mylist[1] will be "beaver2"

So we read in our configurationfile "autostart.cfg", and in a "for-next-loop" we search for "beaver" and replace it.
Finally the file is written back with writelisttofile().


So for the final "autostart-editor" just 1 thing is missing:
We must add our configuration file to one of the existing startupfiles.

Therefore we add a new Button "Save and Exit".
It will run autostart_save.wxbs and then exit.

    <button>
      <label>Save and Exit</label>
      <action>./autostart_save.wxbs</action>
      <action>EXIT=EXIT</action>
    </button>

The script "autostart_save.wxbs" will be the last chapter of this small tutorial.