Skip to main content
December 12, 2025
Answered

Textvariable zu Speicherort

  • December 12, 2025
  • 3 replies
  • 653 views

Ich erstelle täglich dutzende Inserate. Auf diesen Inseraten muss die Inserate-Nummer abgedruckt sein.

Der Name der Indesign-Datei lautet nicht wie die Inserate-Nummer. Das zu ändern ist leider keine Option.

Der Speicherordner der Indesign-Datei lautet aber wie die Inserate-Nummer.

Nun war meine Idee, eine Textbox zu erstellen, in der die Textvariable «Übergeordneter Ordner» steht.

Das ist aber leider nicht möglich, da es eine solche Variabel nicht gibt.

 

Hat jemand eine Idee? Ich habe ebenfalls schon versucht, ein Script zu erstellen. Dieses müsste aber manuell ausgelöst werden, was ebenfalls keine Option ist. 

Correct answer m1b

Hi @41291280 oops! I forgot to attach demo.indd. I have posted it now on my original post.

 

Actually I never expected you literally open/close/re-open your real documents. I (wrongly) thought that you had hundreds of ad documents already existing and you wanted to get the parent folder name insert into them so all you needed to do was open them (once!).

 

But it sounds like you are creating new ad documents so a better way would be to use the "afterSave" and "afterSaveAs" events. See modified script below: now it will make the substitution every time you save or saveAs. This time I remembered to attach my demo.indd for you to try it out! Try saving my demo document into different folders—it should update the magenta text accordingly.

- Mark

/**
 * @file Populate With Parent Folder Name.js
 *
 * Will start an "afterSave" event listener which will
 * replace any text with the target paragraph style applied
 * with the name of the document's parent folder.
 *
 * Once the script has been run, opening any document that has
 * the target paragraph style will have the text(s) replaced.
 * Note: the function will only happen when a document is opened.
 *
 * @author m1b
 * @version 2025-12-13
 * @discussion https://community.adobe.com/t5/indesign-discussions/textvariable-zu-speicherort/m-p/15629977/thread-id/644543
 * @acknowledgement I am using Marc Autret's clever pattern for adding event listeners here.
 */
(function (ev) {

    /** the name of the paragraph style to target */
    const PARAGRAPH_STYLE_NAME = 'Parent Folder Name';

    /** Populates the found text with tje document's parent folder's name. */
    function populate(text) {
        text.contents = decodeURI(doc.filePath.name);
    };

    var listener1, listener2;

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

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

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

        if (app.eventListeners.itemByName(UID).isValid)
            app.eventListeners.itemByName(UID).remove();

        if (
            file.exists
            && !(app.eventListeners.itemByName(UID)).isValid
        ) {
            // note: this script file is the 2nd argument
            listener1 = app.eventListeners.add('afterSave', file);
            listener2 = app.eventListeners.add('afterSaveAs', file);
            listener2.name = UID;
        }

        return alert('Listener is running.');

    }

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

    var doc = ev.target || 0;

    if (!(doc instanceof Document))
        // not the right target
        return;

    var targetParagraphStyle = getThing(doc.allParagraphStyles, 'name', PARAGRAPH_STYLE_NAME);

    if (!targetParagraphStyle)
        return;

    // use doScript so that the undo stack is nice and clean
    app.doScript(populateAllTextInParagraphStyle, ScriptLanguage.JAVASCRIPT, undefined, UndoModes.ENTIRE_SCRIPT, 'Auto Populate');

    function populateAllTextInParagraphStyle() {

        app.findGrepPreferences = NothingEnum.NOTHING;
        app.findGrepPreferences.appliedParagraphStyle = targetParagraphStyle;

        var texts = doc.findGrep();

        for (var i = 0; i < texts.length; i++)
            populate(texts[i]);

    };

    /**
    * Returns a thing with matching property.
    * If `key` is undefined, evaluate the object itself.
    * @author m1b
    * @version 2024-04-21
    * @param {Array|Collection} things - the things to look through.
    * @param {String} [key] - the property name (default: undefined).
    * @param {*} value - the value to match.
    * @returns {*?} - the thing, if found.
    */
    function getThing(things, key, value) {

        for (var i = 0; i < things.length; i++)
            if ((undefined == key ? things[i] : things[i][key]) == value)
                return things[i];

    };

})($.global.evt);

 

