Skip navigation
PaulR72
Currently Being Moderated

[CS4] [JS] Which item does a panel context menu action work on?

Jun 1, 2010 1:35 AM

Hi there,

 

I'm trying to write a script that operates on a paragraph style. But rather than go through InDesign's objects to find the paragraph style, I want the user to select it by right clicking it in the styles panel.

 

This (working) code adds a menu item to the Paragraph Styles Context Menu (thanks Jongware, Steven and Harbs for your help with this last week):

 

   #targetengine "session"

   aScriptAction = app.scriptMenuActions.add("Browse");
   aEventListener = aScriptAction.eventListeners.add("onInvoke", aEventHandler, false); 
   aMenu = app.menus.item("$ID/RtMenuStyleListItem");   // = "Text Style List Context Menu" menu text in InDesign English version.
   aCustomMenuItem = aMenu.menuItems.add(aScriptAction);

 

aEventHandler() is called properly when I right-click on a paragraph style in the panel, and then select the "Browse" option created above. So far so good.

 

But the problem now is that I can't find the index or id of the paragraph style in the panel that was right-clicked on. An event object is passed to aEventHandler(), but I can't find any variables or methods in this event, or within any of the objects it points to (say, with Event.currentTarget or Event.target), that can give this information. Attempted many searches on the net too, no result.

 

Hope someone can give me a pointer!

 

Bye, Paul

 
Replies
  • Currently Being Moderated
    Jun 1, 2010 7:08 AM   in reply to PaulR72

    I don't know if it is available. In C++ it would be in the "this" property, because the event calling the menu is attached to listbox item, but I don't know how it would appear in Javascript

     
    |
    Mark as:
  • Currently Being Moderated
    Jun 1, 2010 7:30 AM   in reply to PaulR72

    Probably a ScriptMenuAction ("A script menu action. Base Class: MenuAction").

     

    A MenuItem has a MenuAction, with which it is "associated". The parent of a MenuAction is the Application itself, so I think this means it has been implemented as an internal "list of functions" which can then be activated by a menu item. In other words: the Item is not the Action, you have to associate them. (That's not un-like the hot key system; in fact, it might be more than superficially similar.)

     

    Checking MenuAction in my own version of the Help, I immediately noticed it's a superclass of "ScriptMenuAction", and since that has a list of properties that smell like meta-scripting (event listeners and such), I guess that's what one ought to use.

     
    |
    Mark as:
  • Currently Being Moderated
    Jun 1, 2010 7:33 AM   in reply to PaulR72

    I don't believe panels are scriptable in this way.

     

    Make a feature request...

     

    Harbs

     
    |
    Mark as:
  • Currently Being Moderated
    Jun 1, 2010 7:37 AM   in reply to [Jongware]

    Fascinating. It seems there are more than 3,200 "menu actions" in InDesign! That's 73 letter size pages ...

     

    (Although your number may vary: a large part of the list seems to be the full font list, including all its bold and italic etc. variants. Still an impressive list, tho'.)

     

    This quick hack puts them into a selected text frame:

     

    res = [];
    for (a=0; a<app.menuActions.length; a++)
     res.push (app.menuActions[a].name+" ("+app.menuActions[a].area+")");
    app.selection[0].contents = res.join("\r");
    
     
    |
    Mark as:
  • Currently Being Moderated
    Jun 2, 2010 8:50 AM   in reply to PaulR72

    Pfff. It took half an hour and now I have 10 "Browse" items in my Paragraph panel, but I think I got it.

     

    This event handler will show the name of the current selected paragraph. Oh sure, it's a dirty trick, but hey! it works!

     

    function aEventHandler (parent)
    {
     m = app.menus.item("Text Style List Context Menu");
     alert (m.menuItems[0].name.match(/"(.+)"/)[1] );
    }
    

     

    I noticed too late I could have used the $ID/xxx code but I'm not running it again. How in the world do I get rid of all those "Browse" items ...

     
    |
    Mark as:
  • Currently Being Moderated
    Jun 2, 2010 9:22 AM   in reply to [Jongware]

    Hey Jong,

     

    Nice thinking! It'll work as long as all the style names are unique...

     

    Your extra menu items should disappear when you restart InDesign twice.

     


    Harbs

     
    |
    Mark as:
  • Currently Being Moderated
    Jun 4, 2010 1:46 AM   in reply to PaulR72

    As a dirty trick, it comes with a cave constructor ("watch out, programmer").

     

    As Harbs pointed out, it'll not work if you have the same style name in another group ... I never use groups so I didn't realize it, but it seems to be perfectly possible.

    Another one is, for some reason it's all out of whack if you right-click a group header. It simply repeats the name of the last style you right-clicked.

     

    Oh -- and, quite unexpectedly really, I tested with the paragraph styles but it appears the character style panel uses the exact same menu!

     
    |
    Mark as:

More Like This

  • Retrieving data ...

Bookmarked By (0)

Answers + Points = Status

  • 10 points awarded for Correct Answers
  • 5 points awarded for Helpful Answers
  • 10,000+ points
  • 1,001-10,000 points
  • 501-1,000 points
  • 5-500 points