1

I'm currently developing an agent "Agent.app" which must scan your usb port and if it detect an usb device, the app start an application called "Filebrowser.app".

I want the Agent.app located inside the Filebrowser.app to avoid multiple apps.

My first app FileBrowser is develop in Qt/c++ under QT creator. I have also develop the Agent.app under Qt Creator but it's just a C++ class no relation with Qt.

I think I would need the agent.pri declared inside the FileBrowser.pri to make sure that the FileBrowser.app will include it.

What I have done in the agent is :

int main()
{
    PulsUsbDetection *MyUSBDevice = new PulsUsbDetection;

    if(MyUSBDevice->isPulsConnected() == true) {
        char *filepath = "/Applications/puls_connect.app";
        string command = "open " + filepath;
        system(command.c_str());
    }

    return 0;
}

That's the worst thing I can do mainly if the app is not in Application.app.

I do have 2 questions:

  • How to allow the agent.app to start the FileBrowser.app when the agent is inside the FileBrowser.app ?
  • How to make sure that the agent.app will be included in the FileBrowser.app ?

Thanks a lot

Michael Dautermann
  • 88,797
  • 17
  • 166
  • 215
Seb
  • 2,929
  • 4
  • 30
  • 73

1 Answers1

0

I want the Agent.app located inside the Filebrowser.app to avoid multiple apps.

Have you considered having just a single program that includes the functionality of both applications, and chooses (inside main()) which functionality to execute based on e.g. a command line argument? That would be easier to implement, and would also make your install package a lot smaller. That said...

How to make sure that the agent.app will be included in the FileBrowser.app ?

After compiling both programs, move the agent.app folder into the FileBrowser.app/Contents/Resources folder. You could use a shell command or bash script to do that, or just right-click on the FileBrowser.app icon, choose "Open Package" from the menu, and then open the Contents and Resources sub-folders and drag agent.app in.

How to allow the agent.app to start the FileBrowser.app when the agent is inside the FileBrowser.app ?

The first thing you'll want to do is find the path of the directory that your "agent" executable is in. Fortunately Qt has a method to just that: You can just call QApplication::applicationDirPath().

So, for example, if your FileBrowser.app was placed in /Applications, and agent.app was placed in FileBrowser.app/Contents/Resources (as suggested above), then the above function call would return a string like "/Applications/FileBrowser.app/Contents/Resources/agent.app/Contents/MacOS".

The second step would be to derive from that string the path to your FileBrowser app. That should be straightforward to do:

// This code to be run in the agent app, to launch the FileBrowser app...
QString agentExecutableDirPath = QApplication::applicationDirPath();
QString fileBrowserExecutablePath = agentExecutableDirPath + "../../../../MacOS/FileBrowser";
system(QFile::encodeName(fileBrowserExecutablePath).constData());  // run it

Note that you'd probably get nicer behavior launching the FileBrowser process using Qt's QProcess API than by calling system() -- in particular, the system() call won't return until the FileBrowser process exits, which will have the side effect of freezing up your agent GUI until then.

Jeremy Friesner
  • 70,199
  • 15
  • 131
  • 234