File opened from Finder

I’m new to Juce and only been using a Mac for a year so I apologise in advance if I’m asking a stupid question…

I’m trying to create a basic MP3 player with Juce using the Quicktime framework. All is going well and Juce has made this extremely easy so far. However I have hit a problem. I want to use this player to be my default mp3 app, only if I open a file in finder with it, the commandline string passed in begins with -psn_ (a process id of some sort I assume??).

My question is, how to I get the file name of the file which has been clicked from this? can I? Same thing happens when using open in terminal, however a command like “appname -file.mp3” in terminal passes the argument in fine.

I hate iTunes, I need help now!!! :cry:

No idea about the psn thing - does it contain the filename as well? Or maybe the number’s a file ID of some kind? I think the Apple docs will be more use than me on this one!

I’ve been trying to read the Apple docs…best I got was possibly some LauncherServices thing and OpenDocument event…I dunno…Does anyone know if anyone has made an app that is opened from clicking a file in finder?? (or even for other OS’s, any clue would be great!)

The psn looks a bit like “-psn_0_620180” (note number completely made up for example purposes).

Love using Juce by the way! :slight_smile:

You want to look at FSMakeFSSpec, FSpMakeFSRef, and LSOpenFSRef. You have to create a unique fileType in FSpCreate so your app knows what type of document it is. After your app initializes, you can then receive the AppleEvent kAEOpen (which the OS sends) and check the event for your document type. If it’s your document, you can open it.

If you want your app to open up a file type created by another application, you’ll have to set your Creator type for your app to the same as that other app. Then once you run your app, the OS will associate your app with those files. I have no idea though what happens when two apps have the same Creator type and are running at the same time – never tried it, but I’d guess they’ll both end up getting AppleEvents.

  • kbj

I got this working!! My solution might not be perfect, or right…but for me, a C++ beginner I’m just pleased to get it working!! :smiley:

What I did, in case someone else needs it or (more likely) someone can suggest a more correct way of doing it, was…

When you open a file from Finder you get two Apple Events. The first is OpenApp which opens and starts your JUCE application (this is the same event recieved when opening the application on its own), the second is an OpenDoc event. JUCE handes both these, though from what I could see the OpenDoc event sends its data(file path) as a string to the anotherInstanceStarted function. So I just put in a function to pass this string from anotherInstanceStarted to my application components…I need to probably put in a few error checks here so I’m passing valid data, and tidy it up etc

I’m sure there is a better way of doing this. But for now it works. And JUCE was able to handle it all for me all along! :slight_smile:

Now I have to take a break from this to concentrate on university coursework for the weekend… :cry:

Ah yes, I’d forgotten that it did that!

I was looking again at this, as I hadn’t implemented it for when the user opens multiple files at once. I think I’m right in saying Juce does this by joining the filenames into a single string like so (line 147 juce_Mac_Messageing.cpp):

JUCE_TRY { JUCEApplication::getInstance() ->anotherInstanceStarted (files.joinIntoString (T(" "))); } JUCE_CATCH_ALL

However this will not work if the filename has a space, because the files can’t be seperated. I changed this to a “:” (colon) because I don’t think it can be used in a Mac filename. I don’t know how this is done with other OS’s, but if its a space, I could see a similar problem arising. Am I doing something wrong, or is this something the should be changed?

Good point, but it’d be better just to put quotes around the filenames, I think. Changing the separator would break existing code. I’ll do that for the next release.

(oh, just looked at the code, and my stuff already puts quotes around the filenames)

Ahh! That explains why I have to unquote my strings coming in from Finder. Silly me. :oops:

I should just change how I search for files in the string then. Thanks!

/ goes for cup of coffee