3 replies

Willi Adelberger
Community Expert
Community Expert
December 13, 2025

Weshalb nimmst du nicht eine Standardtextvariable?

Textvariabe, Typ Name, Pfad und Erweiterung einschließen

December 16, 2025

Weil mit einer Textvariable nur der komplette Speicherpfad eingeschlossen werden kann. Ich benötige aber nur einen Teil des Pfades. Und leider lässt sich nichts ausschliessen. 

m1b
Community Expert
Community Expert
December 13, 2025

@41291280 another interesting approach could be to run an "afterOpen" event listener that performs the substitution automatically when a document is opened (if the document doesn't contain the target paragraph style, the script does nothing).

 

For a demonstration, before running the script open the attached "demo.indd" and save it into a folder called "AD1234567" (just an example fake ad number). At first you will see this:

demo-1.jpg

This document has some text set in a particular paragraph style that the script is looking for.

 

Now run the script. A message appears: "Listener is running." Then close demo.indd and open it again.

 

You will see this:

demo-2.jpg

where the text in magenta has been replaced with the parent folder's name. You can have any number of texts to replace.

 

I hope that is useful. The script listing is below.

- Mark

 

/**
 * @file Populate With Parent Folder Name.js
 *
 * Will start an "afterOpen" event listener which will
 * replace any text with the target paragraph style applied
 * with the name of the document's parent folder.
 *
 * Once the script has been run, opening any document that has
 * the target paragraph style will have the text(s) replaced.
 * Note: the function will only happen when a document is opened.
 *
 * @author m1b
 * @version 2025-12-13
 * @discussion https://community.adobe.com/t5/indesign-discussions/textvariable-zu-speicherort/m-p/15629977/thread-id/644543
 * @acknowledgement I am using Marc Autret's pattern for adding event listeners here - that cleverness is all his!
 */
(function (ev) {

    /** the name of the paragraph style to target */
    const PARAGRAPH_STYLE_NAME = 'Parent Folder Name';

    /** Populates the found text with tje document's parent folder's name. */
    function populate(text) {
        text.contents = decodeURI(doc.filePath.name);
    };

    var listener;

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

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

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

        if (
            file.exists
            && !(app.eventListeners.itemByName(UID)).isValid
        ) {
            // note: using this script file as the 2nd argument
            listener = app.eventListeners.add('afterOpen', file);
            listener.name = UID;
        }

        alert('Listener is running.');
        return;

    }

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

    var doc = ev.target || 0;

    if (!(doc instanceof Document))
        // not the right target
        return;

    var targetParagraphStyle = getThing(doc.allParagraphStyles, 'name', PARAGRAPH_STYLE_NAME);

    if (!targetParagraphStyle)
        return;

    // use doScript so that the undo stack is nice and clean
    app.doScript(populateAllTextInParagraphStyle, ScriptLanguage.JAVASCRIPT, undefined, UndoModes.ENTIRE_SCRIPT, 'Auto Populate');

    function populateAllTextInParagraphStyle() {

        app.findGrepPreferences = NothingEnum.NOTHING;
        app.findGrepPreferences.appliedParagraphStyle = targetParagraphStyle;

        var texts = doc.findGrep();

        for (var i = 0; i < texts.length; i++)
            populate(texts[i]);

    };

    /**
    * Returns a thing with matching property.
    * If `key` is undefined, evaluate the object itself.
    * @author m1b
    * @version 2024-04-21
    * @param {Array|Collection} things - the things to look through.
    * @param {String} [key] - the property name (default: undefined).
    * @param {*} value - the value to match.
    * @returns {*?} - the thing, if found.
    */
    function getThing(things, key, value) {

        for (var i = 0; i < things.length; i++)
            if ((undefined == key ? things[i] : things[i][key]) == value)
                return things[i];

    };

})($.global.evt);

 

December 16, 2025

Das wäre grundsätzlich ein guter Ansatz. Leider habe ich keine Datei "demo.indd" gefunden.

 

