简体   繁体   English

如何在自制的 Mac OS 应用程序中打开外部文件

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

I am learning SwiftUI.我正在学习 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.我知道如何在 Windows / Linux 中实现它,我必须确保我的应用程序可以读取作为参数传递的参数。 For example: "MyImageBrowser.exe d:\\images\\photo.jpg"例如:“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.我不知道的是如何“强制”MacOs 传递带有要打开的图像位置的参数。

I compiled my code and the result (iSee.app) copied to Application folder.我编译了我的代码并将结果 (iSee.app) 复制到 Application 文件夹。

在此处输入图片说明

Then I tried to open an image by clicking on it and choosing "Open with...".然后我尝试通过单击它并选择“打开方式...”来打开图像。

I received warning:我收到警告:

在此处输入图片说明

Mac apps have a file called info.plist that tells the system things about the app. Mac 应用程序有一个名为 info.plist 的文件,它告诉系统有关应用程序的信息。 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.您需要将 CFBundleDocumentTypes 条目添加到应用程序的 info.plist 文件中。

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.在 Xcode 帮助系统中的“信息属性列表”中搜索有关声明您的应用程序能够打开/编辑的文档类型的相关部分。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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