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