By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
457,916 Members | 1,336 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 457,916 IT Pros & Developers. It's quick & easy.

Identifying bundles in MacOS X

P: n/a
Greetings,

The following question pertains to a problem I am solving in Python
2.4b2 on a MacOS 10 (Panther) system with an HFS+ filesystem.

Given the pathname of a directory in my filesystem, I would like a
graceful way to determine whether or not that directory represents a
"bundle", in the sense that, when you double-click on the directory's
icon in the Finder, it is launched rather than "opened" for viewing.
Applications behave this way by default, but so do various other file
types (e.g., the "rtfd" files created by TextEdit, and the data files
for certain applications such as Quicken).

So, what I want is the following: A function I can call which will,
given a pathname, return True if the corresponding directory has that
property, and False otherwise. So far, I'm stumped. Can anyone help?

Here are the things I've tried so far:

I found the Carbon.File module, and tried what I thought was the obvious
thing:

from Carbon import File

fss = File.FSSpec(dir_path)
info = fss.FSpGetFInfo()

This, unfortunately, does not seem to work for directories; an OSError
is raised (-43, File Not Found). I investigated on Apple's developer
site, and it seems this is the expected behaviour. So, no fault of
Python there.

Next, I tried the following:

from Carbon import Files

fsr = File.FSRef(dir_path)
cat_info = fsr.FSGetCatalogInfo(Files.kFSCatInfoFinderInfo)

This works, but now I have no idea how to determine the answer to my
question. The value of cat_info is a tuple consisting of:

- a File.FSCatalogInfo object
- a Unicode string (the name of the directory)
- a File.FSSpec object
- a File.FSRef object

According to [1], the FSCatalogInfo structure has finderInfo and
extFinderInfo fields, which ought to contain what I'm after (I think),
but these do not appear to be exposed by the FSCatalogInfo class in the
Carbon.File library, even if I specify I want the Finder info and
extended Finder info when I call FSGetCatalogInfo().

All right, so now my questions for those of you who know better than I
do, are these:

1. Am I looking in the right place for this information?

2. If not, is there a preferred method for determining whether
a given directory represents a "bundle" in the MacOS sense?

I would be most grateful for any helpful advice you might be able to
lend me regarding this puzzle.

Thanks,
-M

P.S.- I was disconcerted to discover that the Python documentation
does not seem to list the Carbon.File module. The macfs module
is listed, but it is deprecated, so I would prefer not to use it
in new code.

References:
[1] Carbon API documentation, "FSCatalogInfo"
http://shorl.com/biginegastuni

--
Michael J. Fromberger | Lecturer, Dept. of Computer Science
http://www.dartmouth.edu/~sting/ | Dartmouth College, Hanover, NH, USA
Jul 18 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
Michael J. Fromberger wrote:
Given the pathname of a directory in my filesystem, I would like a
graceful way to determine whether or not that directory represents a
"bundle", in the sense that, when you double-click on the directory's
icon in the Finder, it is launched rather than "opened" for viewing.
Applications behave this way by default, but so do various other file
types (e.g., the "rtfd" files created by TextEdit, and the data files
for certain applications such as Quicken).


I think that a MacOSX-style application can be recognised
simply by the fact that it's a directory whose name ends
in ".app". (You don't usually see that extension in the
Finder, but all MacOSX application bundles seem to have it.)

A Classic-style application is a file with a type code
of "APPL".

As for other files, they can be associated with applications
using either the old type/creator code system, or the new
filename-extension-based system (which is disappointingly
Windows-like, but sadly seems to be the dictated Way of
the Future :-(.)

The type and creator of a file can be found using FSpGetInfo.
You're right that this doesn't work for directories, but
directories don't have type/creator codes anyway. MacOSX
bundles seem to be recognised purely by filename suffix
(and possibly also by having the right internal structure).

--
Greg Ewing, Computer Science Dept,
University of Canterbury,
Christchurch, New Zealand
http://www.cosc.canterbury.ac.nz/~greg

Jul 18 '05 #2

P: n/a
In article <2v*************@uni-berlin.de>,
Greg Ewing <gr**@cosc.canterbury.ac.nz> wrote:
Michael J. Fromberger wrote:
Given the pathname of a directory in my filesystem, I would like a
graceful way to determine whether or not that directory represents
a "bundle", in the sense that, when you double-click on the
directory's icon in the Finder, it is launched rather than "opened"
for viewing. Applications behave this way by default, but so do
various other file types (e.g., the "rtfd" files created by
TextEdit, and the data files for certain applications such as
Quicken).


I think that a MacOSX-style application can be recognised simply by
the fact that it's a directory whose name ends in ".app". (You don't
usually see that extension in the Finder, but all MacOSX application
bundles seem to have it.)


