# OSX Finder selected files

**URL:** <https://www.ruby-forum.com/t/osx-finder-selected-files/138410>\
**Category:** Ruby\
**Created:** [May 22, 2008, 6:36pm UTC](https://www.ruby-forum.com/t/osx-finder-selected-files/138410 "2008-05-22T18:36:36Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![djmook](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/d6d6ee/32.png) [@djmook](https://www.ruby-forum.com/u/djmook)\
**Post date:** [May 22, 2008, 6:36pm UTC](https://www.ruby-forum.com/t/osx-finder-selected-files/138410/1 "2008-05-22T18:36:36Z")

</div>

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

☹

I’m all at sea.

---

<div class="post-metadata">

**Author:** ![djmook](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/d6d6ee/32.png) [@djmook](https://www.ruby-forum.com/u/djmook)\
**Post date:** [May 23, 2008, 3:35am UTC](https://www.ruby-forum.com/t/osx-finder-selected-files/138410/2 "2008-05-23T03:35:51Z")

</div>

Mark C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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](http://rubyforge.org/mailman/listinfo/rubyosa-discuss)

Good luck.

---

<div class="post-metadata">

**Author:** ![djmook](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/d6d6ee/32.png) [@djmook](https://www.ruby-forum.com/u/djmook)\
**Post date:** [May 23, 2008, 10:58am UTC](https://www.ruby-forum.com/t/osx-finder-selected-files/138410/3 "2008-05-23T10:58:09Z")

</div>

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](http://appscript.sourceforge.net)

---

<div class="post-metadata">

**Author:** ![djmook](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/d6d6ee/32.png) [@djmook](https://www.ruby-forum.com/u/djmook)\
**Post date:** [May 24, 2008, 2:48pm UTC](https://www.ruby-forum.com/t/osx-finder-selected-files/138410/4 "2008-05-24T14:48:41Z")

</div>

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.
