iTerm and Ruby::OSA

M

Matt Mower

Hi.

I wonder if anyone has successfully gotten iTerm to open a new session
within a terminal window and execute commands using RubyOSA?

http://rubyosa.rubyforge.org/

I can do this from the Apple ScriptEditor but not using OSA. The
problem, in a nutshell, is that

iterm = OSA.app( 'iTerm' )

returns an instance of OSA::ITerm::Application where all of the useful
methods for working with terminals & sessions are in
OSA::ITerm::ITermApplication and I cannot seem to find out how to go
from one to the other. (My primary sources of info are the RubyOSA
docs, the output of rdoc-osa --name iTerm, and Google).

I note that this distinction is not visible in ScriptEditor and that
pointing OSA at another app (e.g. iTunes as in the examples for OSA)
seems to yield an immediately useful application object.

Is this perhaps a problem with iTerm not supporting introspection
properly (even if ScriptEditor can do it)? Or maybe a bug in RubyOSA?

If anyone can help me I'd be much obliged.

Regards,

Matt
 
H

has

Hi.

I wonder if anyone has successfully gotten iTerm to open a new session
within a terminal window and execute commands using RubyOSA?

http://rubyosa.rubyforge.org/

I can do this from the Apple ScriptEditor but not using OSA. The
problem, in a nutshell, is that

iterm = OSA.app( 'iTerm' )

returns an instance of OSA::ITerm::Application where all of the useful
methods for working with terminals & sessions are in
OSA::ITerm::ITermApplication and I cannot seem to find out how to go
from one to the other.

Unlike AppleScript, which is very more forgiving of imperfect
dictionaries, RubyOSA strictly enforces the structure and content
provided by application dictionaries. As a result, any bugs or quirks
in an application's dictionary may prevent RubyOSA from accessing some
or all of that application's scripting features.

Anyway, I think your best solution here would be to use rb-appscript -
see my sig for links. Appscript is designed to mimic the way that
AppleScript operates as closely as possible, so application
compatibility problems are rare. For example, to echo 'hello world' in
every session tab of the current terminal window:

require 'appscript'
include Appscript

Iterm = app('iTerm')

p Iterm.current_terminal.sessions.write:)text=>'echo "hello world"')

HTH

has
 
M

Matt Mower

Unlike AppleScript, which is very more forgiving of imperfect
dictionaries, RubyOSA strictly enforces the structure and content
provided by application dictionaries. As a result, any bugs or quirks
in an application's dictionary may prevent RubyOSA from accessing some
or all of that application's scripting features.

Thanks Has -- it does seem like Appscript is the way to go.

After some experimenting I came up with the following, that addresses
my need to create a new tab in the current iTerm window and execute a
command in it:

require 'rubygems'
require 'appscript'

iterm = Appscript::app('iTerm')

session = iterm.current_terminal.sessions.end.make( :new => :session )
session.exec( :command => "bash" )
session.write( :text => "echo 'Hello World'" )

Being unfamiliar with Appscript as well as AppleScript in general it
took me a few tries to to go from your example to making a new session
and executing a command. The key was to appreciate that Appscript
follows script dictionary closely and
uses the literal AppleScript parameters passed in hashes.

Many thanks again for your help and suggestion of using Appscript :)

Regards,

Matt
 
H

has

Being unfamiliar with Appscript as well as AppleScript in general it
took me a few tries to to go from your example to making a new session
and executing a command. The key was to appreciate that Appscript
follows script dictionary closely and
uses the literal AppleScript parameters passed in hashes.

If you've installed from gem, you might want to grab a copy of
the .zip distribution as well to get the documentation and examples
[1]. The appscript manual includes a tutorial that should help you get
started.

I'd also recommend getting ASDictionary and ASTranslate if you've not
already done so; see <http://rb-appscript.rubyforge.org/
downloads.html>.

HTH

has

[1] RubyGems does include all this stuff in the installed gem as well,
but you have to go hunting around your Ruby installation to find it
yourself.
 

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
473,982
Messages
2,570,185
Members
46,736
Latest member
AdolphBig6

Latest Threads

Top