Home > HOT Posts!, InDesign Scripts > Prevent executing menu items

Prevent executing menu items

Well, this is almost ‘Most Wanted’ snippet around 😀 Have you ever wanted user to prevent executing some menu item? I did, but I didn’t had answer how to do it. There is no way to disable menu item, but what we can do is to cancel executing it. What we are going to do is to add event to ‘menuActions’. So, let’s take a look.

Best way to find ‘menuActions’ we want to disable is to find item’s ‘ID’ and attach ‘beforeInvoke’ event. Reason why I use ‘ID’ instead of ‘name’ is because I don’t have to think about different InDesign GUI translations, all translations uses same ‘ID’. I made PDF with all ‘menuActions’ items combined with their ‘ID’ and ‘area’ (item place in menu system). It’s for Adobe InDesign CS5.

So, first we are going to add event listener to the ‘menuActions’. We are targeting ‘File/New/Document’ with ‘ID’ 257. Using ‘beforeInvoke’ and forwarding to function ‘stopMenuEvent’.

So, in function, first, we display alert, to warn user that action is disabled. You can skip this one if you want. Second we stop propagation of event with ‘stopPropagation();’, and finally we cancel menu action with ‘preventDefault();’ which is the key part here. Here is the code:

#targetengine disableItems

var disableNew = app.menuActions.itemByID(257).addEventListener("beforeInvoke", stopMenuEvent);
function stopMenuEvent(stopMenuEvent){
    alert("Sorry, this action is disabled!","Disabled menu item!");
    stopMenuEvent.stopPropagation();
    stopMenuEvent.preventDefault();
}

This script is tested only in InDesign CS5, and if you want to create ‘menuActions’ table of other versions, here is the script for collecting all items:

var myActions = app.menuActions;
var myActionsList = Array();
var counter = Number(0);

for(var i = 0; i < myActions.length; i++){
    myActionsList.push(String(myActions[i].name));
    myActionsList.push(String(myActions[i].area));
    myActionsList.push(String(myActions[i].id));
}

var myDoc = app.activeDocument;
var myTextFrame = myDoc.pages[0].textFrames.add();
myTextFrame.geometricBounds = app.activeDocument.pages[0].bounds;

var myMenuActionsTbl = myTextFrame.insertionPoints[0].tables.add();
myMenuActionsTbl.columnCount = 3;
myMenuActionsTbl.bodyRowCount = myActions.length;
myMenuActionsTbl.contents = myActionsList;

That’s it, not big deal, but it’s really useful and handy little feature.

Have fun! 😀

  1. Shonky
    August 20, 2010 at 13:12

    Its very useful information.

    Thanks

  2. Shonky
    August 22, 2010 at 08:48

    I have tried your code on InDesign CS4 Mac and I got an error message “An attached script canceled this action” after alert message.

    Is there any solution to stop this msg?

    Shonky

    • August 22, 2010 at 14:11

      Well, it seems like there is no way to disable that message 😦 I tried it with userInteractionLevel but it doesn’t go away… Maybe I’ll find way to disable that messae…

    • August 22, 2010 at 14:12

      But, on CS5 is working just fine without that message…

  3. Anil Yadav
    September 15, 2010 at 09:00

    Hi Tomaxxi,

    Did you get any solution for the alert message ““An attached script canceled this action””

    Regards,
    Anil Yadav

    • September 15, 2010 at 09:49

      Hey!

      Unfortunately there is no way to turn off that message in pre-CS5 :-/

      tomaxxi

  1. August 20, 2010 at 11:02

Leave a comment