# OS X File information question

**URL:** <https://www.ruby-forum.com/t/os-x-file-information-question/76862>\
**Category:** Ruby\
**Created:** [November 21, 2006, 7:41pm UTC](https://www.ruby-forum.com/t/os-x-file-information-question/76862 "2006-11-21T19:41:45Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![waddilove](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/b5ac83/32.png) [@waddilove](https://www.ruby-forum.com/u/waddilove)\
**Post date:** [November 21, 2006, 7:41pm UTC](https://www.ruby-forum.com/t/os-x-file-information-question/76862/1 "2006-11-21T19:41:45Z")

</div>

OS X (tiger) stores some useful information about files, including  
“where from” and “spotlight commnets”.

Is there an (easy) way to access these fields from Ruby?

Many thanks, Jonathan

---

<div class="post-metadata">

**Author:** ![waddilove](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/b5ac83/32.png) [@waddilove](https://www.ruby-forum.com/u/waddilove)\
**Post date:** [November 21, 2006, 10:29pm UTC](https://www.ruby-forum.com/t/os-x-file-information-question/76862/2 "2006-11-21T22:29:08Z")

</div>

On 21/nov/06, at 19:41, Jonathan W. wrote:

> OS X (tiger) stores some useful information about files, including  
> “where from” and “spotlight commnets”.
> 
> Is there an (easy) way to access these fields from Ruby?

Give a look here: [http://developer.apple.com/macosx/spotlight.html](http://developer.apple.com/macosx/spotlight.html)  
Under the “Command-Line tool” section you can find something  
interesting.

For example:

piastrella:~/Desktop$ mdls -name kMDItemFinderComment test.txt  
test.txt -------------  
kMDItemFinderComment = “This is a Spotlight comment.”

You could wrap that tool in a Ruby class or something like that.

---

<div class="post-metadata">

**Author:** ![waddilove](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/b5ac83/32.png) [@waddilove](https://www.ruby-forum.com/u/waddilove)\
**Post date:** [November 22, 2006, 10:17am UTC](https://www.ruby-forum.com/t/os-x-file-information-question/76862/3 "2006-11-22T10:17:29Z")

</div>

Gabriele, Fred

Thank you both for the help. I’ll poke around in the documentation,  
first task is to extract the settings in those fields (changing them  
from Ruby is later in my game plan)

Many thanks for your help.

---

<div class="post-metadata">

**Author:** ![waddilove](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/b5ac83/32.png) [@waddilove](https://www.ruby-forum.com/u/waddilove)\
**Post date:** [November 21, 2006, 11:25pm UTC](https://www.ruby-forum.com/t/os-x-file-information-question/76862/4 "2006-11-21T23:25:58Z")

</div>

It’s a little more tricky writing to these: to write a finder comment  
(now called spotlight comment, but implementation-wise it’s the same)  
you need to send an appleevent to the finder (there’a a code sample on  
[developer.apple.com](http://developer.apple.com) for this).

To set the where\_from, you need to use the undocumented api  
MDItemSetAttribute, since in theory meta deta doesn’t just get set at  
random times, but when the importer for that file is run.  
MDItemSetAttribute does however enable you to set metadata at will  
(those changes would of course be blown away if the user chose to  
reindex their files.

Fred

---

<div class="post-metadata">

**Author:** ![waddilove](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/b5ac83/32.png) [@waddilove](https://www.ruby-forum.com/u/waddilove)\
**Post date:** [November 24, 2006, 9:14pm UTC](https://www.ruby-forum.com/t/os-x-file-information-question/76862/5 "2006-11-24T21:14:51Z")

</div>

Frederick C. wrote:

> It’s a little more tricky writing to these: to write a finder comment  
> (now called spotlight comment, but implementation-wise it’s the same)  
> you need to send an appleevent to the finder (there’a a code sample on  
> [developer.apple.com](http://developer.apple.com) for this).

You can do AEs from C but it’s a bit tedious. Much easier with a  
high-level bridge, e.g.:

```
require "appscript"

pth = "/path/to/some file or folder"
txt = "some comment"

AS.app("Finder").items[MacTypes::Alias.path(pth)].comment.set(txt)

```

Or, if you want to avoid dependencies and speed isn’t an issue, you  
could just use osascript to invoke an AppleScript via the command line.  
Kludgy, but it’ll do the job.

If portability is a must though, bear in mind that some OS X users run  
third-party file managers in place of the Finder. (Not like the ol’ OS  
7-9 days when Finder availability was pretty well guaranteed.) I think  
there’s also some very crusty Carbon APIs for accessing Finder-related  
data, though can’t remember anything about them. Probably in the Carbon  
File Manager or somewhere like that.

For querying files, the OP might also want to check out the Carbon or  
Cocoa Spotlight APIs:

[http://developer.apple.com/documentation/Carbon/Conceptual/SpotlightQuery/index.html](http://developer.apple.com/documentation/Carbon/Conceptual/SpotlightQuery/index.html)

There’s a ‘RubySpotlight’ example included with RubyCocoa 0.4.2 that  
may be of help here.

has
