More questions on automation from naïve Windows user

Hi all,

I’m still working on making Ruby an automation language for the Mac and
along those lines I thought I’d check out what’s available on Windows. It
seems that profound ignorance of how Windows works is not an asset in this
endeavor. Please answer in a way that will convey something to a Windows
naïf if that is at all possible.

Ok, here are the questions:

  1. What is the relationship between ActiveScriptRuby and WIN32OLE? Is the
    latter useful by itself?

  2. How does the scripter know what capabilities the target application has
    under Windows? The example on page 168 of the PickAxe Book assumes a lot of
    knowledge on the part of the scripter about Excel’s internal objects. Mac
    Excel has an object browser so it’s easy to see what objects are inside
    Excel and to discover their methods. Does the same thing apply on Windows? I
    don’t have Win Excel but I do have IE and it’s supposedly scriptable. I just
    can’t discover the vocabulary to script it.

  3. The PickAxe book talks about “dynamic lookups.” Could someone provide
    some insight on how this works?

TIA,
-Chris

···


The secret of life is honesty and fair dealing. If you can fake that,
you’ve got it made. -Groucho Marx

Ok, here are the questions:

  1. What is the relationship between ActiveScriptRuby and WIN32OLE? Is the
    latter useful by itself?

The first is a stand-alone Ruby installation, while the latter is a loadable
module.
So, if you have, for example, the PragProg Windows version, and you install
ActiveScriptRuby, you end up with two separate versions of Ruby.

  1. How does the scripter know what capabilities the target application has
    under Windows? The example on page 168 of the PickAxe Book
    assumes a lot of
    knowledge on the part of the scripter about Excel’s internal objects. Mac
    Excel has an object browser so it’s easy to see what objects are inside
    Excel and to discover their methods. Does the same thing apply on
    Windows? I
    don’t have Win Excel but I do have IE and it’s supposedly
    scriptable. I just
    can’t discover the vocabulary to script it.

The VBA IDE that comes with Word, Excel, and other MS Office apps (or Visual
Studio IDE) provides very nice “IntelliSense ™”, which is a tremendous
help. Most of my knowledge of the MS Word DOM comes from hacking about with
VBA. However, there also used to be nice outlines and images of the Office
object models on msdn.microsoft.com. I don’t spend as much time on that
site as I used to :).

If you have the opportunity, try using the VBA IDE to write some code in
VBScript first, using the intelligence goodness, then port it to Ruby once
you know the names of objects and their method signatures.

  1. The PickAxe book talks about “dynamic lookups.” Could someone provide
    some insight on how this works?

Dotted paths are expensive. For example, if you have

foo = Object.OtherObject.AndYetAnother.some_method

then Object has to go get a reference to OtherObject, which in turn must go
get a reference to AndYetAnother, which then invokes some_method.

If you then have

bar = Object.OtherObject.AndYetAnother.some_new_method

that same work is done all over again. That’s why, for example, in VB it’s
better to use With

With Object.OtherObject.AndYetAnother
foo = some_method
bar = some_new_method
End

The code gets the reference to AndYetAnotehr once, and reuses it for
multiple calls.

In Ruby, with win32ole, it’s better to get a direct reference to the objects
you need, rather keep using dynamic lookup to get the object reference each
time.

James

···

TIA,
-Chris

The secret of life is honesty and fair dealing. If you can fake that,
you’ve got it made. -Groucho Marx

I do have IE and it’s supposedly scriptable. I just
can’t discover the vocabulary to script it.

msdn.microsoft.com is where I usually go to find stuff. Sometimes it’s hard
to find exactly where the stuff is, but it’s usually in there somewheres.
Googling msdn.microsoft.com is often handy. Entering
’site:msdn.microsoft.com internetexplorer.application’ brings up the
following link on the 1st shot:

http://msdn.microsoft.com/library/default.asp?url=/workshop/browser/webbrows
er/reference/objects/internetexplorer.asp

I’ve got some wrapper Ruby classes for IE that I use in acceptance testing
web apps. It’s not formally released anywhere yet, but you can scrounge for
it here:

http://cvs.sourceforge.net/cgi-bin/viewcvs.cgi/scrapware/scrapware/cliecontr
oller/

