Skip to main content
Participant
June 25, 2009
Question

What is the simplest way to invoke the command associated with an existing menu item from a plugin?

  • June 25, 2009
  • 1 reply
  • 996 views

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.

This topic has been closed for replies.

1 reply

A. Patterson
Inspiring
June 25, 2009

That's a clever solution to a problem I'd never considered (but can see how others might need it) I can't think of a better way to invoke it though than what you're doing except maybe with some minor improvements. If you know the menu group, you can use AIMenuSuite::GetMenuGroupRange() to jump to that group in the overall list. Also, since menuitems seem extremely unlikely to change, you can probably cache the handle.

Beyond that, the usual suggestion I'd make it is to at least explore actions. Try recording an action and see if it even picks up when you display the Swatches panel. I'm guessing it doesn't record stuff like that, but who knows?