How to receive a command line argument in my app?

On MacOS, in Terminal I issue:
open /path/to/my.app --args ‘/path/to/some/file’

In my app’s AppleEventReceived handler I can check for eventID = “rapp” and then System.CommandLine is supposed to return the argument passed from the command line. But it contains “/path/to/my.app” rather than “/path/to/some/file” which is the actual argument.

Is there a way to pass an argument to a specific instance of my.app and catch the actual argument on the other end?

Have you looked at the DesktopApplication.DocumentOpened(file As FolderItem) Event?

To debug your app:

  1. Add the DocumentOpened Event to the App class.
  2. Add a breakpoint or ‘Break’ inside the new event.
  3. Start your app using the “Project > Run Paused” menu command
  4. Then execute your terminal command, i.e., open /path/to/my.debug.app --args /path/to/file.

The Xojo IDE should then open and halt on your breakpoint.

Note: escape any spaces in your terminal paths with a backslash, i.e., \

I just read the command line at open time.

open "./cmd.app" --args sdhfgjshdf 222

image

1 Like

Thx Rick. I actually want to open a 2nd instance of my app using open -n … and your method works for that as well. Just what I was looking for.

1 Like

You can use App.ArgumentsMBS in MBS Xojo Plugins to do this.

See blog post:

Console and GUI in one project