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);
    exit();
    }

var templateDoc = app.activeDocument;

var layerNameArray = ["Fixed_Static_Assets", "Absolute_User_Assets", "Relative_User_Assets"];
CreateLayers(layerNameArray);

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");
    if(defaultLayer.isValid==true){
        //$.writeln('Default is Valid and will be merged with top most layer');
        layerObject.merge([defaultLayer]);
        }
    }

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

Green4ever

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.

@+

Marc

Tags: InDesign

Similar Questions

Maybe you are looking for

  • Photos transferred via USB use optimize the functionality for iCloud photo library?

    Hello I recently transferred 1000 files to my mac via a flash drive. I want to have optimized photos and not actually be fully downloaded on my mac and I'm currently using iCloud photo library with optimization turned on. Does the work automatically?

  • How to send data from the client to the server using tcp

    1. as I am new to watch I need help with sending data from client to server. I went through various examples of labview, but each of them also explains transfer of data to the client server. But I need help regardng send data from the server to the c

  • Order of execution with waveform of reading

    I'm trying to find a way to control the order in which the sound files are played when Set up like that. It does not run them up and down as I thought it might. How can I control this order? Im sure that there is a better way to connect as well, I'm

  • Questions about service packs

    My friends laptop resonance had some problems bad windows which culminated in him having to reinstall windows. I have an Edition Vista Home premium 64-bit SP1 CD, which is what he needs. We could get is the product key and what version of windows he

  • Replace all English win 10 French Win 7 Ultimate

    If I buy a version in French of Windows 7 Ultimate, can I change it to English and then he move to only English Windows 10?