How to use AutoIt in Ruby

H

H- 16

I know for a fact that one can use AutoIt in Rubybut I don't know ho to
do it or how to even use it inside Ruby.

Has anyone actually done this?
 
M

Marvin Gülker

H- 16 said:
I know for a fact that one can use AutoIt in Rubybut I don't know ho to
do it or how to even use it inside Ruby.

Has anyone actually done this?

I once wrote a library for exactly that purpose, but until today I
didn't get much feedback. So, if you want to give it a try:
http://rubygems.org/gems/au3

Otherwise, stick to win32ole:
require "win32ole"
au3 = WIN32OLE.new("AutoItX3.Control")

Marvin
 
J

James Britt

Marvin said:
Otherwise, stick to win32ole:
require "win32ole"
au3 = WIN32OLE.new("AutoItX3.Control")


That's how I've used it. It's pretty straightforward.

I typically ended up writing a few helper methods for some of the
repetitive things that required sending multiple commands to autoitx, or
just to make the main code cleaner.



#---- example: sign in to library Web site ---
require 'win32ole'

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

# Yeah, it's kinda old ...
def browse_to url
@au3.Run "C:\\Program Files\\Mozilla Firefox2\\firefox.exe #{url}"
end

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

def tab
send "{TAB}"
end


def wait n
@au3.Sleep n.to_i
end

def send s
warn s
@au3.Send s.to_s
end

def wait_for_title t
@au3.WinWaitActive t.to_s
end

#---------------

cardnumber = '9670150903536'
pin = '90873'
set_up
browse_to 'http://libcat.scottsdaleaz.gov/patroninfo'
wait_for_title "Millennium Web Catalog"

# Make sure whole page has loaded over flakey WiFi
sleep 4

send cardnumber
tab
send pin
enter


#---------------


It's also handy to learn about the Windows message queue and sending
commands there. It makes it easier/more reliable to trigger actions
using, say, accelerator commands or keyboard shortcuts (e.g. ctrl+v)
instead of mouse clicks or menu navigation.

I had some code that hooked into Watir and took screenshots of each
page, pasted them into Paintbrush, then saved them to disk, but at the
moment I have no idea where that code is. But sending raw key messages
made it pretty easy to do.

http://www.autoitscript.com/autoit3/docs/appendix/WinMsgCodes.htm



--
James Britt

www.jamesbritt.com - Playing with Better Toys
www.ruby-doc.org - Ruby Help & Documentation
www.rubystuff.com - The Ruby Store for Ruby Stuff
www.neurogami.com - Smart application development
 
H

H- 16

Marvin said:
I once wrote a library for exactly that purpose, but until today I
didn't get much feedback. So, if you want to give it a try:
http://rubygems.org/gems/au3

Otherwise, stick to win32ole:
require "win32ole"
au3 = WIN32OLE.new("AutoItX3.Control")

Marvin

Thanks, I didn't know you needed the '.Control'.

I tried installing your gem but I need v. 1.9

I'm on a windows platform so now the question is how do I upgrade?
 
C

Colin Bartlett

That's how I've used it. =A0It's pretty straightforward.

I typically ended up writing a few helper methods for some of the repetit= ive
things that required sending multiple commands to autoitx, or just to mak= e
the main code cleaner.
Yes: I've found it useful to wrap AutoIt#Send to allow multiple
sending multiple keystrokes
with waiting periods between each "bunch" of keystrokes.

WinModule::AutoIt =3D =3D WIN32OLE.new( "AutoItX3.Control" )

# send_keys( "cc", 1.5, "kty" ) sends "cc", sleeps 1.5 seconds, sends "kt=
y"
def send_keys( *args )
args =3D args[ 0 ] if args.size =3D=3D 1 && args[ 0 ].kind_of?( Array =
)
args.each do | arg |
case arg
when String then AutoIt.Send( arg )
when Numeric then
if ! ( defined?( Complex ) && arg.kind_of?( Complex ) ) then
sleep arg
end
end
end
end

