Toggling NSMenuItems for a NSSearchField Menu Template
The Problem
Creating NSMenus for NSSearchFields involves specifying a particular NSMenu as the NSSearchField's 'Menu Template'. The problem with this is that the actual NSMenu is not used, a copy is made. The result is that its impossible to access the NSMenu or its NSMenuItems through IBOutlets. Thus, modification of the NSMenuItem's state, title, etc. attributes are not done in the "Standard Way".
The Solution
This wasn't immediately obvious to me from the documentation or API references and caused a good bit of confusion, digging, and googling before I turned up an answer. I'm sure this is in part due to my newness with The Cocoa Way®. Regardless, it deserves a post so I'll remember it.
The magic bullet is to set your controller to the delegate for the NSSearchField and implement the validateMenuItem:. Each time the NSMenu is refreshed, the validateMenuItem is called for each NSMenuItem at which time you can modify its state or attributes.
The Implementation
Here we do a quick implementation to demonstrate the validateMenuItem in action. I'm going to use PyObjc for brevity, but it should suffice to demonstrate the approach.
Comments !