Copy link to clipboard
Copied
Hi everyone, I need a script to find text by formatting and insert Cross-references to content/Paragraph Text present in a Master page. So, all text found in the document with the formatting attribute paragraph style (e.g:) "Contents" applied must have its own cross-reference - the paragraphs in both places (in the document and master pages) match, but they're not linked. Any help will be greatly appreciated.
Copy link to clipboard
Copied
You can create multiple cross references FROM a masre/parent to content on pages. But it is impossible to link. Ross references TO master/parents the master/parent will appear on several pages in the fresulzing document but a cross reference can only lead to a single target.
Copy link to clipboard
Copied
Hi @Rogerio5C09 you'd need to post an example .indd document showing before and after, so we'd understand *exactly* what you want.
- Mark
Copy link to clipboard
Copied
Hi @m1b, thanks for checking! 🙂 Please find INDD files attached - in the "after" document, you will find the cross-reference for each text/paragraph already set in the Cross-References panel.
Copy link to clipboard
Copied
I found these posts this morning - maybe we could use it a reference for the script?
https://community.adobe.com/t5/indesign-discussions/indesign-javascript-add-cross-reference-at-index...
https://community.adobe.com/t5/indesign-discussions/how-create-crossreference-link-to-paragraphs-or-...
Copy link to clipboard
Copied
Creating Cross-References isn't a problem - but first - we need know exactly what should be the source and destination.
And if you want to override objects from the Master - then would be great if you tag / mark them - then will be much easier to link the copy on the page with the original on the Master.
But I don't think this link will work in the PDF?
I've exported your file to interactive PDF - and it didn't work.
Copy link to clipboard
Copied
That's not a problem, the hyperlinks are applied to the frames in the original document. We need to Cross-Ref the text inside the frames/buttons only to the ones in the master. Let me know if that's possible by any chance.
Copy link to clipboard
Copied
Hi @Rogerio5C09 I had a look at your sample documents, but I'm not sure that cross references are right for your situation. I think they will fight you the whole time because of the trouble with cross-references and master pages.
I had another idea. What if you set up your master pages using "Button Passive" Object Styles for the buttons and then, on the actual pages, (a) overrode the current page's button and (b) applied a "Button Active" object style to it, which could also include paragraph styling to turn the text white.
Then, whenever the master page text content changes—and of course the active button text content did not update because it was overridden—you could run the below script, which will synchronise them. I've configured the script to attempt to synchronize any text in "Contents Active" paragraph style if it is in a text frame that was overridden.
Have a look at the attached demo.indd to see how I've set it up. Compared to your sample document I've just applied two paragraph styles to the text and two object styles to the "buttons". Which you should do anyway, even without the script! 🙂
- Mark
/**
* @file Update Overridden Master Texts.js
*
* Synchronizes contents of specified styled paragraphs
* if they have been overridden from a master page.
*
* @author m1b
* @version 2025-04-23
* @discussion https://community.adobe.com/t5/indesign-discussions/find-text-by-formatting-and-insert-cross-references-to-paragraph-text-in-a-master-page/m-p/15283426
*/
function main() {
var settings = {
/** any text found in these styles will have their contents synchronized */
targetStyleNames: ['Contents Active'],
/** whether to synchronize other text attributes beyond just the contents */
synchronizeAllTextAttributes: false,
/** whether to show results after running */
showResult: true,
};
var doc = app.activeDocument;
// confirm the paragraph styles are valid
var paragraphStyles = [];
for (var i = 0, style; i < settings.targetStyleNames.length; i++) {
style = getThing(doc.allParagraphStyles, 'name', settings.targetStyleNames[i]);
if (!style)
return alert('Document does not have the "' + settings.targetStyleNames[i] + '" style.');
paragraphStyles.push(style);
}
var found = [];
// find all contents in the styles
for (var i = 0; i < paragraphStyles.length; i++)
found = found.concat(findText(doc, { appliedParagraphStyle: paragraphStyles[i] }));
if (0 === found.length)
return alert('No text found in styles [' + settings.targetStyleNames + '].');
var counter = 0;
// update any text that's been overridden from master page
for (var i = found.length - 1; i >= 0; i--) {
if (0 === found[i].parentTextFrames.length)
// ignore overset paragraph
continue;
if (!found[i].parentTextFrames[0].overriddenMasterPageItem)
// ignore text with no master page item
continue;
if (settings.synchronizeAllTextAttributes) {
// duplicate the text of the master item
var insertionPoint = found[i].parentStory.insertionPoints.item(found[i].insertionPoints[0].index);
found[i].contents = '';
found[i].parentTextFrames[0].overriddenMasterPageItem.texts[0].duplicate(LocationOptions.AT_END, insertionPoint)
}
else
// just set the contents
found[i].contents = found[i].parentTextFrames[0].overriddenMasterPageItem.contents;
counter++;
}
if (settings.showResult)
alert('Synchronized ' + counter + ' texts.');
};
app.doScript(main, ScriptLanguage.JAVASCRIPT, undefined, UndoModes.ENTIRE_SCRIPT, 'Update Overridden Texts');
/**
* Returns result of search in the supplied `findIn` object
* using its findText method.
*
* Example:
* var headingParas = findText(doc, { appliedParagraphStyle: headingStyle });
*
* @author m1b
* @version 2025-04-18
* @param {Document|Text} findIn - any object with a valid `findText` method.
* @param {Object} findWhat - an object containing findChangeTextOptions properties.
* @returns {Array<Text>}
*/
function findText(findIn, findWhat) {
if ('function' !== typeof findIn.findText)
throw new Error('findText: bad `where` supplied.');
// configure the find
app.findChangeTextOptions.properties = {
caseSensitive: false,
includeFootnotes: true,
includeHiddenLayers: false,
includeLockedLayersForFind: false,
includeLockedStoriesForFind: false,
includeMasterPages: true,
wholeWord: false,
};
// reset
app.findTextPreferences = NothingEnum.NOTHING;
// configure the find with settings
for (var key in findWhat) {
if (
findWhat.hasOwnProperty(key)
&& app.findTextPreferences.hasOwnProperty(key)
)
app.findTextPreferences[key] = findWhat[key];
}
// perform the find
return findIn.findText();
};
/**
* 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, obj; i < things.length; i++)
if ((undefined == key ? things[i] : things[i][key]) == value)
return things[i];
};
Copy link to clipboard
Copied
Copy link to clipboard
Copied
Here is an example of the filtered out text structure of your document:
As you're working on Windows - I can give you access to my tool for a few days for free - I'll even help you create Tasks.
But again - why do you need to Cross-Ref to something on the Master?
Or if you plan on doing it as buttons - here is object structure of your document:
Copy link to clipboard
Copied
Hi @Robert at ID-Tasker, the blue buttons on pages 1-4 are not in the Master Page because they are also headings in the original document. If I just remove them and use the ones from the master, the headings disappear from the automated TOC, which is not ideal. Adding Cross-References is work around here, a way to keep them linked to the "Master buttons", maintaining the headings in the TOC.
Copy link to clipboard
Copied
So what exactly should happen - what should link to what?
Because once you'll have all objects - they can link together... BUT it would be probably a good choice, to mark / tag them first - then, it will be much easier to find correct "targets".
I can help you automate it - but I would need to have "full picture" first...
Copy link to clipboard
Copied
The live blue buttons in the document shoud be "linked" to the ones in the Master Pages. Once the Cross-References are updated (in case of edits), the text inside of the buttons should always reflect/match the master ones.
Copy link to clipboard
Copied
The live blue buttons in the document shoud be "linked" to the ones in the Master Pages. Once the Cross-References are updated (in case of edits), the text inside of the buttons should always reflect/match the master ones.
By @Rogerio5C09
You mean contents should be the same?
Copy link to clipboard
Copied
that's correct! 🙂
Copy link to clipboard
Copied
that's correct! 🙂
By @Rogerio5C09
So you don't want to "link" - so click will transfer/go somewhere - but to reflect contents?
Copy link to clipboard
Copied
Or maybe you could use Content Collector tool?
https://helpx.adobe.com/uk/indesign/using/linked-content.html