Cocoa – Open File Package in Finder – Education Career Blog

I am trying to open the Finder in a certain package.

I got this file, Example.backup, which is just a folder with an extension. In the finder, you can right click it (Show Package Contents) and I will be presented with the files. (there is no Contents folder).

I’ve tried opening it with NSWorkspace’s methods but none open the finder at that directory, they either select the file or open it with the associated program.

Is there a way to do this in AppleScript maybe? Or Cocoa?

Thanks

,

Since no one answered yet, there might not be a one line solution.

All I could think of was a workaround: call /usr/bin/open with the -R option, which will reveal the given file in Finder. Since you want to show the contents of the package, you will have to reveal any file that is inside (not the package itself).

Downsides: won’t work on empty package (but then you could just reveal the package), also the Finder window will show a selection on the last item.

NSString *pathToBundle = @"/tmp/test.app";
NSFileManager *fm = NSFileManager alloc init autorelease;

// get the last file/directory in the package
// TODO: error handling / empty package
NSString *lastItemInBundle = fm contentsOfDirectoryAtPath:pathToBundle error:NULL lastObject;

// reveal this file in Finder, by using /usr/bin/open
// see -R option in the manpage
NSTask *open = NSTask alloc init autorelease;
open setLaunchPath:@"/usr/bin/open";
open setCurrentDirectoryPath:pathToBundle;
open setArguments:NSArray arrayWithObjects:@"-R", lastItemInBundle, nil;
open launch;

Leave a Comment