Folgendes Problem besteht noch: Ich möchte die Indesign-Datei nicht schliessen und wieder öffnen. Wenn ich das bei jedem Inserat machen muss, generiert das zu viel zusätzlichen Aufwand. 

m1b
Community Expert
m1bCommunity ExpertCorrect answer
Community Expert
December 16, 2025

Hi @41291280 oops! I forgot to attach demo.indd. I have posted it now on my original post.

 

Actually I never expected you literally open/close/re-open your real documents. I (wrongly) thought that you had hundreds of ad documents already existing and you wanted to get the parent folder name insert into them so all you needed to do was open them (once!).

 

But it sounds like you are creating new ad documents so a better way would be to use the "afterSave" and "afterSaveAs" events. See modified script below: now it will make the substitution every time you save or saveAs. This time I remembered to attach my demo.indd for you to try it out! Try saving my demo document into different folders—it should update the magenta text accordingly.

- Mark

/**
 * @file Populate With Parent Folder Name.js
 *
 * Will start an "afterSave" event listener which will
 * replace any text with the target paragraph style applied
 * with the name of the document's parent folder.
 *
 * Once the script has been run, opening any document that has
 * the target paragraph style will have the text(s) replaced.
 * Note: the function will only happen when a document is opened.
 *
 * @author m1b
 * @version 2025-12-13
 * @discussion https://community.adobe.com/t5/indesign-discussions/textvariable-zu-speicherort/m-p/15629977/thread-id/644543
 * @acknowledgement I am using Marc Autret's clever pattern for adding event listeners here.
 */
(function (ev) {

    /** the name of the paragraph style to target */
    const PARAGRAPH_STYLE_NAME = 'Parent Folder Name';

    /** Populates the found text with tje document's parent folder's name. */
    function populate(text) {
        text.contents = decodeURI(doc.filePath.name);
    };

    var listener1, listener2;

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

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

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

        if (app.eventListeners.itemByName(UID).isValid)
            app.eventListeners.itemByName(UID).remove();

        if (
            file.exists
            && !(app.eventListeners.itemByName(UID)).isValid
        ) {
            // note: this script file is the 2nd argument
            listener1 = app.eventListeners.add('afterSave', file);
            listener2 = app.eventListeners.add('afterSaveAs', file);
            listener2.name = UID;
        }

        return alert('Listener is running.');

    }

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

    var doc = ev.target || 0;

    if (!(doc instanceof Document))
        // not the right target
        return;

    var targetParagraphStyle = getThing(doc.allParagraphStyles, 'name', PARAGRAPH_STYLE_NAME);

    if (!targetParagraphStyle)
        return;

    // use doScript so that the undo stack is nice and clean
    app.doScript(populateAllTextInParagraphStyle, ScriptLanguage.JAVASCRIPT, undefined, UndoModes.ENTIRE_SCRIPT, 'Auto Populate');

    function populateAllTextInParagraphStyle() {

        app.findGrepPreferences = NothingEnum.NOTHING;
        app.findGrepPreferences.appliedParagraphStyle = targetParagraphStyle;

        var texts = doc.findGrep();

        for (var i = 0; i < texts.length; i++)
            populate(texts[i]);

    };

    /**
    * Returns a thing with matching property.
    * If `key` is undefined, evaluate the object itself.
    * @author m1b
    * @version 2024-04-21
    * @param {Array|Collection} things - the things to look through.
    * @param {String} [key] - the property name (default: undefined).
    * @param {*} value - the value to match.
    * @returns {*?} - the thing, if found.
    */
    function getThing(things, key, value) {

        for (var i = 0; i < things.length; i++)
            if ((undefined == key ? things[i] : things[i][key]) == value)
                return things[i];

    };

})($.global.evt);

 

Peter Kahrel
Community Expert
Community Expert
December 12, 2025

If you have a script that inserts the advertisement number in a document, you can use a batch processor to run that script against folderfuls of documents. Two such scripts are

https://kasyan.ho.ua/indesign/batch_process_scripts/batch_process_scripts.html

https://creativepro.com/files/kahrel/indesign/batch_convert.html

 

December 16, 2025

Danke für den Input. Leider ist mein technisches Verständnis und Englisch nicht gut genug um das auszuführen.