Hi @Magnetik85, using your test document, I've written a script to do what you need, I think. However, you need to apply an Object Style to all the "menu" text frames so the script can find the menus (assigning an object style to these kinds of elements isn't a bad practice anyway, for other reasons). When you run the script, it will find all menu text frames, iterate over the table cells therein, and remove any existing hyperlinks and add the new hyperlinks. It was a fairly quick script so might not do enough error checking but, for me, it works fine on your test file.
- Mark
/**
* Create hyperlinks to document pages
* by searching for "menus" table cells
* in textFrames with applied object style.
* @author m1b
* @version 2023-02-06
* @discussion https://community.adobe.com/t5/indesign-discussions/automate-menu-creation-in-several-sections/m-p/13551412
*/
var settings = {
menuObjectStyleName: 'Menus',
pageNameTemplate: '#SECTION_PREFIX##PAGE_NAME#',
hyperlinkNameTemplate: 'From #SECTION_PREFIX##PAGE_NAME# to #SECTION_PREFIX##PAGE_NAME#',
matchOldHyperlinks: /^From .*/,
};
function main() {
var doc = app.activeDocument,
menusObjectStyle = doc.objectStyles.itemByName(settings.menuObjectStyleName),
counter = 0,
menuTextFrames = [];
if (menusObjectStyle.isValid) {
// find the menus text frames
app.findObjectPreferences = NothingEnum.NOTHING;
app.findObjectPreferences.appliedObjectStyles = settings.menuObjectStyleName;
menuTextFrames = doc.findObject();
}
if (menuTextFrames.length == 0) {
alert('No menus found in document.');
return;
}
// remove any hyperlinks created previous by this script
for (var i = doc.hyperlinks.length - 1; i >= 0; i--)
if (settings.matchOldHyperlinks.test(doc.hyperlinks[i].name))
doc.hyperlinks[i].remove();
menuTextFramesLoop:
for (var i = 0; i < menuTextFrames.length; i++) {
var menuTableCells = menuTextFrames[i].tables[0].cells,
sectionPrefix = menuTextFrames[i].parentPage.appliedSection.sectionPrefix,
pageNameTemplate = settings.pageNameTemplate.replace('#SECTION_PREFIX#', sectionPrefix),
hyperlinkNameTemplate = settings.hyperlinkNameTemplate
.replace('#SECTION_PREFIX#', sectionPrefix)
.replace('#SECTION_PREFIX#', sectionPrefix),
sourcePageName = menuTextFrames[i].parentPage.name;
cellsLoop:
for (var j = 0; j < menuTableCells.length; j++) {
var cell = menuTableCells[j],
pageName = String(j + 1),
destinationPageName = pageNameTemplate.replace('#PAGE_NAME#', pageName),
hyperlinkName = hyperlinkNameTemplate
.replace('#PAGE_NAME#', sourcePageName)
.replace('#PAGE_NAME#', pageName);
// get the page
var destinationPage = doc.pages.itemByName(destinationPageName);
if (!destinationPage.isValid) {
alert('There is no page named "' + destinationPageName + '". Script will abort.');
return;
}
// remove hyperlink if already exists
if (doc.hyperlinks.itemByName(hyperlinkName).isValid)
doc.hyperlinks.itemByName(hyperlinkName).remove();
// remove hyperlinkSources from cell
var existingHyperlinkSources = cell.texts[0].findHyperlinks();
for (var k = existingHyperlinkSources.length - 1; k >= 0; k--)
existingHyperlinkSources[k].remove();
// set up new hyperlink
var hyperlinkTextSource = doc.hyperlinkTextSources.add(cell.texts[0]),
hyperlinkDestination = makeHyperlinkPageDestination(doc, destinationPage, hyperlinkName),
hyperlink = makeHyperlink(hyperlinkTextSource, hyperlinkDestination, hyperlinkName);
counter++;
}
}
alert('Added ' + counter + ' hyperlinks.');
// finished
/**
* Returns a hyperlink by name or makes a new one.
* @param {Text} sourceText - an Indesign text object.
* @param {HyperlinkDestination} destination - an Indesign hyperlink destination object.
* @param {String} [name] - the name for the hyperlink.
* @returns {Hyperlink}
*/
function makeHyperlink(sourceText, destination, name) {
if (doc.hyperlinks.itemByName(name).isValid)
return doc.hyperlinks.itemByName(name);
else
return doc.hyperlinks.add(sourceText, destination, { name: name });
};
/**
* Returns a hyperlinkPageDestination
* by name or makes a new one.
* @param {Document} doc - an Indesign Document.
* @param {Page} page - the destination page.
* @param {String} [name] - the name for the hyperlinkPageDestination.
* @returns {HyperlinkPageDestination}
*/
function makeHyperlinkPageDestination(doc, page, name) {
if (doc.hyperlinkPageDestinations.itemByName(name).isValid)
return doc.hyperlinkPageDestinations.itemByName(name);
else
return doc.hyperlinkPageDestinations.add(page, { name: name, hidden: false });
};
}; // end main
app.doScript(main, ScriptLanguage.JAVASCRIPT, undefined, UndoModes.ENTIRE_SCRIPT, 'Add Menu Hyperlinks');
Edit: fixed bugs in code.