Using gambas in bash scripts

Post your Gambas programming questions here.
Post Reply
User avatar
BruceSteers
Posts: 1579
Joined: Thursday 23rd July 2020 5:20pm
Location: Isle of Wight
Contact:

Using gambas in bash scripts

Post by BruceSteers »

Hello all.

Further to a recent post i put on the "Did you know" topic I have made something kind of cool for use with bash shell scripts.
It uses gbs3's stdin input method to make simple inline script functions written in gambas

Basically it's a set of requesters than can be very easily used in your scripts.
Attached is the test script that uses the functions.

This is the code....
#!/usr/bin/env bash

# <define commands>
GUI="gb.gui"

OpenFile() { DialogPath=$(echo -e 'Use "'$GUI'"\nDialog.Title="'$1'"\nDialog.Path="'$2'"\nIf Dialog.OpenFile() Then quit 1\nPrint Dialog.Path;'|gbs3); }
SaveFile() { DialogPath=$(echo -e 'Use "'$GUI'"\nDialog.Title="'$1'"\nDialog.Path="'$2'"\nIf Dialog.SaveFile() Then quit 1\nPrint Dialog.Path;'|gbs3); }
SelectDirectory() { DialogPath=$(echo -e 'Use "'$GUI'"\nDialog.Title="'$1'"\nDialog.Path="'$2'"\nIf Dialog.SelectDirectory() Then quit 1\nPrint Dialog.Path;'|gbs3); }
SelectFont() { DialogFont=$(echo -e 'Use "'$GUI'"\nDialog.Title="'$1'"\nDialog.Font=Font["'$2'"]\nIf Dialog.SelectFont() Then quit 1\nPrint Dialog.Font.Tostring();'|gbs3);}
SelectColor() { DialogColor=$(echo -e 'Use "'$GUI'"\nDialog.Title="'$1'"\nDialog.Color=Val(Replace("'$2'", "#", "&h"))\nIf Dialog.SelectColor() Then quit 1\nPrint Color.ToHTML(Dialog.Color);'|gbs3);}
Message() { if [ ! -z "$3" ]; then BUTS=", \"$3\""; fi; if [ ! -z "$4" ]; then BUTS="$BUTS, \"$4\""; fi; if [ ! -z "$5" ]; then BUTS="$BUTS, \"$5\""; fi; Message=$(echo -e 'use "'$GUI'"\nPrint Message.'$1'("'$2'"'$BUTS')'| gbs3); }
InputBox() { if [ ! -z "$1" ]; then MSG="\"$1\""; else MSG='"Enter text"'; fi;  if [ ! -z "$2" ]; then MSG="$MSG, \"$2\""; else MSG="$MSG, \"Enter Text\""; fi;  if [ ! -z "$3" ]; then MSG="$MSG, \"$3\""; fi;  InputBox=$(echo -e 'Use "'$GUI'"\nUse "gb.form"\nDim s As String = InputBox('$MSG')\nIf Not s Then Quit 1\nPrint s'| gbs3 -);}

# <End define commands>

########## Code below ##############

Message question "Message question" "Yes it is" "Oh no it isn\'t"
echo "Response: " $Message

InputBox "put text message here" "The window title"  "Default"
if [ $? -eq 0 ]; then echo "InputBox: $InputBox"; else echo "canceled"; fi

OpenFile "File open dialog" "$HOME/.config/user-dirs.dirs" 
if [ $? -eq 0 ]; then echo "File: $DialogPath"; else echo "canceled"; fi

SaveFile "File save requester" "$HOME/MyTestFile" 
if [ $? -eq 0 ]; then echo "File: $DialogPath"; else echo "canceled"; fi

SelectDirectory "Choose a dir" "$HOME"
if [ $? -eq 0 ]; then echo "folder: $DialogPath"; else echo "canceled"; fi

SelectFont "Get font" "Comic Sans MS,11,Bold"
if [ $? -eq 0 ]; then echo "Font: $DialogFont"; else echo "canceled"; fi

SelectColor "Get color" "#FF00EE"
if [ $? -eq 0 ]; then echo "Color: $DialogColor"; else echo "canceled"; fi



So putting one of the lines from between the # <define commands> in your script you can now have the one of the following commands in your bash script...

Message <type> <text> [<button1> <button2> <button3>]
echo "Response: " $Message

InputBox [ <Text message> <Title> <Default> ]
if [ $? -eq 0 ]; then echo "InputBox: $InputBox"; else echo "canceled"; fi

OpenFile <Title> <Path>
if [ $? -eq 0 ]; then echo "File: $DialogPath"; else echo "canceled"; fi

SaveFile <Title> <Path>
if [ $? -eq 0 ]; then echo "File: $DialogPath"; else echo "canceled"; fi

SelectDirectory <Title> <Path>
if [ $? -eq 0 ]; then echo "folder: $DialogPath"; else echo "canceled"; fi

SelectFont <Title> <Font>
if [ $? -eq 0 ]; then echo "Font: $DialogFont"; else echo "canceled"; fi

SelectColor <Title> <Color>
if [ $? -eq 0 ]; then echo "Color: $DialogColor"; else echo "canceled"; fi


Notes:
* The commands definition line must be before you use the command in a script.
* Dialog SelectColor could use a little more work to get better results.
* Message and InputBox set the respective variable $Message and $InputBox after use.
* Dialog commands set either $DialogPath, $DialogFont, or $DialogColor after use.
* if a Dialog is canceled then it returns error value 1
if [ $? -eq 1 ]; then echo "cancelled"; fi

Maybe you will find this useful?
It sure opens up many possibilities I think, being able to very quickly and easily use gambas scripts inside a bash script rocks.:)
Attachments
gbst.tar.gz
(858 Bytes) Downloaded 149 times
If at first you don't succeed , try doing something differently.
BruceS
User avatar
BruceSteers
Posts: 1579
Joined: Thursday 23rd July 2020 5:20pm
Location: Isle of Wight
Contact:

Re: Using gambas in bash scripts

Post by BruceSteers »

PS. by default it uses gb.gui but the toolkit can be changed in the first line GUI="gb.gui"

change to GUI="gb.qt5" to have it use qt5
If at first you don't succeed , try doing something differently.
BruceS
User avatar
BruceSteers
Posts: 1579
Joined: Thursday 23rd July 2020 5:20pm
Location: Isle of Wight
Contact:

Re: Using gambas in bash scripts

Post by BruceSteers »

I have done the following...

Removed all the test code below the # <End define commands> part so it is just the definitions and saved it as $HOME/gbst.

Now in any script i can just "source" the file and start to use it's functions

Like this for example...

#!/usr/bin/env bash

source ~/gbst

Message Info "Is it worth it?" "Yes"

GUI="gb.qt5"  # GUI variable can be changed at any time

OpenFile "Select a file" $HOME
echo "$DialogPath"

If at first you don't succeed , try doing something differently.
BruceS
Post Reply