CodeGrinder
CodeGrinder

Reputation: 201

How to open external file in self-made Mac OS application

I am learning SwiftUI. I want to write an image browser which I can put into "Application" directory and use it as default image browser. I'd like to choose my app in right click menu: "Open in application..."

I know how to make it in Windows / Linux where I have to ensure that my application can read arguments passed as parameters. For example: "MyImageBrowser.exe d:\images\photo.jpg"

I know what to do inside:

CommandLine.arguments.forEach() { /* do something with the parameters like reading the file */}

or how to display the image

let url = URL(fileURLWithPath: param) //param is obtained from CommandLine.arguments
let image = NSImage(contentsOf: url)!

and

var body: some View {
        Image(nsImage: image)
            .frame( maxWidth: .infinity, maxHeight: .infinity, alignment:.center)
}

The thing that I don't know is how to "force" MacOs to pass the parameter with the location of a image to open.

I compiled my code and the result (iSee.app) copied to Application folder.

enter image description here

Then I tried to open an image by clicking on it and choosing "Open with...".

I received warning:

enter image description here

Upvotes: 1

Views: 981

Answers (1)

Duncan C
Duncan C

Reputation: 131398

Mac apps have a file called info.plist that tells the system things about the app. One of the things it declares is the types of files that your app knows how to open. You'll need to add a CFBundleDocumentTypes entry to your app's info.plist file.

Search on "Information Property List" in the Xcode help system for the relevant section on delcaring the types of document your app is able to open/edit.

Upvotes: 1

Related Questions