Skip to main content
Community Expert
October 30, 2025
Question

Script check each page

  • October 30, 2025
  • 1 reply
  • 222 views

I'm working on a script to compare the text on a page with the filename of a specific layer. Here's the issue I'm running into:

  • For example, I have a text frame on a layer named Code.

  • The script can find it if it's present on Page 1 or Page 2—that part works fine.

  • However, it doesn't report when the text frame is missing from Page 1 or Page 2.

It seems to handle Parent Pages correctly. For instance, if the Code layer contains the text in the same position on each page via the Parent Page, the script detects it and doesn’t need to take any action.

The problem arises when the text frame is not on parent pages. For example:

  • On Page 1, the text frame is in the top right.

  • On Page 2, it’s in the bottom left.


Then it's detecting text on the layer, so it stops as it's found. 
So page 1 might have the text for the code
and page 2 doesn't have text for the code - but the script is not finding it. 

Does this make sense?

1 reply

rob day
Community Expert
Community Expert
October 30, 2025

Hi @Eugene Tyson , Could you post a sample doc with the text frames and layers?

Community Expert
October 30, 2025

Sure 

 

1. Trying to check the parent page for a text variable or text that matches the filename

2. Trying to check the page 1 for a variable, or for text matches the filename
3. When on page 1 and not page 2 (or vice versa) it doesn't recognise as it finds it on one of the pages

Something like the attached. 

For example, the text variable isn't always on the parent page. But it picks up the filename. 

So I'm trying to catch on all pages where the code is not the filename either by text or by text variable. 

Community Expert
October 30, 2025

This is as far as I got with the code - when i got into trouble I tried flushing it through ChatGPT and Gemini - but neither could resolve the issue or figure out why it's not working. 

This is literally where I have left off. 

#targetengine "textVariableCheckEngine"

(function () {
    var enableEventListener = false; // Set true later if you want automatic check on save

    if (enableEventListener) {
        var handlerAlreadyAdded = true;
        for (var i = 0; i < app.eventListeners.length; i++) {
            if (app.eventListeners[i].name === "textVariableCheckHandler") {
                handlerAlreadyAdded = true;
                break;
            }
        }
        if (!handlerAlreadyAdded) {
            var el = app.addEventListener("beforeSave", textVariableCheck, false);
            el.name = "textVariableCheckHandler";
        }
    }

    textVariableCheck(); // Run immediately for manual test

    function textVariableCheck() {
        try {
            if (app.documents.length === 0) return;

            var doc = app.activeDocument;
            var usedVariables = [];

            // --- Scan pages and master spreads ---
            function scanContainer(container) {
                for (var p = 0; p < container.length; p++) {
                    var page = container[p];
                    var pageItems = page.allPageItems;
                    for (var i = 0; i < pageItems.length; i++) {
                        var item = pageItems[i];
                        if (item.hasOwnProperty("textVariableInstances")) {
                            var instances = item.textVariableInstances;
                            for (var j = 0; j < instances.length; j++) {
                                var v = instances[j];
                                if (v && v.associatedTextVariable && v.associatedTextVariable.name != null) {
                                    var nameStr = String(v.associatedTextVariable.name);
                                    // Classic loop duplicate check
                                    var alreadyAdded = false;
                                    for (var k = 0; k < usedVariables.length; k++) {
                                        if (usedVariables[k] === nameStr) {
                                            alreadyAdded = true;
                                            break;
                                        }
                                    }
                                    if (!alreadyAdded) usedVariables.push(nameStr);
                                }
                            }
                        }
                    }
                }
            }

            scanContainer(doc.pages);
            scanContainer(doc.masterSpreads);

            if (usedVariables.length === 0) {
                alert("⚠ No text variables found in pages or master pages.");
            }

            // --- Code Layer check ---
            var fileName = doc.name.replace(/\.[^\.]+$/, "");
            var techMismatch = false;
            var techFound = false;

            for (var t = 0; t < doc.textFrames.length; t++) {
                var tf = doc.textFrames[t];
                if (tf.itemLayer && /Code/i.test(tf.itemLayer.name)) {
                    techFound = true;

                    // Ignore frames that have text variables
                    if (tf.textVariableInstances.length > 0) continue;

                    // Clean content for comparison
                    var content = String(tf.contents)
                        .replace(/[\s\u00A0\u200B-\u200D\uFEFF]/g, "")
                        .replace(/[^\x20-\x7E]/g, "")
                        .toLowerCase();
                    var cleanFile = String(fileName)
                        .replace(/[\s\u00A0\u200B-\u200D\uFEFF]/g, "")
                        .replace(/[^\x20-\x7E]/g, "")
                        .toLowerCase();

                    if (content.indexOf(cleanFile) === -1) {
                        techMismatch = true;
                        break;
                    }
                }
            }

            if (!techFound) {
                alert("⚠ No text found on the Code layer.");
            } else if (techMismatch) {
                alert("⚠ Code layer text does NOT match the filename: " + fileName);
            }

            // Silent if all is okay

        } catch (e) {
            alert("Error in textVariableCheck: " + e);
        }
    }
})();