Problem receiving multiple URLs

I am writing an app that can be sent multiple URLs, either by another app sending those URLs, or by someone selecting them in Finder and clicking "Open with…".


Here is the code that receives the URLs :


@NSApplicationMain
class AppDelegate: NSObject, NSApplicationDelegate
{
  func application(_ application: NSApplication, open urls: [URL])
  {
    DataProvider.clear()
    
    for url in urls
    {
      DataProvider.addURL(url)
    }
    
    NotificationCenter.default.post(name: .onDataLoaded, object: nil)
  }


The problem I have is that not all of the URLs are received at the same time, therefore, clearing the DataProvider down as I would want to do after each drop, gets called part way through a single drop.


e.g. I send 9 URLs but only 5 get processed in one call, followed by the other 4 in another call.


I'm guessing this has something to do with asynchronous handling but, apart from setting up a timer to delay clearing DataProvider in the case of a second drop within a given timespan, I'm at a bit of a loss to solve this problem.


Anyone got any better ideas please?

Replies

My current "hack"


class AppDelegate: NSObject, NSApplicationDelegate
{
  lazy var lastImportTime: Date = .init()

  func application(_ application: NSApplication, open urls: [URL])
  {
    let currentImportTime = Date()
    
    let timeInterval = currentImportTime.timeIntervalSince(lastImportTime)
    
    if timeInterval > 1
    {
      DataProvider.clear()
      
      lastImportTime = Date()
    }
    
    for url in urls
    {
      DataProvider.addURL(url)
    }
    
    NotificationCenter.default.post(name: .onDataLoaded, object: nil)
  }


Like I said, this seems quite hacky but it works. Could I do better?

OK, I've found out why the URLs are received in more than one pass.


The files in question are images. Some have modified EXIF data in them, some only have the camera's EXIF data.


AFAICT, the modified files arrive first, followed by the non-modified. Is this to be expected?

When you opening multiple files at once in Preview, it puts them in a single browser window. If you do this with your 9 files, do you get one browser window? Or two?

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"

Hi Quinn


Upon further investigation, it would seem that I am doing something to the files that is causing this.


The files in question are image files (Nikon RAW) and I am using ExifTool to add/remove keywords in the EXIF data embedded in those files.


One of the parameters passed to ExifTool determines how the file is modified when the keywords are written to the file.


According to the author -


"ExifTool creates a temporary file when it is writing, and renames a temporary file to overwrite the original when -overwrite_original is used. Changing this to -overwrite_original_in_place instead causes the original file to be modified"


I was using -overwrite_original


I tried your suggestion and, in Preview, "modified" files are opened in one window whilst those not modified are opened in another.


I have since changed to using -overwrite_original_in_place and the problem has gone away; all files are passed in, in one pass, whether they have been modified or not.


So, my problem is solved for my app (it also then only gives one window in Preview) but I do wonder why passing a list of URLs to the open urls method behaves differently when an original file has been overwritten by a temporary file that has been changed, rather than modifying the original file internally.


At the time when the URLs are passed in, all changes have been made and saved to the files, so it's not like something's happening to them between sending them and them arriving.


Since this affects any app, including Preview, is this something that should not be happening?

It occurs to me that the difference in the files might be that they have different (though compatible) UTIs, and they're being batched based on UTI rather than receiving app.


You see the behavior as a problem, but it's a genuine question why you would expect the URLs all to be delivered in one batch. I'm not aware of anything that makes that API contract. (Also, keep in mind that the delivery is likely not under control of the Finder, but rather of Launch Services. The semantics of "the user selected and opened these together" might get a bit blurry as the URLs traverse multiple subsystems.)

Ah, now that is interesting. So, as a rule, am I better off using that idea of a time interval, just in case?


The guy at ExifTool also commented "I have found MacOS to be quirky when opening a number of files at the same time in Preview. I've seen similar behaviour myself with files not edited by ExifTool"


This starts to feel like unpredictable behaviour that, dare I say, ressembles a bug? 😉