Skip to main content
Known Participant
July 13, 2024
Answered

Help me with Script to get text of indesign links

  • July 13, 2024
  • 1 reply
  • 2091 views

Hello!
Could anyone help me with creating a script?
I've tried everything, but I always had mistakes or it didn't go the way I expected.
I have an indesign file with many links to other indesign files. These links are "shuffled" into my larger file, and are activity pages. On each of these links (activity pages), there is a text box applied with the "activity" paragraph style that acts as an identifier.
I need a script that in my "shuffle file", it goes through all the pages in that file and opens the links that are inserted on each page and tells me the text that exists there and has the paragraph style applied. My goal is to identify what activity is included on each page of my jumbled file.
At the end, generate a txt containing the information: Scrambled file page: XX + Text copied from link file page.

This topic has been closed for replies.
Correct answer m1b

Thanks @eusoujpg, those demo files are helpful. You didn't include the expected output, so I will go by your instructions as best I can. Here is a script that collects the info and shows it, in CSV format, in an alert box. You can past in Excel, or change script to save to text file, etc.

- Mark

/**
 * @file Show Activities Info.js
 *
 * Displays linked indesign document information
 * including page number, link name, and content
 * of first 'activities' paragraph-styled paragraph
 * on the linked page.
 *
 * @author m1b
 * @discussion https://community.adobe.com/t5/indesign-discussions/help-me-with-script-to-get-text-of-indesign-links/m-p/14750067
 */
function main() {

    var doc = app.activeDocument,
        links = doc.links;

    var indds = [];

    // step 1: gather the indd links

    for (var i = 0; i < links.length; i++) {

        if (
            links[0].parent.itemLink
            && 'InDesign Format Name' === links[i].parent.itemLink.linkType
        )
            indds.push({ link: links[i], index: i, page: links[i].parent.parentPage });

    }

    // sort by Page index
    indds.sort(function (a, b) { return a.page.documentOffset - b.page.documentOffset });

    // step 2: gather the text of the activity

    app.findGrepPreferences = NothingEnum.NOTHING;

    for (var i = 0, indd, f, d, p, found; i < indds.length; i++) {

        indd = indds[i];
        indd.contents = '-- activity not found --';

        f = File(indd.link.filePath);

        if (!f.exists)
            continue;

        d = app.open(f, false);

        app.findGrepPreferences.appliedParagraphStyle = 'activity';

        p = d.pages.itemByName(String(indd.link.parent.pageNumber));

        if (!p.isValid)
            continue;

        found = d.findGrep();

        for (var j = 0; j < found.length; j++)
            if (found[j].parentTextFrames[0].parentPage === p)
                indd.contents = found[j].contents.replace(/[\n\r]/g, ' ');

        d.close(SaveOptions.NO);
        d = null;

    }

    // step 3: output

    var output = ['Page,Filename,Contents'];

    for (var i = 0, indd; i < indds.length; i++) {

        indd = indds[i];

        output.push(indd.page.name + ',' + indd.link.name + ',' + indd.contents);

    }

    alerter('Activities', output.join('\n'), 1000, 500);

};
app.doScript(main, ScriptLanguage.JAVASCRIPT, undefined, UndoModes.ENTIRE_SCRIPT, 'Show Activities');


/**
 * Show text in a multiline edit text field.
 * @param {String} title - the title of the alerter
 * @param {String|Array} obj - the text content (array of strings will work)
 * @param {Number} [width] - the dialog width
 * @param {Number} [height] - the dialog height
 */
function alerter(title, obj, width, height) {

    width = width || -1;
    height = height || -1;

    if (obj instanceof Array)
        obj = obj.join("\r");

    var w = new Window("dialog", title),
        et = w.add("edittext", undefined, obj, { multiline: true, scrolling: true }),
        closeButton = w.add("button", undefined, "Close", { name: "ok", alignment: ['right', 'center'] });

    et.maximumSize.height = w.maximumSize.height - 100;
    et.minimumSize.height = 350;
    et.minimumSize.width = 350;
    et.preferredSize = [width, height];

    w.show();

};

 

By the way, here is the output using your demo documents:

Page,Filename,Contents
1,ACTIVITS_2.indd,activity 1 “xxxx”
2,ACTIVITS_1.indd,activity 1 “xxxx”
3,ACTIVITS_2.indd,activity 2 “xxxx”
4,ACTIVITS_1.indd,activity 2 “xxxx”
5,ACTIVITS_3.indd,activity 1 “xxxx”
6,ACTIVITS_2.indd,activity 3 “xxxx”
7,ACTIVITS_3.indd,activity 2 “xxxx”
8,ACTIVITS_3.indd,activity 3 “xxxx”
9,ACTIVITS_1.indd,activity 3 “xxxx”

 

1 reply

m1b
Community Expert
Community Expert
July 13, 2024

Hi @eusoujpg can you post your demo files? Before and after script? - Mark

eusoujpgAuthor
Known Participant
July 15, 2024
eusoujpgAuthor
Known Participant
July 20, 2024

Could help? Any suggestion?