wxBasicScript


by Mark Ulrich

Introduction

wxbasicscript (short: wxbs) is a simpler version of the programming-language wxBasic ( http://wxbasic.sf.net ).

While wxBasic allows you to alter every detail of a window, wxbasicscript wants to offer some "pre-defined" dialogs, using external addons like "Xmessage" and "Xdialog".

In some cases, Xdialog will be too limited, so you also can use Gtkdialog, that allows you to combine elements like buttons and lists to your own window.

Different to shellscripts, you don't need to learn complex "regular expressions".

wxbs provides simple functions like
Code:
result = left ( "Markus" , 4 )
print result


As wxbs just has a small set of such functions, you can "include" external libraries.

So
Code:
include "/usr/lib/wxbasicscript/basefunctions.inc"
result = cutleft ( "http://www.linux.de" , "//" )
print result
      


will produce:

A first graphical program

Create a new folder /root/wxbs
Open beaver, and enter this code:

Code:
#!/usr/bin/wxbasicscript
// tut01.wxbs
include "/usr/lib/wxbasicscript/basefunctions.inc"
xmessage ( "hello Puppy" )
      


Save this as /root/wxbs/tut01.wxbs

Now make it executable in rox:



Now you can run your program by clicking it in rox, and a messagebox appears.

Explanation

Lines beginning with // are comments and will be ignored during execution.

xmessage ()
is one of the functions included from basefunctions.inc , that make life easier.
It will give you these functions:

--
--
--


Return-values

Code:
#!/usr/bin/wxbasicscript
// tut02.wxbs
include "/usr/lib/wxbasicscript/basefunctions.inc"
result = xmessage ( "-buttons yes,no Please coose" )
xmessage ( result )
      


Xmessage, Xdialog and Gtkdialog return values to let you know, what Option the user selected.
You can optain them by "result = ..."


Conditions

If we got a result, we must decide what to do, depending on its value.

Code:
#!/usr/bin/wxbasicscript
// tut03.wxbs
include "/usr/lib/wxbasicscript/basefunctions.inc"
result = xmessage ( "-buttons yes,no Please coose" )
MSG = ""
if result = "yes" then MSG = "you chose yes"
if result = "no" then MSG = "you chose no"
if result = nothing then MSG = "you closed the window"
xmessage ( MSG )
      


Here the variable MSG is assigned different values, depending on the return-value "result"

You can "contacenate" strings (Text-Variables) with "&" to get shorter code:

Code:
#!/usr/bin/wxbasicscript
// tut04.wxbs
include "/usr/lib/wxbasicscript/basefunctions.inc"
result = xmessage ( "-buttons yes,no Please coose" )
MSG = "you "
if result = "yes" then MSG &= "chose yes"
if result = "no" then MSG &= "chose no"
if result = Nothing then MSG &= "closed the window"
xmessage ( MSG )
      


If you want to put more than one lines in an "if"-condition, do it like this:
Code:
if result = "yes" then 
  MSG &= "chose yes"
  print MSG
end if
      



Execute external programs

You can run external programs using the "shell()"-command.
Your script will stop, until the program is finished (closed).
shell ( "beaver" )

To avoid that, add " &" to the command.
shell ( "beaver &" )

If you need the exit-code of a program, use the "xwin_exec()"-command:
result = xwin_exec ( "beaver" )

Differences of Xmessage and Xdialog

Xmessage returns "strings", for example the "yes" of a button.
Xdialog returns a number, representing a button.
In addition, it returns a string, that returns an optional choice, as in a "menue".




This example shows how to use Xdialog:
Code:
#!/usr/bin/wxbasicscript
// tut05.wxbs
include "/usr/lib/wxbasicscript/basefunctions.inc"
dialog = "--title 'MENU BOX' "&
"         --menu 'Simple program launcher "&
" \n\nWhat program do you want to run?' 0 0 10 "&
"        'beaver'  'Gtk-Texteditor' "&
"        'leafpad'  'another Gtk-Texteditor'"
result , choice = xdialog ( dialog )
if result = 0 then
  shell ( choice & " &" )
else
  xdialog ( "--title 'info' --msgbox 'dismissed' 0 0" )
end if
      


Here the Xdialog-code is encapsulated in ""&
This means
line1 and
line2 and
line3 ...

You also can put this code in an external file, a so called "template".
Then you don't need to "encapsulate" the Xdialog-code:

File: menubox1.tpl
Code:
         --title "MENU BOX"
         --menu "Simple program launcher 
\n\nWhat program do you want to run?" 0 0 10
        "beaver"  "Gtk-Texteditor"
        "leafpad"  "another Gtk-Texteditor"
      


Code:
#!/usr/bin/wxbasicscript
// tut06.wxbs
include "/usr/lib/wxbasicscript/basefunctions.inc"
dialog = readfile ( STARTDIR & "/menubox1.tpl" )
result , choice = xdialog ( dialog )
if result = 0 then
  shell ( choice & " &" )
else
  xdialog ( "--title 'info' --msgbox 'dismissed' 0 0" )
end if
      


The "\n" is a "Line-Feed", so you get an empty line in the dialogbox.
Note the line
dialog = readfile ( STARTDIR & "/menubox1.tpl" )

The Variable STARTDIR holds the folder of your script. Just like this you can be shure, your template will be found.

Using templates has advantages:
- Your mainscript gets shorter (easier to read)
- You can copy&paste lines from the Xdialog-examples at http://xdialog.dyns.net/ ,and just have to delete the"\" in the end of each line.