gpt4 book ai didi

macos - 如何收集多个openFile :(NSString *) events and then do something with them?

转载 作者:行者123 更新时间:2023-12-03 16:17:21 25 4
gpt4 key购买 nike

我正在开发 Cocoa/Objective C++ 实用程序应用程序,并且想要处理多个(瞬时)文件打开。为了处理文件打开部分,我在 AppDelegate.mm 文件中有这个函数:

- (BOOL)application:(NSApplication*)app openFile:(NSString *)filename
{
NSLog(@"Opening file %@", filename);

// more C++ code here

return YES;
}

这些文件正在发送到 Quicksilver,我相信 Quicksilver 会将文件一个接一个地发送到应用程序(我认为 Finder 的“打开方式”会一次发送所有文件 - 因此可能使用 handleOpenApplicationEvent:(NSAppleEventDescriptor *)event 可能更适合这一点) - 但无论如何,应用程序将一个接一个地接收多个文件 - 几乎是瞬时的 - 但不完全一起。以下是 Console.app 显示的内容:

console-screenshot.png http://img109.imageshack.us/img109/1205/consolescreenshot.png

由于文件是单独发送的,但是一个接一个地发送,我的问题是如何收集所有发送的文件并用它们做一件事?例如,我想收集发送的所有文件路径,然后将它们一起显示在对话框中。

我可以想到一个可能有效的概念:在接收到每个文件路径时将其存储在数组中。同时,当收到第一个时,在后台设置 1-2 秒的延迟,然后有一个对话框显示特定变量的所有内容。这个概念正确吗?或者有更好的方法吗?我是 Cocoa/Objective C++ 的新手 - 来自 PHP/Perl、bash 等语言。

我还想让这个辅助应用程序在完成后终止,但如果我正在等待文件,如何在收到最后一个文件时终止该应用程序?

如果我将 [NSApp Terminate:nil]; 添加到 applicationDidFinishLaunching 函数,则应用程序实际上会在仅收到第一个文件后终止。

更新 - 更多注释

这是使用openFiles(复数)函数后的控制台: Picture 7.png http://img24.imageshack.us/img24/5715/picture7zc.png

使用 Quicksilver,我抓取一些文件,例如临时目录中的文件,然后选择“打开方式”,然后选择我正在创建的应用程序(名为 darn.app)

Screen Shot 2012-01-18 at 7.02.45 PM.png http://img9.imageshack.us/img9/4171/screenshot20120118at702.png

但使用 Finder 似乎效果很好:

Screen Shot 2012-01-18 at 7.09.08 PM.png http://img832.imageshack.us/img832/8858/screenshot20120118at709.png

...consolea.app 中的结果:

Picture 8.png http://img24.imageshack.us/img24/2812/picture8fe.png

我确实询问了 Quicksilver 开发人员,他们确认 QS 确实通过“打开方式”操作一次打开一个文件,并且他们承认这与 Finder 的做法不同,但他们认为这是预期的。所以也许达伦是对的 - 计时器可能是最好的方法......

最佳答案

尝试实现 -application:openFiles: 而不是 -application:openFile:

- (void)application:(NSApplication *)sender openFiles:(NSArray *)filenames
{
NSLog(@"Opening multiple files %@", filenames);
}

它处理从 Finder 接收多个文件。

如果这不适用于您的 Quicksilver 设置,那么计时器可能是最好的方法。

在 AppDelegate 的 -init 方法中设置计时器。

@implementation MyAppDelegate {
NSMutableArray* _files;
}

- (id)init {
if (self = [super init]) {
_files = [[NSMutableArray alloc] initWithCapacity:10];

[self performSelector:@selector(processFiles)
withObject:nil
afterDelay:1.0]; // one second delay
}
return self;
}

在-application:openFile中:将文件收集到数组中并重置计时器。

- (BOOL)application:(NSApplication*)app openFile:(NSString*)filename {
NSAssert(_files != nil, @"Timer already fired");

[_files addObject:filename];

[NSObject cancelPreviousPerformRequestsWithTarget:self];
[self performSelector:@selector(processFiles)
withObject:nil
afterDelay:1.0];
}

当计时器启动时,处理收集的文件,然后退出应用程序。

-(void)processFiles

// Process the _files

[_files release];
_files = nil;
[NSApp terminate:nil];
}

关于macos - 如何收集多个openFile :(NSString *) events and then do something with them?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8917874/

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