Reveal to finder : multiple selection ?

Discussion in 'Mac Programming' started by phjo, Mar 8, 2008.

  1. phjo macrumors regular

    Jan 8, 2008
    Revealing in finder one file is easy with selectFile:inFileViewerRootedAtPath: method of NSWorkspace.

    But how would one obtain multiple file selection in finder ?

    (supposing that is possible, of course)

  2. MacDonaldsd macrumors 65816


    Sep 8, 2005
    London , UK

    As far as I can see it always opens it in the same finder window when I loop through a few files, and therefore displays the last file in the finder, which is strange.
  3. phjo thread starter macrumors regular

    Jan 8, 2008
    I thought I was getting nearer with the Scripting Bridge framework, as reading from
    it seems like it is possible with Applescript to reveal in Finder a selection of several files, provided they are in the same container (which sounds reasonable enough...)

    But I was only able to select individual files by using the construct :

    FinderItem *theItem = [[finder items] objectAtLocation: *NSURL];

    and reveal it with :

    [theItem reveal];

    but I would need a way to reveal a list of files, by calling only once a reveal method of some sort...

    I am a bit lost in the documentation, and the fact that I know about nothing about applescript, and not that much about cocoa doesn't help...

    For MacDonaldsd, yes, I have the same experience. There is nothing surprising to see only one file selected, but I would have expected to see as many finder windows opening when using :
    selectFile:inFileViewerRootedAtPath: method...

  4. phjo thread starter macrumors regular

    Jan 8, 2008
    Well, it does confirm what I read, that multiple selection is possible through applescript (again, provided all files are in one same container), but it doesn't tell me how to do it using objective-c.

  5. kainjow Moderator emeritus


    Jun 15, 2000
    It might not be possible through Obj-C, but that doesn't mean you can't do it via NSAppleScript.
  6. phjo thread starter macrumors regular

    Jan 8, 2008
    You're right... something else to learn and try...

    I'd prefer a more integrated way of doing things though, but I'll give it a try later on, thanks.

  7. hhas macrumors regular

    Oct 15, 2007
    Using objc-appscript:

    #import "FNGlue/FNGlue.h"
    // To create Finder glue:  osaglue  -o FNGlue  -p FN  Finder
    int main (int argc, const char * argv[]) {
    	NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
    	FNApplication *finder = [[FNApplication alloc] initWithBundleID: @""];
    	NSArray *files = [NSArray arrayWithObjects: 
    		[NSURL fileURLWithPath: @"/Users/has/Documents"],
    		[NSURL fileURLWithPath: @"/Users/has/Movies"],
    		[NSURL fileURLWithPath: @"/Users/has/Pictures"],
    	NSError *err;
    	[[finder activate] send];
    	[[finder select: files] sendWithError: &err];
    	if (err) NSLog(@"%@", err);
    	[finder release];
    	[pool drain];
    	return 0;
  8. phjo thread starter macrumors regular

    Jan 8, 2008
    Interesting ! By browsing the web looking for solutions for this problem, I found out about objc-appscript, but I didn't pay much attention to it...

    Now the code that hhas pasted looks very nice so it motivated me to try it, and it works just fine indeed... Furthermore, it allows to select files in several containers, which makes then Finder open as many windows as there are containers.

    A question though, although I am ages from distributing any software, I wonder about the distribution of it, when in binary form. Of course, whoever needing to build one application from the source will need to install (download with subversion from sourceforge, building with xcode...) the AppScript framework but is it the case as well for binary distribution ?

    I would guess some kind of static linking should be possible, but I am not sure it is the default, or how you would trigger such a building... (yeah, I am a newbie in osx programming, and will remain one for many months to come I guess...)

  9. hhas macrumors regular

    Oct 15, 2007
    Just include Appscript.framework in your application bundle as a private framework. Tutorials covering this stuff in more detail are currently being written; in the meantime, see:

    Appscript is MIT licensed, so can be freely included and redistributed within your own applications (with the minor requirement that you also include its original license).
  10. phjo thread starter macrumors regular

    Jan 8, 2008
    Yes, embedding the framework (or as a private framework) is the way to go... That's for a very uncertain future though...

    Many thanks for your help, hhas, and for your work.


    P.S. : one can only regret to see Apple trying to reinvent the wheel and propose a new and different framework instead of maybe hiring you and make your framework part of Apple frameworks...

Share This Page