Home > Net >  How to access path sent by `open` command to Swift GUI app
How to access path sent by `open` command to Swift GUI app

Time:08-17

The open command can be used to send files to programs as in this example:

open -a "Google Chrome" Untitled.png

What would a Swift GUI app use to receive the path sent through that command?

I've tried this in the init() function of my main view

if CommandLine.arguments.count != 1 {
    path = CommandLine.arguments[1]
}

but the path doesn't seem to be sent there.

Response to Duplicate Question Claim

In Swift on macOS: open Chrome with a URL as parameter the question is asking how to open Chrome and pass it a URL from their app. I am trying to open my app and pass it files from the open command called from the terminal.

CodePudding user response:

The reason you can't find the arguments is because you didn't pass any arguments to the app in your command.

Unlike MyApp myFileURL, open -a MyApp myFileURL doesn't actually pass myFileURL to MyApp.

To actually pass any arguments to MyApp, you need to specifically use --args.

  • Related