Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

OS X FinderSync 'fails' for /Volumes

I'm creating a simple OS X FinderSync that adds a menu item to the control/right-click menu for all files:

[FIFinderSyncController defaultController].directoryURLs = [NSSet setWithObject:[NSURL fileURLWithPath:@"/"]];

It's working great (the menu item appears, etc.) for all files, except those in /Volumes Oddly, if I manually create a directory in /Volumes and add some files there, the FinderSync's menu item appears when I right-click. However for any files in any mounted Volumes (i.e. from a mounted .dmg), it fails: no menu item appears.

Directly specifying a mounted volume in the directoryURLs similarly fails:

[FIFinderSyncController defaultController].directoryURLs = [NSSet setWithObject:[NSURL fileURLWithPath:@"/Volumes/SomeMountedDMG"]];

It seems others have had similar issues, so maybe this is a known bug/limitation?

like image 661
patrick Avatar asked Jul 20 '16 06:07

patrick


1 Answers

The set of subfolders of a folder monitored by a Finder Sync extension does not cross file system boundaries. Although this is not explicitly mentioned in Apple’s documentation, it can be verified empirically (and is still true as of macOS 10.13.3).

As the intended use case for these extensions is to monitor when the Finder displays specific folders being maintained by synchronization utilities like Dropbox, presumably Apple does not see this behavior as a limitation. However, many developers implement Finder Sync extensions as a way of adding arbitrary items to the top-level contextual menu in the Finder (without being constrained to appear in the Services submenu), even though this usage is explicitly discouraged by Apple:

Make sure the Finder Sync extension point is appropriate for the functionality you plan to provide. The best Finder Sync extensions support apps that sync the contents of a local folder with a remote data source. Finder Sync is not intended as a general tool for modifying the Finder’s user interface.

To work around this limitation and make the extension’s menu item available for any item visible in the Finder, it is necessary to do the following:

  1. Scan for all visible mounted volumes, and initialize the directoryURLs property of the FIFinderSyncController object to the result:
    import FinderSync
    
    let finderSync = FIFinderSyncController.default()
    if let mountedVolumes = FileManager.default.mountedVolumeURLs(includingResourceValuesForKeys: nil,
                                                                  options: .skipHiddenVolumes) {
        finderSync.directoryURLs = Set<URL>(mountedVolumes)
    }
    
  2. Since Finder Sync extensions are long-lived processes, register for notifications of volumes being mounted, unmounted, and renamed, and update directoryURLs accordingly:
    let notificationCenter = NSWorkspace.shared.notificationCenter
    notificationCenter.addObserver(forName: NSWorkspace.didMountNotification, object: nil, queue: .main) {
        (notification) in
        if let volumeURL = notification.userInfo?[NSWorkspace.volumeURLUserInfoKey] as? URL {
            finderSync.directoryURLs.insert(volumeURL)
        }
    }
    
    (Handling unmount and rename notifications are left as an exercise for the reader.)
like image 83
rsfinn Avatar answered Oct 10 '22 20:10

rsfinn