-[NSWorkspace openFile:withApplication:] 等待打开应用程序
在我的 Obj-C 应用程序中,我使用以下代码在 Pages(或任何其他应用程序)中打开文件:
[[NSWorkspace sharedWorkspace] openFile:theUrl withApplication:@"Pages"];
主要是当打开较大的文件时,这可能需要几秒钟才能完成。 所以我希望我的应用程序等待页面,直到它完全打开文件。
下面的代码是我想要的方式:
[[NSWorkspace sharedWorkspace] openFile:theUrl withApplication:@"Pages" onFinish:@selector(pagesfinishedopening)];
当然,我可以简单地使用 sleep() 函数,但这会减慢小文件上的应用程序的速度,并且当文件大于例外时将无法工作。
我已经尝试过使用 NSApplication 进行一些操作,但是页面中文件的打开不受尊重,只能监视目标应用程序的启动。
有什么想法吗?
In my Obj-C App I'm using the following code to open a file in Pages (or any other Application):
[[NSWorkspace sharedWorkspace] openFile:theUrl withApplication:@"Pages"];
Mainly when bigger files are opened, this may take a few seconds to finish.
So I want my Application to wait for Pages until it completely opened the file.
The following code is how I would love to do it:
[[NSWorkspace sharedWorkspace] openFile:theUrl withApplication:@"Pages" onFinish:@selector(pagesfinishedopening)];
Of course I could simply use the sleep() function, but this would slow down the app on small files and would not work when the files are bigger than excepted.
I already tried something with the NSApplication, but then the opening of the file in Pages is not respected, only the start of the target application can be monitored.
Any Ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以收听 通知 从 NSWorkSpace 到达,如下所示
You can listen for notifications arriving from NSWorkSpace like shown below
也许您可以使用 scriptingbridge 来检查页面是否打开了文件,然后在页面打开文件后继续执行应用程序需要执行的操作。任何支持 Apple 脚本的应用程序都可以使用 scriptingbridge 编写脚本。找到应用程序的文档有点困难,但我认为它应该类似于苹果脚本。
Soem ScriptingBridge 文档:
http://www.mugginsoft.com/AutomationDocs
Apple ScriptingBridge 使用指南:
https://developer.apple.com/library/ mac/#documentation/Cocoa/Conceptual/ScriptingBridgeConcepts/UsingScriptingBridge/UsingScriptingBridge.html
上面解释了如何使用脚本桥,并有一个页面示例(非常简短)。通过一些实验,您应该能够让您的应用程序每秒检查页面是否打开文件,直到文件打开(即,当文件未打开时,等待一秒钟 - 可能不是最好的方法,但它是这样)。我认为也有未记录的通知。希望它有所帮助。
Perhaps you could use scriptingbridge to check whether pages opened the file, and then continue on with whatever your app needs to do once pages opens the file. Any app that supports apple script can be scripted using scriptingbridge. Its a bit difficult to find documentation for apps, but I think it should be similar to apple script.
Soem ScriptingBridge documentation:
http://www.mugginsoft.com/AutomationDocs
Apple ScriptingBridge usage guide:
https://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/ScriptingBridgeConcepts/UsingScriptingBridge/UsingScriptingBridge.html
The above explains how to use scripting bridge, and has an example for Pages (very brief). With some experimentation you should be able to have your app check whether pages opened the file every second until its open (i.e. while file is not open, wait one second - probably not the best way to do it, but its something). I think there are undocumented notifications as well. Hope it helps somewhat.