DocumentOpened not executed for running app

I want to pass a string into my desktopn app from the command line.
I added the DocumentOpened Event Handler to my App containing the single line
MsgBox item.Name
When I open the App from terminal using
open /Users/blablaetc/mygreat.app --args 123
the app starts and the message “123” is displayed.
So far, so good.

Now when I call the app again while it is still running, the DocumentOpened handler does not execute (at least I see no message.)

What is the magic trick to get that handler called, when the app is already running?

DocumentOpened would fire when a document that your app supports is dropped onto the app’s icon.

The fact that your app is already running is what’s causing you grief because the routines that you need are part of the application’s launch sequence.

Now… the other way you could handle this would be to make a mutex to tell you if another instance is already running and if it is, send the parameters to the other instance and then immediately quit.

Assuming this on Mac OS:

You’re using ‘open’ to launch the app with arguments, which is fine for the initial startup, but what you really want to do is use ‘open’ to tell your app to open a document. Try this instead:

open -a /path/to/your/app /path/to/your/file

This will work both to launch your app and tell the app to open the document, and should also automatically use your app if it is already running.

1 Like

Bingo, that works. Thanks Eric.