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 (ie 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?
The set of folders monitored by a Finder Sync extension do 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 is to monitor specific folders being maintained by synchronization utilities like Dropbox, presumably Apple does not see this 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:
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) }
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.)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.