在Ruby中使用浏览器作为GUI

在vbscript中,通常使用浏览器(IE)作为GUI。 请参阅下面的示例,它要求输入名称并将其返回给脚本。 在Ruby中你有一些像Tcl和Shoes的GUI,但我想知道如何在浏览器中这样做。 什么是最简单的Ruby解决方案? 所以没有exta gems或package,没有已经运行的服务器..如果需要gem,最好是在Windows中运行而没有问题。

这里是vbscript示例

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
" 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

你可以使用Watir gem。 该gem原本打算用于驱动IE浏览器,但它可以满足您的需求。

查看:

1)安装Watir gem

2)使用以下命令创建test.htm文件:

 Give me a name

3)运行以下watir脚本,它将打开浏览器到您的表单。 输入名称后单击[确定],输出名称。 请注意,您可能需要根据保存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 

我认为这显示了做你想做的事情的一般可行性。 还可以validation和动态创建表格。

通常在Ruby中,人们使用Rails,Sinatra或Camping之类的东西来制作Web应用程序。 这些都需要gem。 如果你想要一些更类似于VBscript示例的东西,而不必使用gems,你可以使用Win32OLE(尽管我还没有尝试过打开它并与IE交互)。

嗯,我相信配对最简单的用于Windows的GUI是简单的命令提示符。 不需要gem,据我所知,从上面的VBscript代码中无需打开浏览器或将内容保存到excel或文本文件。 所以你的简约规格;)在这里你是..:

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

上面的程序将使用Windows cmd作为GUI,并将从用户获得输入并将其输出到屏幕上。 然后,如果你想使用带有按钮和东西的表单,创建一个包含几个表单的简单网站并加载如下(需要一个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) 

如果您需要更多关于如何从/向文件传递/读取值的信息,请告诉我。 祝你好运!

已经提到过 win32ole

这是一个示例脚本:

 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 

要给出一个消息框,您可以使用:

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

在http://rubyonwindows.blogspot.com/2007/04/ruby-excel-inputbox-hack.html (此示例的来源)中,您还可以找到Excel的解决方案。