Send keystrokes to Firefox or Safari?

S

Stedwick

Is there a way to send keystrokes to Firefox or Safari on Mac OS X
Leopard using Ruby? I don't want to remote control the web using watir
or anything, I just want to send keystrokes (ie. I want to be able to
type into a text box really fast hehe).

Anyone know how to do this? Thanks!
 
J

John Joyce

Is there a way to send keystrokes to Firefox or Safari on Mac OS X
Leopard using Ruby? I don't want to remote control the web using watir
or anything, I just want to send keystrokes (ie. I want to be able to
type into a text box really fast hehe).

Anyone know how to do this? Thanks!
You will need to use AppleEvents, specifically System Events, and
AppleScript.
You'll want to use rb-appscript as the Ruby library for AppleScript.
Get ready for a lot of stumbling in the dark...
 
7

7stud --

Philip said:
Is there a way to send keystrokes to Firefox or Safari on Mac OS X
Leopard using Ruby? I don't want to remote control the web using watir
or anything, I just want to send keystrokes (ie. I want to be able to
type into a text box really fast hehe).

Anyone know how to do this? Thanks!

You have to realize that Safari is a software program on your computer.
What you want to do involves mucking around in the internals of that
software program.

Watir is a completely different story. Watir merely mimics what Safari
does. Watir sends the same requests as Safari does out over the
internet and then receives the same responses as Safari.
 
S

Stedwick

Well, how about this: is there something like AutoIt for the Mac (if I
can control it with Ruby, so much the better)?

I tried rb-appscript already, but I couldn't figure out how to send
keystrokes =P
 
D

Dick Davies

You have to realize that Safari is a software program on your computer.
What you want to do involves mucking around in the internals of that
software program.

No, you just need to feed events into it via AppleScript or somesuch.
Watir is a completely different story. Watir merely mimics what Safari
does. Watir sends the same requests as Safari does out over the
internet and then receives the same responses as Safari.

No it doesn't - it scripts actions and drives Safari (or Internet Explorer, or
firefox). See:

http://safariwatir.rubyforge.org/safariwatir_script_0.2.0.mov
 
K

Karl von Laudermann

Well, how about this: is there something like AutoIt for the Mac (if I
can control it with Ruby, so much the better)?

I tried rb-appscript already, but I couldn't figure out how to send
keystrokes =P

There's also rubyosa as an alternative to rb-appscript. (But I have
not used it and couldn't tell you anything about it.)

Another option is to use JRuby, so you have access to Java's API
library, and then use Java's Robot class to send keystrokes. (http://
java.sun.com/j2se/1.5.0/docs/api/java/awt/Robot.html) Unlike with
Applescript, there's no way to send events to a specific app; you just
have to make sure that Safari is the active app at the time your
script "types" the keys.
 
H

has

Well, how about this: is there something like AutoIt for the Mac (if I
can control it with Ruby, so much the better)?

I tried rb-appscript already, but I couldn't figure out how to send
keystrokes =P

See: http://www.apple.com/applescript/uiscripting/index.html

The sample scripts given there are written for AppleScript, obviously,
but could be translated to Ruby with a little work. (Hint:
ASDictionary and ASTranslate are your friends.)

Example:

#!/usr/bin/ruby

begin; require 'rubygems'; rescue ImportError; end
require 'appscript'
include Appscript

app('Safari').activate
app('System Events').keystroke('Your text here')


GUI scripting is crude and fragile, but can be useful when dealing
with applications that don't provide a proper scripting API for the
features you need.

You can also script Safari directly, including executing your own
JavaScripts via its 'do JavaScript' command. (This is what SafariWatir
[http://safariwatir.rubyforge.org/] does.) Dunno about Firefox as I
don't use it myself, but maybe someone else will know.

HTH

has
 
S

Stedwick

app('System Events').keystroke('Your text here')

I think this is what I'm looking for, thanks!
 
K

Kajan Sittampalam

Hi,

how can i send keystokes to a flash object (i.e. Textbox) in Internet
explorer? at the moment all i can do is send words to normal HTML pages.
Thanks in advance.

Kajan.



hengist said:
Well, how about this: is there something like AutoIt for the Mac (if I
can control it with Ruby, so much the better)?

I tried rb-appscript already, but I couldn't figure out how to send
keystrokes =P

See: http://www.apple.com/applescript/uiscripting/index.html

The sample scripts given there are written for AppleScript, obviously,
but could be translated to Ruby with a little work. (Hint:
ASDictionary and ASTranslate are your friends.)

Example:

#!/usr/bin/ruby

begin; require 'rubygems'; rescue ImportError; end
require 'appscript'
include Appscript

app('Safari').activate
app('System Events').keystroke('Your text here')


GUI scripting is crude and fragile, but can be useful when dealing
with applications that don't provide a proper scripting API for the
features you need.

You can also script Safari directly, including executing your own
JavaScripts via its 'do JavaScript' command. (This is what SafariWatir
[http://safariwatir.rubyforge.org/] does.) Dunno about Firefox as I
don't use it myself, but maybe someone else will know.

HTH

has
 

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

Forum statistics

Threads
473,995
Messages
2,570,226
Members
46,815
Latest member
treekmostly22

Latest Threads

Top