2

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 Shoes but i wonder how to do this in the browser. What is the simplest Ruby solution to do this ? So no exta gems or packages, no server that is allready running.. If a gem is needed, preferably one that works in Windows without problems.

Here the vbscript sample

Set web = CreateObject("InternetExplorer.Application")
If web Is Nothing Then
  msgbox("Error while loading Internet Explorer")
  Wscript.Quit
Else
  with web
    .Width = 300
    .Height = 175
    .Offline = True
    .AddressBar = False
    .MenuBar = False
    .StatusBar = False
    .Silent = True
    .ToolBar = False
    .Navigate "about:blank"
    .Visible = True
  end with
End If

'Wait for the browser to navigate to nowhere
Do While web.Busy
  Wscript.Sleep 100
Loop

'Wait for a good reference to the browser document
Set doc = Nothing
Do Until Not doc Is Nothing
  Wscript.Sleep 100
  Set doc = web.Document
Loop

'Write the HTML form
doc.Write "Give me a name<br><form><input type=text name=name ><input type=button name=submit id=submit value='OK' onclick='javascript:submit.value=""Done""'></form>"
Set oDoc = web.Document
Do Until oDoc.Forms(0).elements("submit").Value <> "OK"
  Wscript.Sleep 100
  If web Is Nothing or Err.Number <> 0 Then
    msgbox "Window closed"
    Wscript.Quit
  End If
Loop
name = oDoc.Forms(0).elements("name").value
oDoc.close
set oDoc = nothing
web.quit
set web = nothing
Wscript.echo "Hello " & name
echristopherson
  • 6,974
  • 2
  • 21
  • 31
peter
  • 41,770
  • 5
  • 64
  • 108

4 Answers4

5

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.

Justin Ko
  • 46,526
  • 5
  • 91
  • 101
2

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).

echristopherson
  • 6,974
  • 2
  • 21
  • 31
  • don't know about Camping but Rails is a whole framework, a lot of overkill for what i'm asking. Sinatra is better (i use it) but is in fact a kind of server, i search something that can be started and interpreted in the same script. i considered Win32OLE (i also us it a lot) but surely there must be something better and more Ruby like ? – peter Jul 30 '12 at 19:39
2

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.

Community
  • 1
  • 1
knut
  • 27,320
  • 6
  • 84
  • 112
  • see my comment at echristopherson, i'll try your script and keep you posted but i would prefer another solution – peter Jul 30 '12 at 19:33
  • i see, you use the rare vbscript dialogs here in an interesting manner, +1 for that but they are largely insufficient, therefore the use of the browser in vbscript as GUI, i'll take a look at the site too, thanks – peter Jul 30 '12 at 19:38
  • @peter To be honest, I don't use the vbscript dialogs. Up to now I use a shell (command prompt), often in combination with rake. But I marked the blog-entry in the past and I think it can be an answer for your question. – knut Jul 30 '12 at 19:47
1

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!

Xwris Stoixeia
  • 1,831
  • 21
  • 22
  • 1
    a GUI is a GRAPHICAL User Interface, what you suggest is a simple console text interface but i'll try the selenium solution, thanks – peter Jul 30 '12 at 19:34
  • the script fails, and i had one that used selenium that worked but now also fails, version problem with Firefox 14.0.1 ?C:/Ruby193/lib/ruby/gems/1.9.1/gems/selenium-webdriver-2.20.0/lib/selenium/webdriver/firefox/launcher.rb:77:in `connect_until_stable': unable to obtain stable firefox connection in 60 seconds (127.0.0.1:7055) (Selenium::WebDriver::Error::WebDriverError) But how would you pass and get values ? – peter Jul 30 '12 at 22:55
  • You said the simplest and no gems ;) In regards to the script failing have a look at this [link] (http://stackoverflow.com/questions/7263564/unable-to-obtain-stable-firefox-connection-in-60-seconds-127-0-0-17055). However it could be the version of Firefox, try switching to a lower version if the above does not work. I'm not sure mate what do you mean by 'how do you pass & get values' if you are saying about automation you can use an excel file and get/write data there.. – Xwris Stoixeia Jul 31 '12 at 08:58
  • well pass and get the things you wanna show (text, checkboxes, selectlists etc) and get the response. Tried it at work and discovered IE is not supported by selenium, FF en Chrome are blocked so Selenium is a noway go for me i'm afraid – peter Jul 31 '12 at 14:23