As far as ‘Intellisensing’ info out of an instance – I just played around
with this. You can get some stuff like this:

ie = WIN32OLE.new(‘InternetExplorer.Application’)
ie.ole_methods.each do |x| p x.name end

ole_methods is a method of the ie instance (btw, you can also do ie.methods
to get a list of the Ruby methods available), but I noticed it only returns
some methods and properties (as documented at the msdn.microsoft.com). I’m
not sure why other important methods (like Navigate) and props (like
Document) aren’t returned. Maybe Masaki Suketa is lurking and can answer
this for us.

Chris
http://clabs.org

~/data/rb/ole$ cat methods.rb

require 'win32ole’
object = WIN32OLE.new(ARGV[0])
puts object.ole_methods.sort

~/data/rb/ole$ ruby methods.rb InternetExplorer.Application

AddRef()
AddressBar
AddressBar=
Application
Busy
ClientToWindow()
Container
Document
ExecWB()
FullName
FullScreen


···

On Mon, 05 Aug 2002 20:59:16 GMT, Chris Gehlker gehlker@fastq.com wrote:

  1. How does the scripter know what capabilities the target application has
    under Windows? The example on page 168 of the PickAxe Book assumes a lot of
    knowledge on the part of the scripter about Excel’s internal objects. Mac
    Excel has an object browser so it’s easy to see what objects are inside
    Excel and to discover their methods. Does the same thing apply on Windows? I
    don’t have Win Excel but I do have IE and it’s supposedly scriptable. I just
    can’t discover the vocabulary to script it.

Unfortunately you don’t get the expected number or types for the
arguments, its best to jump into a MS IDE to see what they do (or
search for code fragments on the web).


Here’s an extract from a script which downloads stockmarket data from
a brokers site. (Unfortunately they charge for this now, so I don’t
use it). Should help in getting you started.

ie = WIN32OLE.new(‘InternetExplorer.Application’)
ie.visible = true

ie.navigate “https://www.sanford.com.au/sanford/Public/Home/Login.asp

sleep 1 while ie.Busy == true

document = ie.Document

sleep 1 while ie.Busy == true

document.loginform.username.value="username"
document.loginform.password.value="password"
document.loginform.submit()

@startDate.upto(@endDate) { |date|

sleep 1 while ie.Busy == true

ie.navigate
"http://www.sanford.com.au/sanford/Members/Research/HistoricalData.asp"


If you have the latest version of Win32OLE, then sample/olegen.rb might
help you. The olegen.rb read typelibs specified by argument, and create
some information.

ruby olegen.rb ‘Microsoft Internet Controls’

You can get all typelibs by WIN32OLE_TYPE.typelibs.

require 'win32ole’
WIN32OLE_TYPE.typelibs.each do |t|
puts t
end

Regards,
Masaki Suketa

···

In message “More questions on automation from naïve Windows user.” on 02/08/06, Chris Gehlker gehlker@fastq.com writes:

  1. How does the scripter know what capabilities the target application has
    under Windows? The example on page 168 of the PickAxe Book assumes a lot of
    knowledge on the part of the scripter about Excel’s internal objects. Mac
    Excel has an object browser so it’s easy to see what objects are inside
    Excel and to discover their methods. Does the same thing apply on Windows? I
    don’t have Win Excel but I do have IE and it’s supposedly scriptable. I just
    can’t discover the vocabulary to script it.

Ok, here are the questions:

  1. What is the relationship between ActiveScriptRuby and WIN32OLE? Is the
    latter useful by itself?

The first is a stand-alone Ruby installation, while the latter is a loadable
module.
So, if you have, for example, the PragProg Windows version, and you install
ActiveScriptRuby, you end up with two separate versions of Ruby.

Aha! Thanks.

  1. How does the scripter know what capabilities the target application has
    under Windows? The example on page 168 of the PickAxe Book
    assumes a lot of
    knowledge on the part of the scripter about Excel’s internal objects. Mac
    Excel has an object browser so it’s easy to see what objects are inside
    Excel and to discover their methods. Does the same thing apply on
    Windows? I
    don’t have Win Excel but I do have IE and it’s supposedly
    scriptable. I just
    can’t discover the vocabulary to script it.

The VBA IDE that comes with Word, Excel, and other MS Office apps (or Visual
Studio IDE) provides very nice “IntelliSense ™”, which is a tremendous
help. Most of my knowledge of the MS Word DOM comes from hacking about with
VBA. However, there also used to be nice outlines and images of the Office
object models on msdn.microsoft.com. I don’t spend as much time on that
site as I used to :).

