• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
6

Auto generate layers in InDesign on startup

New Here ,
Dec 04, 2023 Dec 04, 2023

Copy link to clipboard

Copied

Hi 🙂

 

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

 

Does anybody have a solution?

 

TOPICS
Scripting

Views

595

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 2 Correct answers

Community Expert , Dec 07, 2023 Dec 07, 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 even

...

Votes

Translate

Translate
Community Expert , Apr 14, 2024 Apr 14, 2024
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 no

...

Votes

Translate

Translate
Community Expert ,
Dec 04, 2023 Dec 04, 2023

Copy link to clipboard

Copied

I do it either with the library or with templates.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Dec 04, 2023 Dec 04, 2023

Copy link to clipboard

Copied

Try this script from Marijan Tompa aka Tomaxxi:

https://indisnip.wordpress.com/2011/02/25/script-tomaxxilayers%C2%AE-add-layer-sets-to-document/

 

If the answer wasn't in my post, perhaps it might be on my blog at colecandoo!

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Dec 07, 2023 Dec 07, 2023

Copy link to clipboard

Copied

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.

Screenshot 2023-12-08 at 11.02.11.pngScreenshot 2023-12-08 at 11.23.38.png

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);

 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Dec 11, 2023 Dec 11, 2023

Copy link to clipboard

Copied

Thank you so much, it works ! 🙂 

 

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Apr 14, 2024 Apr 14, 2024

Copy link to clipboard

Copied

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Apr 14, 2024 Apr 14, 2024

Copy link to clipboard

Copied

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.

 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Apr 14, 2024 Apr 14, 2024

Copy link to clipboard

Copied

LATEST

Mais, oui bien sûr ! Merci
RTF > shift+commnad+t > TXT

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines