Open With not working correctly on Mac


#1

I would like to open files with the extension .test123 with my app. It works, but only if the app was already running before double-clicking the file. Not if opening the file launches the app.

I have set the Document File Extensions in my Projucer project to .test123. When I open my app first, and then double-click on the Foo.test123 file, anotherInstanceStarted gets called with the commandLine set to the file path – good :+1: Also, I can drag Foo.test123 onto my app, and it works fine as well.

Problem: With the app not already running, double-clicking Foo.test123 does open my app, but just calls initialise with empty commandLine. The same if I drag Foo.test123 onto my app.

Has someone else encountered this, and how do you handle this case? Projucer does it correctly, but I couldn’t find the part in the Projucer code that fixes this…

My code to reproduce it:

#include "../JuceLibraryCode/JuceHeader.h"

class OpenWithTestApplication  : public JUCEApplication
{
public:
    OpenWithTestApplication() {}

    const String getApplicationName() override       { return ProjectInfo::projectName; }
    const String getApplicationVersion() override    { return ProjectInfo::versionString; }
    bool moreThanOneInstanceAllowed() override       { return false; }

    void initialise (const String& commandLine) override
    {
        AlertWindow::showMessageBox(AlertWindow::NoIcon, "initialise", commandLine);
    }
    
    void anotherInstanceStarted (const String& commandLine) override
    {
        AlertWindow::showMessageBox(AlertWindow::NoIcon, "anotherInstanceStarted", commandLine);
    }

    void shutdown() override {}
    void systemRequestedQuit() override { quit(); }
};

START_JUCE_APPLICATION (OpenWithTestApplication)

#2

Hi, I’m not sure.But this may be because it is necessary to implement code to get the file passed in the commandLine


#3

The Mac OSX is not calling the executable with the file as argument, as a linux user would expect, but it uses the LaunchServices:

Once the preferred application has been determined, Launch Services launches or activates it (depending on whether it is already running) and sends it an 'odoc' (“open document”) Apple event instructing it to open the specified document.

I am not aware, that JUCE has something in place to support that behaviour out of the box. It would be a nice addition though (especially for Projucer as well)…


#4

I am not aware, that JUCE has something in place to support that behaviour out of the box.

JUCE supports that, but it doesn’t seem to work in this case. It gets called when you double-click a file with the app already running.


#5

That’s interesting, thanks @basteln

That seems to be connected to call anotherInstanceStarted (const String& commandLine).
And that’s how it works indeed in Projucer…