What is the simplest way to invoke the command associated with an existing menu item from a plugin?
Hey there,
My specific task is to bring forward the "Swatches" window. I want to initiate this from my plugin code (C++). However, I may want to execute other menu items in the future, so understanding the simplest, most straightforward general mechanism would be great.
I've pieced together a sequence that could work using the Menu and Interface suites, but it seems more complicated than it should be and requires knowledge of the localized name of the menu item (incomplete and uncompiled sample code below). Is there a better way to do this?
// Get the menu item handle by walking all items to find the swatches item
long numMenuItems = 0;
sAIMenu->CountMenuItems( &numMenuItems );
AIMenuItemHandle menuItemHandle;
ai::UnicodeString localizedItemName;
for ( long menuItemIndex = 0; menuItemIndex < numMenuItems; ++menuItemIndex )
{
sAIMenu->GetNthMenuItem( menuItemIndex, &menuItemHandle );
sAIMenu->GetItemText( menuItemHandle, localizedItemName );
if ( localizedItemName == “Swatches” )
{
break;
}
}
AIBoolean bchecked =false;
sAIMenu->IsItemChecked( menuItemHandle,
&bchecked );
if ( !bchecked )
{
// Find the plugin responsible for adding and responding to the menu item
SPPluginRef swatchPlugin;
sAIMenu->GetMenuItemPlugin( menuItemHandle, &swatchPlugin );
// Construct and send a message to the plugin instructing it to execute as if the menu item were invoke by the user
AIMenuMessage message;
sSPInterface->SetupMessageData( swatchPlugin, &message.d );
message.menuItem = menuItemHandle;
SPErr result;
sSPInterface->SendMessage( swatchPlugin, kAIMenuCaller, kSelectorAIGoMenuItem, &message, &result );
sSPInterface->EmptyMessageData( swatchPlugin, &message.d );
}
Glen.