Mark C. wrote:
I’m new to both Ruby and OS X. What I’m aiming for is to rename some
files that have been highlighted in Finder. How do I do it? So far I’ve
got
require ‘rubygems’
require ‘rbosa’
finder = OSA.app(‘Finder’)
w = finder.window
Scriptable Mac apps are notorious for their inadequate API
documentation, but for all its shortcomings the built-in dictionary is
still the best place to start. e.g. If you open Finder’s dictionary in
Script Editor (only supports AppleScript-style formatting, but it’s
already on your system), you’ll see that ‘window’ is an element (i.e.
one-to-many relationship) of the root ‘application’ object, and elements
are always written in the plural in RubyOSA, e.g. w = finder.windows[0].
That said, to get the current selection, you actually need to refer to
the ‘application’ object’s ‘selection’ property
BTW, note that RubyOSA no longer appears to be actively developed or
supported (I suspect Laurent has moved on to bigger and better things
since Apple decided not to include RubyOSA in Leopard). I’d recommend
using rb-appscript (see my sig) which aside from being actively
supported is better featured and more reliable than RubyOSA/SB. Example:
require ‘appscript’
include Appscript
Finder = app(‘Finder’)
Finder.selection.get.each do |ref|
ref.name.set(“renamed #{ref.name.get}”)
end
The ASDictionary application on the appscript website can be used to
export appscript-style application dictionaries in HTML format, and the
accompanying ASTranslate tool is very handy when you want to know how to
translate an application command from AppleScript syntax to its
appscript equivalent.
As a bonus, with ASDictionary installed, you can use appscript’s
built-in #help method to explore an application’s dictionary
interactively, e.g.:
$ irb
require ‘appscript’; include Appscript
=> Object
f = app(‘Finder’)
=> app(“/System/Library/CoreServices/Finder.app”)
f.help
==============================================================================
Help (-t)
Reference: app(“/System/Library/CoreServices/Finder.app”)
Description of reference
Terminology for application class
Class: application – The Finder
See also:
Finder Basics
Properties:
clipboard (r/o) : reference – (NOT AVAILABLE YET) the Finder’s
clipboard
window
name (r/o) : international_text – the Finder’s name
visible : boolean – Is the Finder’s layer visible?
frontmost : boolean – Is the Finder the frontmost process?
selection : reference – the selection in the frontmost Finder
window
…
=> app(“/System/Library/CoreServices/Finder.app”)
HTH
has
Control AppleScriptable applications from Python, Ruby and ObjC:
http://appscript.sourceforge.net