use browser as GUI in Ruby

后端 未结 4 2007
小鲜肉
小鲜肉 2020-12-10 00:09

In vbscript it is common to use the browser (IE) as a GUI. See the example below, it asks for a name and returns it to the script. In Ruby you have a few GUI\'s like Tcl and

相关标签:
4条回答
  • 2020-12-10 00:27

    You could use the Watir gem. The gem was originally intended to drive the IE browser, but would fit your need.

    To see:

    1) Install the Watir gem

    2) Create a test.htm file with the following:

    Give me a name<br>
    <form name="myForm" title="myForm">
        <input type="text" id="name" >
        <input id="submit" type="button" value="OK" onclick='document.myForm.submit.value="Done"'>
    </form>
    

    3) Run the following watir script, which will open the browser to your form. After you input the name and click [OK], the name is outputted. Note that you may need to change the location of the file in the script depending on where you saved your test.htm:

    require 'watir'
    
    b = Watir::IE.new
    begin
        b.goto('file:///C:/Documents%20and%20Settings/Setup/Desktop/test.htm')
        begin
            sleep(5)
        end until b.button(:id, 'submit').value != "OK"
        name = b.text_field.value
    ensure
        b.close
    end
    puts name
    

    I think this shows the general feasibility of doing what you want. Validation and dynamic creation of the forms would also be possible.

    0 讨论(0)
  • 2020-12-10 00:29

    Generally in Ruby people use something like Rails, Sinatra, or Camping to make web apps. Those all require gems. If you want something more similar to your VBscript example, without having to use gems, you can probably use Win32OLE (although I haven't tried it to open and interact with IE).

    0 讨论(0)
  • 2020-12-10 00:43

    win32ole is already mentioned.

    Here an example script:

    require 'win32ole' 
    def inputbox( message, title="Message from #{__FILE__}" )
      # returns nil if 'cancel' is clicked
      # returns a (possibly empty) string otherwise
      # hammer the arguments to vb-script style
      vb_msg = %Q| "#{message.gsub("\n",'"& vbcrlf &"')}"|
      vb_msg.gsub!( "\t", '"& vbtab &"' )
      vb_msg.gsub!( '&""&','&' )
      vb_title = %Q|"#{title}"|
      # go!
      sc = WIN32OLE.new( "ScriptControl" )
      sc.language = "VBScript"
      sc.eval(%Q|Inputbox(#{vb_msg}, #{vb_title})|)
      #~ sc.eval(%Q|Inputbox(#{vb_msg}, #{vb_title}, aa,hide)|)
    end
    
    #simple use
    res = inputbox "Your input please." 
    p res
    

    To give a message box you may use:

    require 'win32ole'
    def popup(message)
      wsh = WIN32OLE.new('WScript.Shell')
      wsh.popup(message, 0, __FILE__)
    end
    

    In http://rubyonwindows.blogspot.com/2007/04/ruby-excel-inputbox-hack.html (source of this examples) you find also a solution with Excel.

    0 讨论(0)
  • 2020-12-10 00:47

    Well I believe mate the simplest of GUI's for Windows is the humble command prompt. No need for gems and as far as I can see from the VBscript code above no need to open browsers or save the contents to excel or text file. So with your minimalistic specs ;) here you are..:

        puts "Give me a name" #output to cmd
        $name=gets.chomp #get a name from user 
    
        puts "Hello there..: #{$name}"
    

    The program above will use windows cmd as GUI and will get an input from the user and output it on the screen. Then if you want to use forms with buttons and stuff, make a simple website with a couple of forms and load it as following (requires one gem --> 'selenium-webdriver')

    require "selenium-webdriver"        #selenium lib
    driver = Selenium::WebDriver.for :firefox
    
    !30.times { if (driver.navigate.to("http://www.google.com") rescue false) then break else sleep 1; end }  #loop that will try 30times (once every sec to access the google.com)
    

    Then let me know if you require more on how to pass/read values from/to a file. Good luck man!

    0 讨论(0)
提交回复
热议问题