5 Replies Latest reply on Aug 27, 2010 10:48 AM by Marijan Tompa [tomaxxi]

    CS3:JavaScript Adding an item to a context menu

    Level 1
      Fellow Scripters,

      Here's an example of a script that adds a menu item to a context menu. The LabelGraphicsMenu example script that's installed with InDesign CS3 also demonstrates this technique, but it's somewhat complicated. This example provides a basic template you can use for creating your own menu items.

      Note that the "Select" menu item doesn't seem to have a locale-independent string (unlike the Layout context menu itself). You will probably need to provide a localized string for the language/locale you need to work with.

      //SelectSameStrokeWeight.jsx
      
      //An InDesign CS3 JavaScript
      //
      #targetengine "session"
      //Adds a menu item to the Select submenu of the Layout context menu. Choose the menu item to select all of the page items on a given page
      //whose stroke weight matches the stroke weight of the selection.
      SelectSameStrokeWeight();
      function SelectSameStrokeWeight(){
      //If you want to make this script a startup script, comment out the next line (i.e., add "//" at the start of the line)
      //and remove the "//" at the start of the following line.
      var myResult = confirm("This script installs a new menu item 'Select Same Stroke Weight' \ron the context menu that appears when you have an object selected.\r\rClick the Yes button to add the menu item.");
      //var myResult = true;
      if(myResult == true){
        //The locale-independent name (aka "key string") for the
        //Layout context menu is "$ID/RtMouseLayout".
        var myLayoutContextMenu = app.menus.item("$ID/RtMouseLayout");
        var mySelectSubmenu = myLayoutContextMenu.submenus.item("Select");
        //Create the event handler for the "beforeDisplay" event of the Layout context menu.
        var myBeforeDisplayListener = mySelectSubmenu.addEventListener("beforeDisplay", myBeforeDisplayHandler, false);
      }
      function myBeforeDisplayHandler(myEvent){
        //Check for open documents is a basic sanity check--
        //it should never be needed, as this menu won't be
        //displayed unless an item is selected. But it's best
        //to err on the side of safety.
        if(app.documents.length != 0){
         if(app.selection.length == 1){
          switch(app.selection[0].constructor.name){
           case "Rectangle":
           case "Oval":
           case "Polygon":
           case "GraphicLine":
           case "TextFrame":
            //Add the menu item if it does not already exist.
            if(myCheckForMenuItem(mySelectSubmenu, "Select Same Stroke Weight") == false){
             myMakeMenuItem();
            }
            break;
           default:
            if(myCheckForMenuItem(mySelectSubmenu, "Select Same Stroke Weight") == true){
             mySelectSubmenu.menuItems.item("Select Same Stroke Weight").remove();
            }
          }
         }
        }
      }
      function myCheckForMenuItem(myMenu, myString){
        var myResult = false;
        try{
         var myMenuItem = myMenu.menuItems.item(myString);
         myMenuItem.name;
         myResult = true
        }
        catch(myError){}
        //alert("Menu item found? " + myResult);
        return myResult;
      }
      function myCheckForScriptMenuItem(myString){
        var myResult = false;
        try{
         var myScriptMenuAction = app.scriptMenuActions.item(myString);
         myScriptMenuAction.name;
         myResult = true
        }
        catch(myError){}
        //alert("Script menu action found? " + myResult);
        return myResult; 
      }
      function myMakeMenuItem(){
        if(myCheckForScriptMenuItem("Select Same Stroke Weight") == false){
         //alert("Making a new script menu action!");
         var myMenuAction = app.scriptMenuActions.add("Select Same Stroke Weight");
         var myEventListener = myMenuAction.eventListeners.add("onInvoke", myEventHandler, false);
        }
        var mySelectSameMenuItem = app.menus.item("$ID/RtMouseLayout").submenus.item("Select").menuItems.add(app.scriptMenuActions.item("Select Same Stroke Weight"));
        function myEventHandler(myEvent){
         //alert("Got to myEventHandler!");
         if(app.selection.length == 1){
          var myObjectList = new Array;
          switch(app.selection[0].constructor.name){
           case "TextFrame":
           case "GraphicLine":
           case "Rectangle":
           case "Oval":
           case "Polygon":
            var myStrokeWeight = app.selection[0].strokeWeight;
            var myParent = app.selection[0].parent;
            for(var myCounter = 0; myCounter < myParent.pageItems.length; myCounter++){
             if(myParent.pageItems.item(myCounter).strokeWeight == myStrokeWeight){
              myObjectList.push(myParent.pageItems.item(myCounter));
             }
            }
            break;
          }
          if(myObjectList.length > 0){
           app.select(myObjectList, SelectionOptions.REPLACE_WITH);
          }
          else{
           alert("Please select a single page item and try again.");
          }
         }  
        }
      }
      }

      Thanks,

      Ole