gpt4 book ai didi

cocoa - -[NSWorkspace openFile :withApplication:] wait for opening Application

转载 作者:行者123 更新时间:2023-12-03 16:18:00 26 4
gpt4 key购买 nike

在我的 Obj-C 应用程序中,我使用以下代码在 Pages(或任何其他应用程序)中打开文件:

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

主要是当打开较大的文件时,这可能需要几秒钟才能完成。所以我希望我的应用程序等待页面,直到它完全打开文件。

以下代码是我想要的方式:

[[NSWorkspace sharedWorkspace] openFile:theUrl withApplication:@"Pages" onFinish:@selector(pagesfinishedopening)];

当然,我可以简单地使用 sleep() 函数,但这会减慢小文件上的应用程序的速度,并且当文件大于异常(exception)时将无法工作。

我已经尝试过使用 NSApplication 进行一些操作,但是页面中文件的打开不受尊重,只能监视目标应用程序的启动。

有什么想法吗?

最佳答案

您可以收听notifications从 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]);
}

关于cocoa - -[NSWorkspace openFile :withApplication:] wait for opening Application,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8897817/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com