views:

24

answers:

2

Hello,

I have an array of filenames that i would like to pass to an external application for opening. I'd like to do one of the following:

a) Somehow instruct OSX to open all these files with an associated application, but it must invoke the target app's openFiles NSApplication delegate method b) Specify the application to open these files with (and also invoke openFiles)

Basically it doesn't matter which solution to realise, because these files will be associated with the target application anyway. How would i do one of these things?

A: 

Check out the NSWorkspace methods, such as openFile:withApplication:.

jtbandes
I know this one, but how can i pass multiple files? I don't want to call this method N times, i just want to pass an entire array of paths.
Marius
+1  A: 

To open a whole bunch of files at once, send the shared NSWorkspace object an openURLs:withAppBundleIdentifier:options:additionalEventParamDescriptor:launchIdentifiers: message, or call the LSOpenURLsWithRole function or the LSOpenFromURLSpec function. Either way, you'll pass an array of URLs to items to open.

Each one of these will let you identify a specific application to use. NSWorkspace lets you specify it by bundle identifier, while the two Launch Services functions let you provide the URL or FSRef to a specific application bundle.

… it must invoke the target app's openFiles NSApplication delegate method

That isn't possible to require, because (a) the application may be document-based, in which case it probably does not have an NSApplication delegate and, even if it does, such a delegate would probably not respond to application:openFiles:, and (b) the application may not be Cocoa-based, in which case it would handle the Open Documents Apple Event directly. None of this is your application's business, so don't worry about it.

Peter Hosey
It is possible to require in this case, because i'm creating that application and i am implementing the openFiles: method. So it would be great if i could retrieve all filenames at once in the target application.
Marius
Marius: Fair enough. As long as you open all the documents at once, not separately, you should get a single `applictaion:openFiles:` message on the receiving end. Also, you may want to read the source code for the Growl framework, because we do something similar, opening a document with GrowlHelperApp. In particular, you'll probably want to use the same opening flags we do.
Peter Hosey
Thank you. Will try your suggestions.
Marius
It seems i got happy too early. I've tried openURLs: method. It seems to work and activates the target app, but the files aren't passed to application:openFiles: method. So how do i receive them then? I don't know of any other methods.
Marius
Never mind. I'll just use distributed notifications to pass the list of files in addition to supporting openFile: and openFiles: methods.
Marius
How did you create the URLs? Maybe you didn't pass valid file: URLs. If you used one of the LSOpen functions, did it return an error?
Peter Hosey