-[NSWorkspace openFile:withApplication:] 等待打开应用程序

发布于 2024-12-27 06:28:31 字数 529 浏览 2 评论 0原文

在我的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

你曾走过我的故事 2025-01-03 06:28:31

您可以收听 通知 从 NSWorkSpace 到达,如下所示

- (void)myMethod {
    [[[NSWorkspace sharedWorkspace] notificationCenter] addObserver:self
                                                       selector:@selector(appDidLaunch:)
                                                           name:NSWorkspaceDidLaunchApplicationNotification
                                                         object:nil];

    [[NSWorkspace sharedWorkspace] openFile:theUrl withApplication:@"Pages"];    
}

- (void)appDidLaunch:(NSNotification*)notification {
    NSLog(@"app info: %@", [notification userInfo]);
}

You can listen for notifications arriving from NSWorkSpace like shown below

- (void)myMethod {
    [[[NSWorkspace sharedWorkspace] notificationCenter] addObserver:self
                                                       selector:@selector(appDidLaunch:)
                                                           name:NSWorkspaceDidLaunchApplicationNotification
                                                         object:nil];

    [[NSWorkspace sharedWorkspace] openFile:theUrl withApplication:@"Pages"];    
}

- (void)appDidLaunch:(NSNotification*)notification {
    NSLog(@"app info: %@", [notification userInfo]);
}
微暖i 2025-01-03 06:28:31

也许您可以使用 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文