From: Colin Bartlett on
On Wed, Apr 14, 2010 at 10:07 PM, James Britt <james.britt(a)gmail.com> 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
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 = 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. ...
> http://www.autoitscript.com/autoit3/docs/appendix/WinMsgCodes.htm

That's a useful thought and link. Thanks.

From: Colin Bartlett on
On Fri, Apr 16, 2010 at 5:45 PM, Colin Bartlett <colinb2r(a)googlemail.com> 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)

From: Marvin Gülker on
Colin Bartlett 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
--
Posted via http://www.ruby-forum.com/.