Home | Contents | Index | Help | < Browse | Browse >

 Creating a switch button

 You can simplify making a switch button by writing a little shell script:

   .key var,s1,s2

   if $<var> EQ <s1>
          setenv <var> <s2>
   else
          setenv <var> <s1>
   endif

 Name this script "switch", Set the script flag (Protect "switch" +S)
 and use it as shown below:

    close off
    gap 4
    defenv s ON

    button exit 'unsetenv s' exit
    space
    startbox
      text "The switch is:"
      button [s] 'switch s ON OFF' update
    endbox

 Try it!

 -------------------------------------------------------------------------

 More about variables...

 Remember that the env variables in a selector script file only are
 updated when a script is updated or loaded. So don't try something
 like this:

 button "Load script" "setenv name `requestfile`" load [name]

 The variable [name] is not changed... You can solve the problem
 above like this:

 button "Load script" "run >nil: selector `requestfile`" exit

 -------------------------------------------------------------------------

 Changable arguments

 Maybe you would like to pass a variable from the command line, that then
 could be altered. This you can do by passing the arg to an env variable:

 Example: (script called ChArgs.sel)

    defarg arg1 'Nothing passed'
    defenv vary '[arg1]'
    close off

    button '[vary]' 'setenv vary "Button pressed"' update
    button Quit 'unsetenv vary' exit

 Try it like this:

    Selector ChArgs 'Press the button!!'
    Selector ChArgs

 -------------------------------------------------------------------------

 Unsetenv technique

 If you wan't some variables in an information window without buttons,
 you could wonder how to unset them again. This because there is no exit
 button to unset them on... well, the answer is simple, use the unsetenv
 command at the end of the script:

 Ex: (script called "etips.sel")

    defarg arg1 c:type
    exec 'setenv size `list "[arg1]" lformat "%l"`'

    text 'The size of [arg1]'
    text 'is [size] bytes'

    unsetenv size

 Try it like this:

    selector etips
    selector etips sys:utilities/more

 This technique is possible because of the way selector inserts the env
 variables into the script, line by line. You can use this technique as
 long as you only use [variables], and no $variables. This because the
 variables only exists when the panel is loaded, and not when a button
 is pressed etc. Then they exist in the script, but not in ENV:

 This technique is in fact preferable over the alternative to unset the
 variables on an exit button. It also makes the env variables occupied for
 a shorter time, which in turn prevents name conflicts. It also in many
 cases let you skip the exit button and "close off" command. The fact that
 I not used this technique in many of the examples, is that I didn't think
 of this possibility until some minutes ago. I also added the unsetenv
 command to speed the technique up (instead of exec 'unsetenv...)

 Example:

 The corresponding script (using this technique) for the
 dissapearing button example would be:

    defenv item "button 'Remove button' 'setenv item Text Nothing' update"
    [item]
    unsetenv item

 Try it!

 But, as mentioned, this technique is NOT allways possible to use, for
 example in the switch example.

 -------------------------------------------------------------------------

 Passing variables

 If you want to write, for example a general requester that can be called
 from other selector scripts, you can make this in several different ways.
 I want to show you three different ways to accomplish the same goal.

 In the examples, we want to call a color requester, and set the
 background to this color. Each of the alternatives uses different
 ways of passing variables.

 1. The simplest way. One that starts a new copy of selector. This
    takes of cource a little more memory, and is a little bit slower
    than the next two alternatives. It is much more slow if you not
    have a hard disk and selector not resident.

    Calling file:
    background [color]
    button "Select color" "setenv color `selector color.sel`" update

    Called file:
    button "Gray" exit 0
    button "Black" exit 1
    button "White" exit 2
    button "Blue" exit 3

 2. One that loads the other file to this copy of selector, and then
    reloads the old script. For this you must pass the name of the calling
    script to the called file.

    Calling file:
    background [arg1]
    button "Select color" load 'color.sel "[this]"'

    Called file:
    button "Gray" load '"[arg1]" 0'
    button "Black" load '"[arg1]" 1'
    button "White" load '"[arg1]" 2'
    button "Blue" load '"[arg1]" 3'

 3. Similar to number 2. But here the name of the variable that will
    be set is passed to color.sel, and set in that file.

    Calling file:
    background [color]
    button "Select color" load 'color.sel "[this]" color'

    Called file:
    button "Gray" 'setenv [arg2] 0' load '"[arg1]"'
    button "Black" 'setenv [arg2] 1' load '"[arg1]"'
    button "White" 'setenv [arg2] 2' load '"[arg1]"'
    button "Blue" 'setenv [arg2] 3' load '"[arg1]"'

 -------------------------------------------------------------------------

Home | Contents | Index | Help | < Browse | Browse >