Skip to main content
Participant
December 4, 2023
Answered

Auto generate layers in InDesign on startup

  • December 4, 2023
  • 3 replies
  • 1368 views

Hi 🙂

 

I need a script which can auto generate default layers in Indesign on startup/new document.

 

Does anybody have a solution?

 

This topic has been closed for replies.
Correct answer Robert at ID-Tasker
quote

Bonjour j'ai testé l'utilisation du script, mais j'ai cette erreur :
JavaScript Erreur !
Numéro de l'erreur : 8
Chaîne de l'erreur : Syntax error
Moteur : main
Fichier : /Users/xxxx/Library/Preferences/Adobe InDesign/Version
19.0-ME/fr_FR/Scripts/startup scripts/Calques-defaut.jsx
Ligne : 1
Source : {\rtf1 \ansi\ansicpg1252\cocoartf2761
Texte incorrect: \
Merci


By @agb83

 

You have not saved it as a PLAIN TEXT.

 

Source: {\rtf1 \ansi\ansicpg1252\cocoartf2761

 

means that you've saved it as RTF.

 

I'm not a Mac user so can't tell you what exactly you should do - but from the "text" editor you are using - you should select PLAIN TEXT or something like that when saving your file as script.

 

3 replies

m1b
Community Expert
Community Expert
December 8, 2023

Hi @clausb13086067, I was interested in this too. I've written a script (with some help) that will do what you say.

 

It needs to be saved as a plain-text file with .js extension—eg. "Make Default Layers.js" and saved into Indesign's Startup Scripts folder (I hope this info is current) and then restart Indesign.

 

When you create a new document it will create the layers specified in the script, which you can edit as needed.

 

Note: there is an annoying shortcoming (between Indesign's UI and event system) which means that the script won't work in two situations:

1. if you do NOT use the "Legacy" New Document Dialog, and

2. if you ARE using the "Legacy" New Document Dialog, and click the preview checkbox from off to on.

If the script fails it will warn you with an alert.

Script is below. Let me know how it goes for you.

- Mark

 

/**
 * Will create default layers as per configured in the `settings` object.
 * To use, adjust the `settings object to suit your needs, and
 * put this script inside Indesign's Startup Scripts folder.
 * 
 * @author m1b
 * @discussion https://community.adobe.com/t5/indesign-discussions/auto-generate-layers-in-indesign-on-startup/m-p/14275846
 * @acknowledgement Many thanks to Marc Autret, for helping with ExtendScript events. I have used his pattern discussed at https://community.adobe.com/t5/indesign-discussions/extendscript-how-to-use-quot-afternew-quot-event/m-p/14282711
 */
(function (ev) {

    // here is where you set up your default layers
    var settings = {

        layers: [
            { name: 'My Layer 1', layerColor: [255, 0, 0] },
            { name: 'My Layer 2', layerColor: [0, 255, 0] },
            { name: 'My Layer 3', layerColor: [0, 0, 255] },
        ],

        removeAllOtherLayers: true,

    };

    var listener;

    if (!(ev || 0).isValid) {

        // Install the event listener (if not yet installed!)
        const UID = 'makeLayers';

        // this script file
        var file = File($.fileName);

        if (
            file.exists
            && !(app.eventListeners.itemByName(UID)).isValid
        ) {
            listener = app.eventListeners.add('afterNew', file);
            listener.name = UID;
        }

        return;

    }

    if ('afterNew' != ev.eventType)
        // not the right event
        return;

    var doc = ev.target || 0;

    if ('Document' != doc.constructor.name)
        // not the right target
        return;

    try {

        // use doScript so that the undo stack is nice and clean
        app.doScript(_makeLayers, ScriptLanguage.JAVASCRIPT, undefined, UndoModes.ENTIRE_SCRIPT, 'Make Default Layers');

    } catch (error) {
        alert('Could not create default layers. This may occur when not using the "Legacy" New Document Dialog preference setting, or when clicking the "Preview" checkbox in the "Legacy" New Document Dialog.');
    }

    function _makeLayers() {

        var layersToCreate = settings.layers,
            layerNames = [];

        // create the layers
        for (var i = 0; i < layersToCreate.length; i++) {
            layer(doc, layersToCreate[i].name, layersToCreate[i].layerColor);
            layerNames[i] = layersToCreate[i].name;
        }

        // delete the other layers?
        if (settings.removeAllOtherLayers === true) {
            for (var i = doc.layers.length - 1; i >= 0; i--)
                if (indexOf(doc.layers[i].name, layerNames) === -1)
                    doc.layers[i].remove();
        }

    };

    /**
     * Returns an existing or new Layer.
     * @param {Document} doc - an Indesign document.
     * @param {String} name - the layer name.
     * @param {Array<Number>} [layerColor] - color breakdown array [red, green, blue].
     * @returns {Layer} an existing or new layer
     */
    function layer(doc, name, layerColor) {

        var layer = doc.layers.itemByName(name);

        if (!layer.isValid)
            layer = doc.layers.add({
                name: name,
                layerColor: layerColor
            });

        return layer;

    };

    /**
     * Returns index of obj in arr.
     * Returns -1 if not found.
     * @param {any} obj
     * @param {Array} arr
     * @returns {Number}
     */
    function indexOf(obj, arr) {
        for (var i = 0; i < arr.length; i++)
            if (arr[i] === obj)
                return i;
        return -1;
    };

})($.global.evt);

 

Participant
December 11, 2023

Thank you so much, it works ! 🙂 

 

I also discovered that Tomaxxilayers works if i choose to use "Legacy" New Document Dialog. 

Colin Flashman
Community Expert
Community Expert
December 4, 2023
If the answer wasn't in my post, perhaps it might be on my blog at colecandoo!
Willi Adelberger
Community Expert
Community Expert
December 4, 2023

I do it either with the library or with templates.