Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why NSApplicationDelegate method openFiles: is being called multiple times on a multiple drag to the dock icon?

I have a Mac OS X application that implements the -(void)application openFiles: method to react to dragged files on the application icon.

I have a list of allowed filetypes in the document types section of my target info settings and the Finder indeed allows drags, but when a PDF is in the list of dragged items, my delegate method is called twice: one for all the elements without the PDF, and one for the PDF alone.

This of course makes it impossible for me to handle the situation properly.

Can anybody help me or explain what is happening? Thanks

like image 841
Alfonso Tesauro Avatar asked Jun 03 '16 21:06

Alfonso Tesauro


1 Answers

I've seen this behavior in one of my apps (usually when dragging a whole bunch of files at one time). As I workaround, instead of opening the files directly from application:openFiles:, I queue them up and open the queued files after a small delay. Something like the following:

- (void) application:(NSApplication*)sender openFiles:(NSArray*)filenames
{
    // I saw cases in which dragging a bunch of files onto the app
    // actually called application:openFiles several times, resulting
    // in more than one window, with the dragged files split amongst them.
    // This is lame.  So we queue them up and open them all at once later.
    [self queueFilesForOpening:filenames];

    [NSApp replyToOpenOrPrint:NSApplicationDelegateReplySuccess];
}


- (void) queueFilesForOpening:(NSArray*)filenames
{
    [self.filesToOpen addObjectsFromArray:filenames];
    [self performSelector:@selector(openQueuedFiles) withObject:nil afterDelay:0.25];
}


- (void) openQueuedFiles
{
    if( self.filesToOpen.count == 0 ) return;

    [self makeNewWindowWithFiles:self.filesToOpen];

    [self.filesToOpen removeAllObjects];
}
like image 88
zpasternack Avatar answered Sep 26 '22 00:09

zpasternack