Other examples are here
http://gist.github.com/368632
which has some stuff which doesn't need Autoit,
and other stuff for which AutoIt is not needed
but for which *I* need AutoIt because I have not yet
found out how to do it without AutoIt.
For example this MsgBox code works in JRuby 1.4 and Ruby 1.8.6
but (it seems) not in Ruby 1.9.1 (at least not for me!):
User32_MsgBox =3D User32[ 'MessageBoxA', 'ILSSI' ]
result, =3D User32_MsgBox.call(0, text, title, buttons + icon + modality)

So if anyone knows how to use "dl" and
Win32API.new( 'user32', 'SendInput', 'IPI', 'I' ) to send keystrokes
(or can pop up a MsgBox in Ruby 1.9.1)
I'd be very interested to see how.
It's also handy to learn about the Windows message queue and sending
commands there. =A0It makes it easier/more reliable to trigger actions us= ing,
say, accelerator commands or keyboard shortcuts (e.g. ctrl+v) instead of
mouse clicks or menu navigation. ...
http://www.autoitscript.com/autoit3/docs/appendix/WinMsgCodes.htm

That's a useful thought and link. Thanks.
 
C

Colin Bartlett

For example this MsgBox code works in JRuby 1.4 and Ruby 1.8.6
but (it seems) not in Ruby 1.9.1 (at least not for me!):
=A0User32_MsgBox =3D User32[ 'MessageBoxA', 'ILSSI' ]
=A0result, =3D User32_MsgBox.call(0, text, title, buttons + icon + modali= ty)

So if anyone knows how to use "dl" and
Win32API.new( 'user32', 'SendInput', 'IPI', 'I' ) to send keystrokes
(or can pop up a MsgBox in Ruby 1.9.1)
I'd be very interested to see how.

Sorry: in case anyone is confused by that User32[...], the "proper" code is=
:

# note that Win32API is deprecated after Ruby 1.9.1 - use dl directly inste=
ad
require 'dl'
User32 =3D DL.dlopen( 'user32' )
# works in JRuby 1.8 and MRI Ruby 1.8.6, but not in 1.9.1;
User32_MsgBox =3D User32[ 'MessageBoxA', 'ILSSI' ]
# User32_MsgBox.call returns an array:
# [return_value, [0, text, title, buttons + icon + modality]]
# we only want the return value, hence "result, =3D"
result, =3D User32_MsgBox.call(0, text, title, buttons + icon + modal=
ity)
puts result

But I'd still be interested if anyone can get this to work in Ruby 1.9.1.
I get this error message from
User32_MsgBox =3D User32[ 'MessageBoxA', 'ILSSI' ]
in '[]': wrong number of arguments(2 for 1) (Argument Error)
 
M

Marvin Gülker

Colin said:
But I'd still be interested if anyone can get this to work in Ruby
1.9.1.

You could use the win32-api gem:
---------------------------------------
require "win32/api"

MessageBox = Win32::API.new("MessageBox", 'LPPI', 'I', "user32")
MessageBox.call(0, "Text here", "title here", 0)
---------------------------------------
The constants for style and return value remain the same.

The above code will get the ASCII function, if you want the UTF-16 +
extra-NUL-w-function use this:
---------------------------------------
MessageBox = Win32::API.new("MessageBoxW", 'LPPI', 'I', "user32")

title = "Title\0".encode("UTF-16LE")
text = "Text with ä\0".encode("UTF-16LE")

MessageBox.call(0, text, title, 0)
-------------------------------------

If I ommit the trailing NUL, I get some strange Chinese characters
displayed.

Disclaimer: This was written from memory, at the moment I haven't a
Windows machine to test. Tomorrow I can check if it works.

Marvin
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,156
Messages
2,570,878
Members
47,413
Latest member
KeiraLight

Latest Threads

Top