Bug? : layers.add () does not work correctly when called from the menu (ID/CS6/Win7)

Hello Devs,

I am facing a mysterious problem with the layers.add () method.

I am trying to create n number of layers for my document. So I decided to create a menu for that.

I created two scripts

1. for creating menu & menu action.

2. real script that creates n number of layers.

When I run the script #2 of ESTK directly it works fine.

Now when I try to call the same script in the Menu it does work very well. Only the last layer is created.

Script 1: LayerPopupMenuAction

#target indesign
#targetengine createLayerset
var layerTemplateScript = File(File(getActiveScriptPath()).parent.fsName+"/IntializeTemplate.jsx"); 

var initTemplateHandler = {
    'beforeDisplay' : function(ev)
            ev.target.enabled = (app.documents.length>0);
    'onInvoke' : function()
            app.doScript(layerTemplateScript, ScriptLanguage.JAVASCRIPT, undefined, UndoModes.FAST_ENTIRE_SCRIPT, "Initialize Script");

var layerMenuAction = app.scriptMenuActions.add("&Create Layer Set");

for(var init_ev in initTemplateHandler) {
    layerMenuAction.eventListeners.add(init_ev, initTemplateHandler[init_ev]);

var refMenuItem = app.menus.item("$ID/LayerPanelPopup");
refMenuItem.menuItems.add(layerMenuAction, LocationOptions.AFTER, refMenuItem.menuItems.item(2));

function getActiveScriptPath() {
    // This function returns the path to the active script, even when running from ESTK
    try { 
        return app.activeScript; 
    } catch(e) { 
        return e.fileName; 

Script 2: IntializeTemplate

#target indesign
if (app.documents.length == 0){
    alert("Please open a document and try again", "Document Error", true);

var templateDoc = app.activeDocument;

var layerNameArray = ["Fixed_Static_Assets", "Absolute_User_Assets", "Relative_User_Assets"];

alert("Initialization Successful");

//--------------------------------FUNCTIONS ARE DEFINED HERE--------------------------------
function CreateLayers(LayerNameList){
    var layerListLength = LayerNameList.length;
    var layerObject;
    for (var ln=0; ln<layerListLength; ln++){
        layerObject = templateDoc.layers.itemByName(LayerNameList[ln]);
        if (layerObject.isValid==false){
            layerObject = templateDoc.layers.add({name:LayerNameList[ln]});
            //$.writeln('Layer Creation: ' + layerObject.isValid + ' for '+layerObject.name);
    //Merger Default Layer with the bottom layer:
    var defaultLayer = templateDoc.layers.itemByName("Layer 1");
        //$.writeln('Default is Valid and will be merged with top most layer');

Any help / advice on this topic will be useful.

@Marijan Tompa [tomaxxi]: I googled for the solution and I noticed that you have already worked on the same type of script. Please help me if you can.

Thank you



Hi Green4ever,

I think at a first sight, UndoModes.FAST_ENTIRE_SCRIPT , who, as you may know, can scramble deeply the script steps, especially when one ... try statement catch is used.

Try to replace FAST_ENTIRE_SCRIPT by ENTIRE_SCRIPT and tell us.



Tags: InDesign

Similar Questions

Maybe you are looking for

  • Satellite A60 and wireless card

    What card can I use for my laptop so that I can connect to the internet wireless, at the University or in other places of the internet enabled. Really need help don't understand something on the wireless cards and also could you recommend all those w

  • upgrade envy 15 k0006tx

    Hello Please provide details How can I increase the RAM? And if I can add ssd drive? Thank you

  • Computer runs hot

    My pavilion dv2-1100 goes hot almost just to start. fan is running hard and strong.  What can cause this and how can I solve this problem?

  • How can I install Windows 8 preview of consumers on a Vista PC that won't start?

    Hello world I want to install Windows 8 because I've tried everything to fix my desktop using Windows Vista pc. Windows will not start even AFTER a full restoration of a Windows PC. I've performed to repair the start several times without success. Sa

  • I want to be able to hear my voice in my headset that I speak

    Original title: Microphone problems I'm trying to record my voice on my laptop. I want to be able to hear my voice in my headset that I speak, but no matter what tutorial I try, the parameters do not seem to apply to me. I have Windows Vista on an As