[Rubyosa-discuss] Finder scripting

Laurent Sansonetti lrz at chopine.be
Sun Oct 29 16:58:52 EST 2006


Hi Jean-Pierre,

On Oct 29, 2006, at 9:34 PM, jeanpierre at gmail.com wrote:

> i just cannot seem to wrap my head around using rubyosa to interact
> with the finder. i generated the lovely rdoc doumentation and read
> through it, but i am failing to understand how i would reference an
> arbitrary folder or file.
>
> say i want to move some file matz.png from /tmp to the user's
> documents folder...
>
> all i can come up with so far is:
>     finder = OSA.app('Finder')
>     file = '?' # somehow reference /tmp/matz.png
>     documents = finder.home.items.select {|i| i.name == 'Documents'}
>     file.move(documents)
>
> there must be a better way than selecting items right?
>

I plan to enhance the API to make this kind of requests easier.

> also, how does one do anything with a selection?
> OSA.app('Finder').selection returns an OSA::Element instance that
> doesn't seem to offer up any way to pull the actual items out.
>

The problem is that most of the API defined in the Finder's sdef is  
not typed (e.g. it uses the 'reference' type which means 'any class  
defined in the sdef'). So RubyOSA can't appropriately type the  
variables by default without actually sending an event, which we don't  
by default, for performances reasons.

So, by default, RubyOSA doesn't send an event if you do #selection.  
You can control this behavior with the OSA.lazy_events variable. For  
example:

$ irb -r rbosa
 >> OSA.lazy_events
=> true
 >> a = OSA.app('Finder')
=> #<OSA::Finder::Application:0x6ee2e0>
 >> a.selection
=> #<OSA::Element:0x14c21dc>

You can set lazy_events to false, then RubyOSA will send an apple  
event message within each method.

 >> OSA.lazy_events = false
=> false
 >> a.selection
=> [#<OSA::Finder::Folder:0x14bb4f4>]

When lazy_events is true, you can still explicitly resolve an object  
specifier using the #get method.

 >> OSA.lazy_events = true
=> true
 >> a.selection
=> #<OSA::Element:0x14b7c78>
 >> a.selection.get
=> [#<OSA::Finder::Folder:0x14b5c48>]

This remains me that I should document this... !

HTH,
Laurent

PS:

On a side note, there is another global parameter, `timeout', that  
controls the timeout of responses when sending apple events. By  
default we use the default timeout value which is a minute I believe,  
but you might want to set it to another value.

And there is an extra API to manage global parameters in a more- 
convenient way:

OSA.set_params(:timeout => 20, :lazy_events => false) do
	# Do some RubyOSA calls there
end

The parameters are set to given values within the life on the given  
block, then they are reset to the previous values.

I will make sure these are documented for the next release :-)


More information about the Rubyosa-discuss mailing list