Archive for the 'RubyOSA' Category

All About RubyOSA

Have you heard of RubyOSA? It is a free software that retrieves the scriptable definition of of a given application and populates a new Ruby namespace with classes, methods, constants, enumerations, and all other elements described by the terminology.

For better understanding, most Mac OS X applications are scriptable, and they define their scriptable interface in an XML format. RubyOSA parses this file and creates the Ruby API on the fly. This API will do the necessary AppleEvent work transparently for you (building and sending events).

RubyOSA can be an alternative to the RubyAEOSA project. The latter is more a set of Ruby bindings for the AppleEvent C API, while RubyOSA is a more high-level framework as the AppleEvent infrastructure is completely hidden.

First Steps through Ruby on Rails


Image Source:www.flickr.com

Classes, attributes, methods, and objects are the core of any Object-Oriented language. I am going to discuss how these classes, attributes, methods etc can be implemented in Ruby. There are two types of classes ‘close-ended’ and ‘open-ended’. If a class is close-ended, then new functionalities cannot be added to it without inheriting the class.
If a class is open-ended, then new functionalities can be added to it without inheriting it. Ruby is open-ended.
The class declared and defined just now is an empty class as it doesn’t contain any attributes or methods. You will notice that the procedure to define methods in ruby is very simple. def is the keyword used to define methods.
In ruby we don`t use brackets to define a set of code and we don`t use any semicolon at the end of the statements.

Controlling iTunes Using RubyOSA

Did you know that RubyOSA provides a bridge from Ruby to the Apple Event Manager? It allows Ruby programs to automate Mac OS X applications in the same way as AppleScript.

Here’s an example code in controlling iTunes:

require ‘rbosa’
itunes = OSA.app(‘iTunes’)

track = itunes.current_track
p track #
p track.name # “Over The Rainbow”
p track.artist # “Keith Jarrett”
p track.duration # 362.368988037109
p track.date_added.to_s # “2006-06-30″
p track.enabled? # true

# Play the selected track.
itunes.play

# Fade the volume.
100.times { |i| itunes.sound_volume = i; sleep 0.1 }

# Set iChat’s status message to the current track.
OSA.app(‘iChat’).status_message = “Playing: #{track.name}”

Come on and try this out! :)

Getting an Application Object in RubyOSA

In the previous post, you can locate an application by passing its name to OSA.app(). However, there are also several other ways.

You can also locate an application by providing its signature (as a four character string), or bundle ID:

OSA.app(:signature => ‘hook’)

OSA.app(:bundle_id => ‘com.apple.iTunes’)

If the application you want to control hasn’t been installed in one of the standard applications locations, RubyOSA may have some trouble locating it. You can provide the full path of the application to work around this:

OSA.app(:path => ‘/Somewhere/Applications/MyApp.app’)

Note that OSA.app(‘iTunes’) is a convenience shortcut to OSA.app(:name => ‘iTunes’).

Getting Started with RubyOSA

RubyOSA is a bridge between Ruby and the Open Scripting Architecture (OSA). The key idea is to create a Ruby API on the fly based on a target application’s scriptable definition.

Once you target an application with RubyOSA you get a special kind of proxy object, which is the entry point of the generated Ruby API.

Let’s start with something easy:

require ‘rbosa’ # (1)
app = OSA.app(‘iTunes’) # (2)
puts app.current_track.name # (3)
First, we need to require the RubyOSA bridge

**Note that if you installed RubyOSA via RubyGems you might want to require ‘rubygems’ before.