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”