# GUI scripting

**URL:** <https://forum.crystal-lang.org/t/gui-scripting/2131>\
**Category:** News\
**Created:** [May 18, 2020, 10:37pm UTC](https://forum.crystal-lang.org/t/gui-scripting/2131 "2020-05-18T22:37:35Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![pebauer68](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/pebauer68/32/1008_2.png) [@pebauer68](https://forum.crystal-lang.org/u/pebauer68)\
**Post date:** [May 18, 2020, 10:37pm UTC](https://forum.crystal-lang.org/t/gui-scripting/2131/1 "2020-05-18T22:37:35Z")

</div>

The [Gambas](http://gambas.sourceforge.net/en/main.html) project has a nice jitted scripter which can be called from crystal for small GUIs, here just a [textarea](http://gambaswiki.org/wiki/comp/gb.qt4/textarea?nh). Lets see if we can talk to this [‘alien’ objects](https://gambaswiki.org/wiki/doc/object-model) and [types](http://gambaswiki.org/wiki/lang/type?nh) in a more direct way.  
Should we call this crybas ? Hope it is useful.

[![](https://1.bp.blogspot.com/-0t3lT5DdloE/XsMLHbIBmiI/AAAAAAAAl9Q/72CPZY7adb8niMdga7ZnZseOxxXp0g3BwCLcBGAsYHQ/s1600/crybas1.png) ](https://1.bp.blogspot.com/-0t3lT5DdloE/XsMLHbIBmiI/AAAAAAAAl9Q/72CPZY7adb8niMdga7ZnZseOxxXp0g3BwCLcBGAsYHQ/s1600/crybas1.png)

> **[Crystal Language - GUI scripting](https://www.bitkistl.com/2020/05/crystal-language-gui-scripting.html)**
>
> Blog about: Linux, ARM boards, Software

```
#Define GUI in an heredoc
gui =<<-'GUI'
use "gb.form"
DIM Line$ As String
DIM myForm AS Form
With myForm = New Form
   .Title = "Text Viewer" 
   .Arrangement = Arrange.Fill 
   .AutoResize = False
   .width = 600
   .height = 300
   .show
End with
DIM myText As New TextArea(myForm)
file.in.blocking = false
DO
  Line$ = Read -1024 'read up to 1024 Bytes from stdin
  myText.Insert(Line$) 'insert text into text area
  wait 0.01 'give time to Gambas events
LOOP
GUI
File.write("./gui.gbs",gui)
spawn do; begin  
    cmd = "gbs3 ./gui.gbs"
    Process.run(cmd, shell: true) do | p |
         p.input.puts("hello textarea")     
         p.input.puts("crystal+gambas = crybas")                
       end 
    end
end
sleep
```

---

<div class="post-metadata">

**Author:** ![Blacksmoke16](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/blacksmoke16/32/1241_2.png) [@Blacksmoke16](https://forum.crystal-lang.org/u/Blacksmoke16)\
**Post date:** [May 18, 2020, 11:58pm UTC](https://forum.crystal-lang.org/t/gui-scripting/2131/2 "2020-05-18T23:58:19Z")

</div>

It would probably be more efficient to write some C bindings to it (if possible) rather than spawning a sub process. Given most higher level languages can do this I’m not sure what you’re trying to show here? Not really getting any benefit from using Crystal in this case.

---

<div class="post-metadata">

**Author:** ![pebauer68](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/pebauer68/32/1008_2.png) [@pebauer68](https://forum.crystal-lang.org/u/pebauer68)\
**Post date:** [May 19, 2020, 6:24am UTC](https://forum.crystal-lang.org/t/gui-scripting/2131/3 "2020-05-19T06:24:11Z")

</div>

The benefit could be:  
-to re-use a [VB like GUI designer](https://www.bitkistl.com/2018/12/gambas-3120.html) for crystal projects  
-or just script small GUIs when you do not need the GUI designer(just 2 or 3 GUI elements)  
Crystals semantik and syntax is much more compact and modern. I dont want to re-envent the wheel. I need an object bridge between the crystal \<-\> gambas side. I can imagine to write a text based object bridge(key,value pairs) for GUI objects and GUI events based on sockets or pipes in some evenings.

---

<div class="post-metadata">

**Author:** ![pebauer68](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/pebauer68/32/1008_2.png) [@pebauer68](https://forum.crystal-lang.org/u/pebauer68)\
**Post date:** [May 21, 2020, 8:07am UTC](https://forum.crystal-lang.org/t/gui-scripting/2131/4 "2020-05-21T08:07:35Z")

</div>

[Setting Properties](http://gambaswiki.org/wiki/comp/gb/object/setproperty?ht=setproperty) of a Gambas GUI Object like: Textarea1.Text=“some text”  
already works. This is done by doing live object introspection by the received object name e.g. a Textarea has 106 symbols for properties,methods and events

Received data \<–  
Textarea1.Text,2020-05-21 11:46:37 +02:00  
Key,val: Textarea1.Text 2020-05-21 11:46:37 +02:00

```
Objname: Textarea1
Prop: Text
(TextArea 0x55e41302e6c8)
TextArea1:
.,H,W,X,Y,Id,Cut,Pos,Tag,Top,_new,Copy,Drag,Drop,Font,Grab,Hide,Left,Line,Move,Name,Next,Redo,Show,Text,Undo,Wrap,:Drag,:Drop,:Menu
,Clear,Lower,Mouse,Paste,Proxy,Raise,ToPos,Width,:Enter,:Leave,Action,Border,Column,Cursor,Delete,Design,Expand,Handle,Height,Ignor
e,Insert,Length,Parent,Resize,Select,ToLine,Window,:Change,:Cursor,_Family,_Parent,Enabled,Hovered,Refresh,ScreenX,ScreenY,Tooltip,
Visible,:MouseUp,_Similar,CursorAt,HasFocus,Previous,ReadOnly,Reparent,Selected,SetFocus,ToColumn,Tracking,Unselect,:DblClick,:Drag
Move,:GotFocus,:KeyPress,Alignment,PopupMenu,ScrollBar,SelectAll,Selection,:DragLeave,:LostFocus,:MouseDown,:MouseDrag,:MouseMove,_
IsControl,Background,Foreground,MoveScaled,NoTabFocus,:KeyRelease,:MouseWheel,_Properties,_DefaultSize,ResizeScaled,_DefaultEvent,E
nsureVisible,
Symbols found: 106

```

**Events for Button pressed** are working, I just send “Button nn Clicked” to crystal.

```
#connect to Gambas GUI via TCP
require "socket"
GUI_SERVER="localhost"
GUI_PORT=9090
class GuiClient
  def send (object,value)
    client = TCPSocket.new(GUI_SERVER,GUI_PORT)
    client << object + "," + value + "\n"
    response = client.gets
    client.close
    puts 
  end    
end
mygui = GuiClient.new
#send key,value pairs for GUI Objects based on GTK or QT 
mygui.send "Textarea1.Text","hello-textarea"
sleep 5.seconds
mygui.send "Textarea1.Text","hello-textarea-more-text"
loop do 
    mygui.send "Textarea1.Text",Time.local.to_s
    sleep 2.seconds
end
sleep

Event for Button clicks forwarded like this:
def receive
    spawn do #receiving fiber,handle GUI events
        loop do    
            event = @client.not_nil!.gets
            case event 
            when "Button1 clicked"
              @sending=false
            when "Button2 clicked"
              @sending=true    
            end
          end
        end
    end
end
```

---

<div class="post-metadata">

**Author:** ![pebauer68](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/pebauer68/32/1008_2.png) [@pebauer68](https://forum.crystal-lang.org/u/pebauer68)\
**Post date:** [May 22, 2020, 8:56am UTC](https://forum.crystal-lang.org/t/gui-scripting/2131/5 "2020-05-22T08:56:07Z")

</div>

A Gambas TCP GUI Server Demo App is available.  
Download: [https://gambas.one/gambasfarm/?id=821&action=search](https://gambas.one/gambasfarm/?id=821&action=search)  
Connect with any Telnet client or with your Crystal App.  
[https://www.bitkistl.com/2020/05/gambas3-gui-server.html](https://www.bitkistl.com/2020/05/gambas3-gui-server.html)

> **[The Crybas Demo App](https://www.bitkistl.com/2020/05/the-crybas-demo-app.html)**
>
> Blog about: Linux, ARM boards, Software