Actually, Apple's developer documentation seems to imply that there is a
"bundle bit" in the Finder info (or possibly the extended Finder info)
for directories, and that if this bit is set, the Finder will treat the
folder as a bundle. At any rate, there are many bundle-like directories
other than just ".app" files and old-style 'APPL' type files. The
trouble is, the Carbon.File module does not seem to expose the
appropriate fields of the FSCatalogInfo structure from the Carbon API,
so I can't get at that bit from within Python.

I found and adapted a patch to Mac/Modules/file/_Filemodule.c, that
makes the finderInfo and finderXInfo fields visible, but either I did it
wrongly, or there is some other issue I've missed, for I get nothing but
an empty FInfo structure back when I try to access either of them.

I guess my next step should be to write a C program against the Carbon
API, and see if I can get the information that way -- if so, I guess I
will just have to patch the Carbon.File module myself. If not, I might
have to admit defeat for now.

Meanwhile, I'm using a workaround, a.k.a. a gross hack: If the
directory's name has one of a set of "known" extensions, or if it
contains a directory named "Contents", and if it doesn't have more than
a few files at its top level apart from the "Contents" directory, I'm
considering it a bundle. That works well enough for my purposes, but
I'd really rather have it work "correctly."

Thanks for taking the time to write back!

Cheers,
-M

--
Michael J. Fromberger | Lecturer, Dept. of Computer Science
http://www.dartmouth.edu/~sting/ | Dartmouth College, Hanover, NH, USA
Jul 18 '05 #3

P: n/a
Michael J. Fromberger wrote:
Actually, Apple's developer documentation seems to imply that there is a
"bundle bit" in the Finder info (or possibly the extended Finder info)
for directories, and that if this bit is set, the Finder will treat the
folder as a bundle.


It's worse than that... the bundle bit _might_ or _might not_ matter,
since it's not always present, and some extensions are known or not
or... ugh.

Fortunately, it should be possible to get this information out of Launch
Services instead of replicating all the same checks: one of the flags
bit it can return is 'kLSItemInfoIsPackage'. It seems to make the
correct bundle/not bundle determination in my admittedly limited spot
checking.

I'm not sure if there's a standard Python module to expose Launch
Services, but here's a C snippet; use it as you like:

#include <Carbon/Carbon.h>
bool isFilePackage(const char *file) {
CFStringRef path = CFStringCreateWithCString(
NULL, file, kCFStringEncodingUTF8);

CFURLRef url = CFURLCreateWithFileSystemPath(
NULL, path, kCFURLPOSIXPathStyle, false);
CFRelease(path);

LSItemInfoRecord info;
LSCopyItemInfoForURL(url, kLSRequestAllInfo, &info);
CFRelease(url);

return 0 != (info.flags & kLSItemInfoIsPackage);
}

(error checking snipped for brevity)

-- brion vibber (brion @ pobox.com)
Jul 18 '05 #4

P: n/a
In article <2v*************@uni-berlin.de>,
Brion Vibber <br***@pobox.com> wrote:
Michael J. Fromberger wrote:
Actually, Apple's developer documentation seems to imply that there
is a "bundle bit" in the Finder info (or possibly the extended
Finder info) for directories, and that if this bit is set, the
Finder will treat the folder as a bundle.
It's worse than that... the bundle bit _might_ or _might not_ matter,
since it's not always present, and some extensions are known or not
or... ugh.


The plot thickens!
I'm not sure if there's a standard Python module to expose Launch
Services, but here's a C snippet; use it as you like:
[...]


Thank you very much for your advice, and for the sample code -- I will
play around with this snippet, and see I can get what I need. It looks
promising! If it works out, it will not be so difficult to add it as an
extension module.

Cheers,
-M
--
Michael J. Fromberger | Lecturer, Dept. of Computer Science
http://www.dartmouth.edu/~sting/ | Dartmouth College, Hanover, NH, USA
Jul 18 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.