Ruby in WinXP as an automation tool

Can anyone tell me if it can be done?

Hi Kevin,

Can anyone tell me if it can be done?

OLE object browser, in Ruby.

http://homepage1.nifty.com/markey/ruby/win32ole/index_e.html

Adelle.

Can you give examples of some tasks?

James

···

kevin.gc@gmail.com wrote:

Can anyone tell me if it can be done?

Yes.
This is not a very informative answer, I suppose. What kinds of things do you want to automate?

···

kevin.gc@gmail.com wrote:

Can anyone tell me if it can be done?

--
Best regards,

Alexey Verkhovsky

Ruby Forum: http://ruby-forum.org (moderator)
RForum: http://rforum.andreas-s.net (co-author)
Instiki: http://instiki.org (maintainer)

Hello,
AutoIT Does the job well. It has an application that will tell you
window names and lot of nice tools. It uses a vb like lang to program.
Becker

···

On 5/14/05, Alexey Verkhovsky <alex@verk.info> wrote:

kevin.gc@gmail.com wrote:

>Can anyone tell me if it can be done?
>
>
Yes.
This is not a very informative answer, I suppose. What kinds of things
do you want to automate?

--
Best regards,

Alexey Verkhovsky

Ruby Forum: http://ruby-forum.org (moderator)
RForum: http://rforum.andreas-s.net (co-author)
Instiki: http://instiki.org (maintainer)

http://www.rubygarden.org/ruby?AutoIt_For_Windows

martin

···

ruby talk <rubytalk@gmail.com> wrote:

Hello,
AutoIT Does the job well. It has an application that will tell you
window names and lot of nice tools. It uses a vb like lang to program.
Becker

Martin DeMello schrieb:

···

ruby talk <rubytalk@gmail.com> wrote:

Hello,
AutoIT Does the job well. It has an application that will tell you
window names and lot of nice tools. It uses a vb like lang to program.

http://www.rubygarden.org/ruby?AutoIt_For_Windows

I'm also using the AutoIT DLL driven by Ruby scripts. It really works great and already has saved me a lot of key-presses and mouse-clicks.

Regards,
Pit

Martin DeMello schrieb:
>
>>Hello,
>>AutoIT Does the job well. It has an application that will tell you
>>window names and lot of nice tools. It uses a vb like lang to program.
>
> http://www.rubygarden.org/ruby?AutoIt_For_Windows

I'm also using the AutoIT DLL driven by Ruby scripts. It really works
great and already has saved me a lot of key-presses and mouse-clicks.

Anybody have any samples of ruby/au3 scripts that are a bit more in
depth than the wiki example?

···

On 5/17/05, Pit Capitain <pit@capitain.de> wrote:

> ruby talk <rubytalk@gmail.com> wrote:

Regards,
Pit

--
Bill Guindon (aka aGorilla)

Bill Guindon wrote:

Anybody have any samples of ruby/au3 scripts that are a bit more in
depth than the wiki example?

No. But here's a script I just moved over to Ruby.

On a whim I started wrapping methods so that the core script would be a bit tidier.

# Launch Firefox and check gmail for that
# special v1agr4 offer
require 'win32ole'

def set_up
   @au3 = WIN32OLE.new("AutoItX3.Control")
   @au3.opt("WinTextMatchMode", 2)
end

def browse_to( url )
   @au3.Run( "e:\\Firefox\\firefox.exe #{url}" )
end

def enter
   @au3.Send( "{ENTER}" )
end

def tab
   @au3.Send( "{TAB}" )
end

def wait( n )
   @au3.Sleep( n.to_i )
end

def send s
   @au3.Send( s.to_s )
end

def wait_for_title t
   @au3.WinWaitActive( t.to_s )
end

set_up
name = 'my.name'
password = 'fake-password'
browse_to 'http://www.gmail.com'
wait_for_title "Welcome to Gmail"
# Make sure whole page has
# load over flakey WiFi
sleep 5
send name
tab
send password
enter
# The end.

James

I'm not using AutoIt, but in case it helps, here's a script I wrote today (inspired by this thread) that repeatedly opens different files with an application, and measures how much RAM the app is using.

I've simplified the code a bit from what I'm actually using. For example, I snipped out the bit which is testing a bunch of different builds of the app to check for variations between builds.

