OSX Finder selected files

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

but I’m getting
NoMethodError: undefined method ‘window’ for
<OSA::Finder::Application:0x548ee0
desc="‘sign’($5343414D$)">:OSA::Finder::Application

:frowning:

I’m all at sea.

Mark C. [email protected] writes:

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.

,----
| require ‘rubygems’
| require ‘rbosa’
|
| finder = OSA.app(‘Finder’)
|
| finder.selection.each do |item|
| # path to file, using URL notation
| puts “file=” + item.url
|
| # Rename the file
| item.name = ‘new-name’
| end
`----

You can also use rdoc-osa to generate documentation for a scriptable
application:

,----
| rdoc-osa --name Finder
`----

Finally, you might want to think about subscribing to the RubyOSA
mailing list: http://rubyforge.org/mailman/listinfo/rubyosa-discuss

Good luck.

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

In addition, before going terribly far with OS X scripting outside of
AppleScript itself, you’ll do well to learn some AppleScript first to
understand how it works (and how it doesn’t).
Matt Neuberg’s book on AppleScript will teach you a lot of what it
can and cannot do and the how and why.
Other books may be gentler introductions/tutorials and will be
useful, but Matt’s explanations of the frustrations and workflow will
be very reassuring.
That said, you might be surprised how much you can pass to apps via
the command line as well…
(there’s more than one way to do it in many cases)

rb-appscript will make it more convenient to pass data to/from Ruby.