如何使用 NSWorkspace 打开文件类型? - 可可
我的应用程序能够提取 .deb 文件,但前提是您通过应用程序中的按钮指定位置。但我希望能够只需单击 .deb 文件并打开我的应用程序,运行我已有的操作,然后在完成后退出。现在我正在使用 NSWorspace 告诉我的应用程序它可以打开 .deb 文件,但我不知道如何让它运行我的操作。使用 NSWorkspace 是正确的做法吗?我还需要能够获取以字符串格式打开的文件的文件路径,但我似乎无法弄清楚这一点?
My app is able to extract .deb files but only if you specify where with buttons in the app. But I want to be able to just click on a .deb file and have my app open up, run the actions I already have, then quit when it's done. Right now I'm using NSWorspace to tell my app it can open .deb files but I don't know how to have it run my actions from that. Is using NSWorkspace even the right thing to do? I also need to be able to get the Filepath of the file being opened in string format and I can't seem to fighure this out?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我做了一些测试,其他人可能也会对此感兴趣。通过一个小例子列出了解决方案。
在 Xcode 中创建一个新的 Cocoa 项目,转到 xib 并向窗口添加一个标签。我们将用它来进行概念验证。接下来,向您的应用程序委托提供一些内容。接口:
实现非常简单:
有一个方法需要关注:
application:openFiles。
猜猜它有什么作用?它接收通过单击桌面或查找器中的项目或将文件拖到图标上传递的文件列表。它将传递一个包含文件名和路径的数组,UNIX 风格。
我的测试不需要在应用程序的 plist 中设置“文档类型”,或者使用查找器进行花哨的注册操作。当然,这可以添加...
这应该可以解决您的问题,您可以对 .deb 文件执行任何您想要的操作(我假设是 debian 软件包?)。
祝你好运!
Living
P.S.:只是不要忘记将 NSTextField 连接到 GUI 控件;-) 否则,它会工作,但不会显示任何内容......
I did some testing and this might be of interest to others, too. Solution is lined out by a little example.
Create a new Cocoa project in Xcode, go to the xib and add a label to the window. We will use it for a proof of concept. Next, some stuff to your app delegate. Interface:
The implementation is pretty straight forward:
There is one method to keep an eye on:
application:openFiles.
Guess what it does? It receives a list of files passed from either having clicked on an item on the desktop or in the finder, or having dragged files onto the icon. It will be passed an array with the filenames, along with the paths, UNIX style.
My tests did not require to set "Document types" in the plist of the app, or doing fancy registration stuff with the finder. Of course, this could add...
That should fix your problem and you can do whatever you want with your .deb files (I assume debian packages?).
Good luck!
Living
P.S.: Just do not forget to hook up the NSTextField to the GUI control ;-) Otherwise, it will work but nothing is displayed....
请提供更多关于您到底在做什么以及为什么它不起作用的信息,例如故障转储、gdb 输出等。
如果您不告诉我们我在做什么,很难猜测什么会有所帮助。
Please supply a bit more information on what exectly are you doing and maybe why it does not work, e. g. crash dump, gdb output, etc.
It is hard to guess what might help if you don't tell us what i going on.