(Suggestions on cleaner ways to do things are welcome; I really don't understand OLE at all, or the WMI service. Although I did find this list[1] of properties which you can access on the processes returned by the InstancesOf call.)

class Fixnum
     def sample_average
         sum=0.0
         self.times{ sum += yield }
         sum/self
     end
end

class WIN32OLE
     def to_a
         a =
         self.each{ |p| a<<p }
         a
     end
end

require 'win32ole'

players = {
     '25139' => 'C:/Documents and Settings/gavin.kistner/Desktop/25139/AMPlayer.exe'
}

file_base = 'D:/QA/TestProjects/v30/Performance/MasterSlideAssetWithManySlides/'
files = %w| 1x1_10Assets.am 100x1_10Assets.am 1x100_10Assets.am 100x100_10Assets.am |

filepaths = {}
files.each{ |filename|
     filepaths[ filename ] = (file_base + filename)
}

stats = {}
samples_per_launch = 5
seconds_after_launch = 4
seconds_after_terminate = 1

players.each{ |player_name, exe|
     filepaths.each{ |filename, path|
         cmd = %Q|#{exe} "#{path}"|

         # Different launches use slightly different amounts of RAM
         # so launch it a few times and average the samples
         stats[ [player_name, filename ] ] = samples_per_launch.sample_average{

             # Run the command (open the file with the player) asynchronously
             IO.popen( cmd )

             # Wait to be sure it launched and initialized
             sleep seconds_after_launch

             # Find the process for the player
             mgmt = WIN32OLE.connect('winmgmts:\\\\.')
             player = mgmt.InstancesOf("win32_process").to_a.find{ |proc> proc.name =~ /AMPlayer.exe/ }

             # Figure out how much RAM it's using
             kbytes = player.workingSetSize.to_f / 1024

             # Kill it
             player.Terminate

             # Make sure it's really dead (not needed?)
             sleep seconds_after_terminate

             puts "#{player_name}:#{filename} :: #{kbytes}"

             # Return the measured RAM, for averaging
             kbytes
         }
     }
}

[1] Microsoft Learn: Build skills that open doors in your career

···

On May 17, 2005, at 3:28 PM, Bill Guindon wrote:

Anybody have any samples of ruby/au3 scripts that are a bit more in
depth than the wiki example?

--
"Despite the surge of power you feel upon learning Ruby,
resist the urge to trip others or slap them in the bald head.
DO NOT LORD YOUR RUBYNESS OVER OTHERS!"
- Why the Lucky Stiff

Bill Guindon schrieb:

Anybody have any samples of ruby/au3 scripts that are a bit more in
depth than the wiki example?

Hi Bill,

I doubt that it is of any use to you, but here's a script I use when developing stored procedures for an Oracle database. There are no comments, so feel free to ask if you need more information.

   def main
     start_automation
     if toad_running?
       activate_toad
       switch_away_from_output if output_showing?
       execute_code
       if package_state_changed?
         remove_error_window
         switch_to_output
         restart_output
         switch_away_from_output
         execute_code
       end
       switch_to_output unless error_window?
     end
   end

   TOAD = "FREE TOAD"
   OUT = "DBMS Output"
   ERR = "TOAD Error"

   def start_automation
     require "win32ole"
     @ai = WIN32OLE.new( "AutoItX3.Control" )
   end

   def toad_running?
     @ai.WinExists( TOAD ) > 0
   end

   def activate_toad
     @ai.WinActivate( TOAD )
     @ai.WinWaitActive( TOAD )
   end

   def output_showing?
     window_title =~ /#{OUT}/
   end

   def switch_away_from_output
     switch_with( "^{TAB}" )
   end

   def switch_to_output
     switch_with( "^+{TAB}" )
   end

   def restart_output
     switch_with( "^{F4}" )
     switch_with( "!DD" )
   end

   def execute_code
     @ai.Send( "^{ENTER}" )
     sleep 0.2 while @ai.MouseGetCursor == 0
   end

   def package_state_changed?
     sleep 0.2
     error_window? && window_text =~ /ORA-04068/
   end

   def error_window?
     window_title =~ /#{ERR}/
   end

   def remove_error_window
     switch_with( "{ESC}" )
   end

   def switch_with( cmd )
     title = window_title
     @ai.Send( cmd )
     sleep 0.2 while title == window_title
   end

   def window_title
     @ai.WinGetTitle( "" )
   end

   def window_text
     @ai.WinGetText( "" )
   end

   main

Regards,
Pit

Thanks much for the example, gives me a good place to start.

···

On 5/17/05, James Britt <james_b@neurogami.com> wrote:

Bill Guindon wrote:
> Anybody have any samples of ruby/au3 scripts that are a bit more in
> depth than the wiki example?

No. But here's a script I just moved over to Ruby.

On a whim I started wrapping methods so that the core script would be a
bit tidier.

--
Bill Guindon (aka aGorilla)

Ooh, and I just found this url[2] which links to some tasty scripts showing how to use WMI to automate all sorts of tasks on Windows. (The examples are in VBScript, but I think that it should be relatively easy to directly port them to Ruby, given the code I already supplied. And possibly this page[3])

[2] Microsoft Learn: Build skills that open doors in your career
[3] Microsoft Learn: Build skills that open doors in your career

···

On May 17, 2005, at 10:53 PM, Gavin Kistner wrote:

(Suggestions on cleaner ways to do things are welcome; I really don't understand OLE at all, or the WMI service. Although I did find this list[1] of properties which you can access on the processes returned by the InstancesOf call.)

By the way, if I have a .dll (such as AutoIt) somewhere in my_project/ext, and I want it to work in "download, start, No Step Three (TM)" manner - no changes to Windows registry, no copying of files to c:\WINNT, no other installation to speak of.

In this case, what should this line be preceeded with:
  @au3 = WIN32OLE.new("AutoItX3.Control")

?

···

On 5/17/05, James Britt <james_b@neurogami.com> wrote:

No. But here's a script I just moved over to Ruby.
   

--
Best regards,

Alexey Verkhovsky

Ruby Forum: http://ruby-forum.org (moderator)
RForum: http://rforum.andreas-s.net (co-author)
Instiki: http://instiki.org (maintainer)

Alexey Verkhovsky wrote:

No. But here's a script I just moved over to Ruby.
  
By the way, if I have a .dll (such as AutoIt) somewhere in my_project/ext, and I want it to work in "download, start, No Step Three (TM)" manner - no changes to Windows registry, no copying of files to c:\WINNT, no other installation to speak of.

In this case, what should this line be preceeded with:
@au3 = WIN32OLE.new("AutoItX3.Control")

Good question. The DLL is going to get loaded, I think, by the WIN32OLE wrapper, no? So it needs to know where t look, or the dll has to be someplace it knows to look.

Does the AutoIT.dll have to be registered (via regsvr32 or something) before win32ole can use it?

Time to go experiment ...

James

···

On 5/17/05, James Britt <james_b@neurogami.com> wrote:

James Britt wrote:

Good question. The DLL is going to get loaded, I think, by the WIN32OLE wrapper, no? So it needs to know where t look, or the dll has to be someplace it knows to look.

That's my question, in a more pointed version: how do I tell win32ole to look at my_project/ext?

···

--
Best regards,

Alexey Verkhovsky

Ruby Forum: http://ruby-forum.org (moderator)
RForum: http://rforum.andreas-s.net (co-author)
Instiki: http://instiki.org (maintainer)

Alexey Verkhovsky wrote:

James Britt wrote:

Good question. The DLL is going to get loaded, I think, by the WIN32OLE wrapper, no? So it needs to know where t look, or the dll has to be someplace it knows to look.

That's my question, in a more pointed version: how do I tell win32ole to look at my_project/ext?

I believe win32ole is going to want to find the COM automation info in the registry, and you'll need to run

regsvr32.exe AutoItX3.dll

as part of the installation process. But the DLL can go wherever you like; the location is stored when it gets registered.

James

···

--

http://catapult.rubyforge.org
http://orbjson.rubyforge.org
http://ooo4r.rubyforge.org
http://www.jamesbritt.com

James Britt wrote:

I believe win32ole is going to want to find the COM automation info in the registry, and you'll need to run

regsvr32.exe AutoItX3.dll

as part of the installation process. But the DLL can go wherever you like; the location is stored when it gets registered.

Sigh... as I feared.
How do I query/edit the registry from within Ruby?

···

--
Best regards,

Alexey Verkhovsky

Ruby Forum: http://ruby-forum.org (moderator)
RForum: http://rforum.andreas-s.net (co-author)
Instiki: http://instiki.org (maintainer)

Alexey Verkhovsky wrote:

James Britt wrote:

I believe win32ole is going to want to find the COM automation info in the registry, and you'll need to run

regsvr32.exe AutoItX3.dll

as part of the installation process. But the DLL can go wherever you like; the location is stored when it gets registered.

Sigh... as I feared.
How do I query/edit the registry from within Ruby?

For what? If the goal is to register the DLL, just use, for example:

# unreg
puts `regsvr32 AutoItX3.dll /c /s /u`

# reg
puts `regsvr32 AutoItX3.dll /c /s `

http://www.ss64.com/nt/regsvr32.html

James

···

--

http://catapult.rubyforge.org
http://orbjson.rubyforge.org
http://ooo4r.rubyforge.org
http://www.jamesbritt.com