How to use AutoIt in Ruby

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?

H- 16 wrote:

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

Marvin Gülker wrote:

H- 16 wrote:

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:
au3 | RubyGems.org | your community gem host

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?

H- 16 wrote:

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

Marvin

Marvin Gülker wrote:

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

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

On Fri, Apr 16, 2010 at 5:45 PM, Colin B.
[email protected] wrote:

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 = User32[ ‘MessageBoxA’, ‘ILSSI’ ]
result, = 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.

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

instead
require ‘dl’
User32 = DL.dlopen( ‘user32’ )

works in JRuby 1.8 and MRI Ruby 1.8.6, but not in 1.9.1;

User32_MsgBox = 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, =”
result, = User32_MsgBox.call(0, text, title, buttons + icon +
modality)
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 = User32[ ‘MessageBoxA’, ‘ILSSI’ ]
in ‘[]’: wrong number of arguments(2 for 1) (Argument Error)

On Wed, Apr 14, 2010 at 10:07 PM, James B. [email protected]
wrote:

Marvin Gülker wrote:

… 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.
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 = = WIN32OLE.new( “AutoItX3.Control” )

send_keys( “cc”, 1.5, “kty” ) sends “cc”, sleeps 1.5 seconds, sends

“kty”
def send_keys( *args )
args = args[ 0 ] if args.size == 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
gist:368632 · GitHub
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 = User32[ ‘MessageBoxA’, ‘ILSSI’ ]
result, = 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. 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. …
Windows Message Codes

That’s a useful thought and link. Thanks.

Colin B. wrote:

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