Using writing tools in Xojo for Apple Intelligence
You may have heard that newer Macs get some Apple Intelligence features this year. As a developer, we may want to have our users enjoy these features. But in some cases we like to opt-out of the new features. Or provide hints to the system what is appropriate for our applications, so let's check a few of the new properties to configure.
Check for Writing Tools
First you may want to check if writing tools are available:
The WritingToolsAvailable property in NSApplicationMBS class provides a boolean with the status. If you query it on an older macOS or iOS version, on Windows or even on Linux this will always be false. But if the version is new and the feature is enabled by the user, the property turns true.
Show Writing Tools
First you can have a button to show the writing tools for a text area. For this you would get the NSTextViewMBS object and call the showWritingTools method there. The showWritingTools can be called for other controls as well, but most useful is the TextArea:
This should show the panel and let the user improve the text.
We also have a showWritingTools method on NSApplicationMBS to trigger the tools for the current focussed control, if it supports it. Ideal to call from an event handler based on some hotkey combination.
Toolbar
If your application has a toolbar, you can use NSToolbarWritingToolsItemIdentifier as the identifier to add a toolbar button for writing tools.
Please include NSToolbarWritingToolsItemIdentifier as an identifier in the list of allowed identifiers for your toolbar, so the user can add it to the toolbar. Or even include it in the default set of toolbar buttons. The icon and title is provided by Apple automatically.
The writing tools button is the 4th one with the little pen in the circle.
Attributed Text
You can directly work on the attributed text of a TextArea using our NSMutableAttributedStringMBS class. Like adding or removing attributes for text ranges. One of the new things you can use is the NSWritingToolsExclusionAttributeName attribute to disable writing tools for a range of text. Whatever text you mark this way is preserved from changes.
In the following sample code, we take current selection and exclude it for writing tools. To make the change visible, we also set the font to a bold one:
Menus
If you show a contextual menu with NSMenuMBS class, you can configure whether the writing tools show up. The automaticallyInsertsWritingToolsItems property can be set to true to show them or false to hide them.
You may sometimes explicitly want to disable the tools to make a menu shorter when the user doesn't need these tools.
NSTextViewControl events
If the user uses writing tools, you can check the textViewWritingToolsWillBegin and textViewWritingToolsDidEnd events in NSTextViewControlMBS control. These information your application when writing tools start. Any text change between this events may be from Apple Intelligence and not from the user. If you have an undo stack, you may merge all changes in the time as one operation to undo.
The NSTextViewMBS has a property isWritingToolsActive to know if writing tools are active. So you could check on each text change and decide whether the user did it or the AI.
NSTextFieldMBS options
While a text field is usually only used for a line, we got a property there to turn wiring tools on/off. You may want to turn the feature on where useful and turn it off when not. See the allowsWritingTools property in the NSTextFieldMBS class:
NSTextView options
For the text view with NSTextViewMBS class, you can configure writing tools in detail with the writingToolsBehavior property. This can be configure using constants to be None to turn it off, limited to only do a few things and complete for a full featured widget.
The allowedWritingToolsResultOptions property allows you to configure the possible results. This starts with only doing plain text. If you like, you can upgrade to rich text to get text formatting like bold text. Then you can decide whether lists and tables are okay.
For example we can allow all:
Or just a limited inline editing:
Please try the things with new plugins and see what you can get working in Xojo for your application!