Sounds like I just don’t have enough “stuff” If I Win Office or VS I’d be
home free.

If you have the opportunity, try using the VBA IDE to write some code in
VBScript first, using the intelligence goodness, then port it to Ruby once
you know the names of objects and their method signatures.

  1. The PickAxe book talks about “dynamic lookups.” Could someone provide
    some insight on how this works?

Dotted paths are expensive. For example, if you have

foo = Object.OtherObject.AndYetAnother.some_method

then Object has to go get a reference to OtherObject, which in turn must go
get a reference to AndYetAnother, which then invokes some_method.

I got that much. It’s the mechanism that remains obscure. I guess my
question boils down to this, "If Ruby can do these lookups at run time,
then how come it can’t do them at edit time? Why isn’t IntelliSense built
into Ruby on Windows?

Thanks for taking the time to answer so thoroughly.
-Chris

···

On 8/5/02 4:14 PM, “JamesBritt” james@jamesbritt.com wrote:

When I was a boy I was told that anybody could become President. Now I’m
beginning to believe it. -Clarence Darrow, lawyer and author (1857-1938)

~/data/rb/ole$ cat methods.rb

require 'win32ole’
object = WIN32OLE.new(ARGV[0])
puts object.ole_methods.sort

~/data/rb/ole$ ruby methods.rb InternetExplorer.Application

AddRef()
AddressBar

Huh - this didn’t work for me (well, it did, but I only got a partial list).
What version of WIN32OLE did you do this with?

Mine is:
C:\temp>irb
irb(main):001:0> require 'win32ole’
true
irb(main):002:0> WIN32OLE::VERSION
"0.4.5"

And what IE version?

Chris
http://clabs.org

Hey James and Chris, thanks for the informative responses.

I fired up Google and learned everything I need to know about COM. Thanks
again.

···


I have suffered from being misunderstood, but I would have suffered a hell
of a lot more if I had been understood. -Clarence Darrow, lawyer and author
(1857-1938)

Sorry, I have noticed this problem, but not fixed yet.

FYI, You can get Navigate information from typelibrary name (Microsoft
Internet Controls).

