Search code examples
rubyvariablesuser-interfaceshoes

Ruby: Green_shoes, assign list_box selection to a variable


I'm using Ruby/green_shoes to create a gui application. I want the user to be able to select a com port and then press a button to run the script.

It's nearly done, however I can't seem to take the highlighted com port and assign it to the variable 'port' so that it can be passed to my script when I press the button.

para "Select your COM port: "
list_box items: ["COM1", "COM2", "COM3", "COM4", "COM5", "COM6", "COM7", "COM8", "COM9", "COM10"], width: 120
@push = button "Begin Patching Process"
@push.click {
serial = SerialPort.new("#{port}", 9600)
serial.read_timeout = 200
serial.write("\x50\xBB\xFF\x01\x25\x98\x4D") #contains initialization to program
serial.readlines
serial.write("\x02") #requests firmware version
serial.readlines
serial.write("\x06")
serial.readlines
serial.write("\x58\x1f\xc0\x10\x00\x00\x00\x02\x02\x02\x01\x00\x00\x00\x01\x01\x34\x01\x80\x00") #contains VHF freqs (\x01\x34\x01\x80)
serial.readlines
serial.write("\x06\x58\x1f\xd0\x10\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x01\x04\x00\x05\x20\x00") #contains UHF freqs (x01\x04\x00\x05\x20)
serial.readlines
serial.write("\x06\x58\x1e\xe0\x10\x20\x20\x56\x46\x4f\x20\x20\x55\x4e\x4c\x4f\x43\x4b\x20\x00\x00") #contains pwr on msg (ASCII)
}
end

There is of course more code after this containing what to do when the button is pushed.

I just need to have it so that when I select COM7 for example.... the variable 'port' is equal to that selected com port from the list.

Working revisions below thanks to accepted answer @7stud

Shoes.app title: "BFB-251 VFO Patcher", width: 420, height: 120 do
background gray
@note = para "This Patcher is for use ONLY with Firmware BFB251"
#@note = para "This software does NOT confirm your model at this time."
#@note = para "Hold down the 3 key while powering on the unit to confirm it's version"
@note = para "I take no responsibility for any damanges or misuse."
para "Select your COM port: "

@my_list_box = list_box(
items: ["COM1", "COM2", "COM3", "COM7"], 
width: 120,) 
button "Begin Patching Process" do
port = @my_list_box.text
serial = SerialPort.new("#{port}", 9600)
serial.read_timeout = 200
serial.write("\x50\xBB\xFF\x01\x25\x98\x4D") #contains initialization to program
serial.readlines
serial.write("\x02") #requests firmware version
serial.readlines
serial.write("\x06")
serial.readlines
serial.write("\x58\x1f\xc0\x10\x00\x00\x00\x02\x02\x02\x01\x00\x00\x00\x01\x01\x34\x01\x80\x00") #contains VHF freqs (\x01\x34\x01\x80)
serial.readlines
serial.write("\x06\x58\x1f\xd0\x10\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x01\x04\x00\x05\x20\x00") #contains UHF freqs (x01\x04\x00\x05\x20)
serial.readlines
serial.write("\x06\x58\x1e\xe0\x10\x20\x20\x56\x46\x4f\x20\x20\x55\x4e\x4c\x4f\x43\x4b\x20\x00\x00") #contains pwr on msg (ASCII)
end
end

Solution

  • Untested because Green Shoes installation caused an error on Mac OSX 10.6.8:

    Shoes.app do
    
      para "Select your COM port: "
      @current_selection = para "No port selected" 
    
      my_list_box = list_box(
        items: ["COM1", "COM2", "COM3"], 
        width: 120,
      )
    
      my_list_box.change do |list_box|
        choice = list_box.text
        @current_selection.text = choice
        @port = choice
      end
    
    end
    

    Of course, you can do away with the @current_selection para, and just do @port = list_box.text.

    You can also specify the change block directly after the list_box() method call:

    Shoes.app do
    
      para "Select your COM port: "
      @current_selection = para "No port selected" 
    
      list_box(
        items: ["COM1", "COM2", "COM3"], 
        width: 120,
      ) do |my_list_box|
        choice = my_list_box.text
        @current_selection.text = choice
        @port = choice
      end
    
    end
    

    Response to comment:

    It sounds like you want to do something like this:

    Shoes.app do
    
      para "Select your COM port: "
    
      @my_list_box = list_box(
        items: ["COM1", "COM2", "COM3"], 
        width: 120,
      ) 
    
      button "Begin Patching Process" do
        port = @my_list_box.text
        #Do stuff with port here...
      end
    
    end
    

    According to the Green Shoes docs, @variables are available throughout the blocks in your app, so you just need to assign the result of calling list_box() to an @variable.