Opening multiple copies of application

Discussion in 'Mac Programming' started by MSutton, Jul 21, 2009.

  1. macrumors newbie

    Joined:
    Jul 21, 2009
    #1
    I'm trying to get a Mac conversion of a QT-based C++ project of mine working. Despite being very much a Mac novice I've solved most issues, but am stuck with a problem relating to multiple application instances.

    My application is designed as a single-document system - every time someone double-clicks a file associated with it, I need a new instance of the application to start. Currently the Mac version of my program starts, waits for the system 'open file' message to appear, then opens that file - works fine the first time, but not of course for opening multiple files.

    Ideally I'd like to either

    a) Tell the OS that it should automatically start a new instance of my application - if there is any way to do that - or
    b) Have my application intercept new 'file open' messages and start a new instance of itself to handle it - again, if this is possible.

    Can anyone advise on how to do this? Obviously plan B is to recode the whole thing to work on a single application with multiple windows paradigm, but there are a whole host of practical reasons why I'd much rather not do this!

    Thanks in advance

    Mark
     
  2. macrumors 6502a

    GorillaPaws

    Joined:
    Oct 26, 2003
    Location:
    Richmond, VA
    #2
    You really should try to go with plan B imo. Perhaps you could share some of the issues you're having with implementing the document-based application architecture and some of us could help you out. I'm certainly no expert, but one of the things I've learned is that you should really avoid fighting/circumventing Apple's design patterns and go with the way they want you to do it at all costs.
     
  3. thread starter macrumors newbie

    Joined:
    Jul 21, 2009
    #3
    There's no show stoppers or things I don't know how to do in going multi-document - it's just going to be a lot of work which I was hoping to avoid. But if the OS is determined to force me down this route I may not have any choice :(
     
  4. macrumors 6502a

    Joined:
    Sep 17, 2007
    #4
    I would go with plan B, however, if you really want to have multiple instances of the same application you should be able to do it using QProcess. So, when you receive a new 'file open' signal, you can create and start a new QProcess object. I'm not certain, but I'm pretty sure you can use QProcess to start another instance of the same application.

    I still think plan B is the right thing to do. Opening multiple instances of your program just seems like a hack or a (no offense intended) lazy way out.
     
  5. macrumors 6502

    Joined:
    Mar 8, 2009
    Location:
    Denver, CO
    #5
    I don't know Mac programming, so this may or may not be doable.

    I think you could look into using two programs. The first program is the clickable one (and can be associated with the file type). So it loads and has the file name/path. Then it needs to start second program giving the file as a parameter. Presumably, starting the second programming will use a different mechanism that won't be intercepted by the OS. I'm thinking something like fork or exec, but that might not change the name associated with the process. Anyway, you would want the first program to exit, so then the OS will not think there is an instance of it already running. You would have multiple instances of the second program running.
     
  6. thread starter macrumors newbie

    Joined:
    Jul 21, 2009
    #6
    Thanks - Yep, QProcess (using the startDetatched static approach) does the trick, thanks for the hint. As for whether it's a 'legit' approch - well if this was a commercial app or something intended primarily for a Mac I'd agree, but this is research-type software - only intended for a few users, and allowed to be a bit quick and dirty, so long as it basically works. The more I think about the plan B approach the worse it gets (I'd have to rework the whole thing to use threads for a start) and besides separate instances gives me crash protection (if one dies the others stay up). Also the single-shot approach makes more sense on Windows, which is the primary target - and I'd much rather avoid too much difference between the two versions.
     
  7. macrumors G5

    gnasher729

    Joined:
    Nov 25, 2005
    #7
    When the user opens a document intended for your application, and your application is already running, then the Finder will send a message to your application to open that document. When you receive that message, you should check whether there is already an open document. If not, open the document. If yes, you can call the OS to open a copy of your application, and pass the document to it.
     
  8. macrumors 6502a

    Joined:
    Jan 15, 2007
    #8
    There is a hack way to do it, but I wouldn't recommend it

    You can launch multiple copies of applications issuing the following command:
    /Applications/(your app name).app/Contents/MacOS/(yourappname)

    For example, if you wanted to launch multiple instances of Safari, issue the following command in Terminal:
    /Applications/Safari.app/Contents/MacOS/Safari

    This doesn't apply to Java applications and other non-cocoa/carbon apps, so be careful, but it is possible.

    Essentially what you could do is create a second app that basically takes the opened file, then calls your app using the above method and provides the file name as an argument.

    Again, its a hack and there are 0 guarantees of how the operating system will treat multiple instances of your app.....
     
  9. macrumors 6502a

    Joined:
    Sep 17, 2007
    #9
    I'm glad you got it figured out. I never tried doing that myself but it's good to know. You're right about the plan B approach. No sense making it more complicated than it needs to be, especially considering it's only going to be used by a few users.
     

Share This Page