require 'win32ole’
WIN32OLE_TYPE.ole_classes(‘Microsoft Internet Controls’).each do |c|
c.ole_methods.sort{|m1, m2|
m1.name <=> m2.name
}.each do |m|
puts m.name
m.params.each do |p|
print " " + p.ole_type + " " + p.name
pinfo = []
pinfo.push “in” if p.input?
pinfo.push “out” if p.output?
pinfo.push “optional” if p.optional?
print “[” + pinfo.join(",") + “]” if !pinfo.empty?
print “(= #{p.default})” if p.default
puts "\n"
end
end
end

The sample/olegen.rb creates more detail information.

I use the latest version(0.4.8.1) of Win32OLE :wink:

Regards,
Masaki Suketa

···

In message “Re: More questions on automation from naïveWindows user.” on 02/08/06, “Chris Morris” chrismo@clabs.org writes:

ie = WIN32OLE.new(‘InternetExplorer.Application’)
ie.ole_methods.each do |x| p x.name end

ole_methods is a method of the ie instance (btw, you can also do ie.methods
to get a list of the Ruby methods available), but I noticed it only returns
some methods and properties (as documented at the msdn.microsoft.com). I’m
not sure why other important methods (like Navigate) and props (like
Document) aren’t returned. Maybe Masaki Suketa is lurking and can answer
this for us.

Wow! This is fantastic - don’t know how I ever overlooked this before. You
undersold this in your comment below – ‘some information’ is actually a
complete Ruby wrapper for the COM objects. Thanks very much.

Chris

···

----- Original Message -----
From: “Masaki Suketa” masaki.suketa@nifty.ne.jp

If you have the latest version of Win32OLE, then sample/olegen.rb might
help you. The olegen.rb read typelibs specified by argument, and create
some information.

ruby olegen.rb ‘Microsoft Internet Controls’

You can get all typelibs by WIN32OLE_TYPE.typelibs.

require 'win32ole’
WIN32OLE_TYPE.typelibs.each do |t|
puts t
end

Regards,
Masaki Suketa

Wow! This is fantastic - don’t know how I ever overlooked this before. You
undersold this in your comment below – ‘some information’ is actually a
complete Ruby wrapper for the COM objects. Thanks very much.

Chris

···

----- Original Message -----
From: “Masaki Suketa” masaki.suketa@nifty.ne.jp

If you have the latest version of Win32OLE, then sample/olegen.rb might
help you. The olegen.rb read typelibs specified by argument, and create
some information.

ruby olegen.rb ‘Microsoft Internet Controls’

You can get all typelibs by WIN32OLE_TYPE.typelibs.

require 'win32ole’
WIN32OLE_TYPE.typelibs.each do |t|
puts t
end

Regards,
Masaki Suketa

  1. The PickAxe book talks about “dynamic lookups.” Could
    someone provide

some insight on how this works?

Dotted paths are expensive. For example, if you have

foo = Object.OtherObject.AndYetAnother.some_method

then Object has to go get a reference to OtherObject, which in
turn must go
get a reference to AndYetAnother, which then invokes some_method.

I got that much. It’s the mechanism that remains obscure. I guess my
question boils down to this, "If Ruby can do these lookups at run time,
then how come it can’t do them at edit time? Why isn’t IntelliSense built
into Ruby on Windows?

The lookups aren’t done by Ruby, but by the COM objects being referenced.

Thanks for taking the time to answer so thoroughly.

Well, you got me curious abut this (and I have to do some writing on this
topic as well!)

I poked around, and used some Visual Studio tools to peer into ShDocVW (home
of IWebBrowser) and get the IDL.
I can sent it to you, if you like.

James

~/data/rb/ole$ cat methods.rb

require 'win32ole’
object = WIN32OLE.new(ARGV[0])
puts object.ole_methods.sort

~/data/rb/ole$ ruby methods.rb InternetExplorer.Application

AddRef()
AddressBar

Huh - this didn’t work for me (well, it did, but I only got a partial list).
What version of WIN32OLE did you do this with?

Mine is:
C:\temp>irb
irb(main):001:0> require 'win32ole’
true
irb(main):002:0> WIN32OLE::VERSION
"0.4.5"

Strange - although I’m using an earlier version:

~/data/rb$ ruby --version
ruby 1.6.5 (2001-09-19) [i386-cygwin]

~/data/rb$ irb
irb(main):001:0> require 'win32ole’
true
irb(main):002:0> WIN32OLE::VERSION
"0.3.0"

And what IE version?

Its worked with IE5.5 and 6.0 from memory.

···

On Tue, 06 Aug 2002 17:21:22 GMT, “Chris Morris” chrismo@clabs.org wrote:

Chris
http://clabs.org

I forgot to announce. This problem has been fixed in 0.4.9 or later.

Regards,
Masaki Suketa

···

In message “Re: More questions on automation from naïveWindows user.” on 02/08/08, Masaki Suketa masaki.suketa@nifty.ne.jp writes:

ie = WIN32OLE.new(‘InternetExplorer.Application’)
ie.ole_methods.each do |x| p x.name end

ole_methods is a method of the ie instance (btw, you can also do ie.methods
to get a list of the Ruby methods available), but I noticed it only returns
some methods and properties (as documented at the msdn.microsoft.com). I’m
not sure why other important methods (like Navigate) and props (like
Document) aren’t returned. Maybe Masaki Suketa is lurking and can answer
this for us.

Sorry, I have noticed this problem, but not fixed yet.

Strange - although I’m using an earlier version:

~/data/rb$ ruby --version
ruby 1.6.5 (2001-09-19) [i386-cygwin]

~/data/rb$ irb
irb(main):001:0> require 'win32ole’
true
irb(main):002:0> WIN32OLE::VERSION
"0.3.0"

I thought that might be the case after I looked further. Your ole_methods
returns an array of strings - mine returns an array of WIN32OLEMETHOD (or
somesuch) objects. I wonder why 0.4.5 doesn’t return all methods. I’ll check
with Masaki.

Chris