简体   繁体   中英

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

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.

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM