Disable NSTextAttachment Action and Sharing Services Menu Drop-Down

By default, NSTextView will show the NSSharingServicePicker button when you hover over an image inside the text view. That’s true even for custom image-based NSTextAttachments in the attributed string.

The NSSharingServicePicker with some system services showing for an image in TextEdit

The default menu item is limited to “Markup” and a “Services” submenu, I believe. Apps can register to be shown in this menu, and users can customize the menu in System Preferences.

But what if you want to hide this dropdown button thingie completely?

I don’t know why the official means to react to the service picker don’t work, but here’s something that does the trick: override the default behavior in a NSTextView subclass.

class MyTextView: NSTextView {}

extension MyTextView: NSSharingServicePickerDelegate {
    public func sharingServicePicker(
        _ sharingServicePicker: NSSharingServicePicker,
        sharingServicesForItems items: [Any],
        proposedSharingServices proposedServices: [NSSharingService]
    ) -> [NSSharingService] {
        // Deactivate sharing services completely.
        return []
    }
}

In my mind, this is only a temporary quick fix until I figure out why the default pathways don’t trigger as expected. Things to look into include the setup of the NSTextAttachments – do the default pathways work when you use a NSTextAttachment with a FileWrapper instead of a custom cell? Do they work if you declare rich text support on the text view?

If you know anything, I’d love to hear how you tackled this.