How does NSServicesMenuRequester work with writing tools?

Asked on 2024-07-29

1 search

NSServicesMenuRequester is a protocol that allows the system to read contents from a view and write contents back to the view. When working with writing tools, you need to ensure that your text view adopts this protocol. Here’s how it works:

  1. Adopt the Protocol: Make sure your text view adopts the NSServicesMenuRequester protocol. This allows the system to interact with the text view for reading and writing content.

  2. Override Methods: Override the validRequestor(forSendType:returnType:) method in NSResponder. This method helps determine if your text view can handle specific types of content.

  3. Implement Write Selection Method: Implement at least the writeSelection(to:type:) method in NSServicesMenuRequester. If your view is an editable text view, also implement the readSelection(from:type:) method to handle rewritten text sent back to the text view.

  4. Context Menu Integration: As long as a context menu is added to the view, the writing tools menu item will be available automatically. This integration allows your app to be available to various services and shortcuts.

By following these steps, you can enable writing tools in your app, making it easier for users to proofread, rewrite, or transform text within your text views.

For more detailed information, you can refer to the session Get started with Writing Tools.