Hi @wahid28246070iu4o, here's a quick script that does what you want, I think. It works on your sample file. To use it, select one or more table cells and run script. You don't need to tell it what month, because I noticed you have set the script labels for the text frames on first page to "calendarDatesN" so the script gets the month from that. Now you just need to set up the pages so that the 1st of each month is a section start with the section prefix "FEB-" and "MAR-" etc. See attached sample doc. If you want to use a different scheme, just alter the script to match. I've put all the relevent settings in a "settings" object at the start. Let me know how it goes.
- Mark
/**
* Create hyperlinks to document pages, based on selected table cell contents.
* @author m1b
* @version 2023-02-04
* @discussion https://community.adobe.com/t5/indesign-discussions/hyperlinking-script/m-p/13552525
*/
var settings = {
pageNameTemplate: '#MONTHCODE#-#DAYNUMBER#',
hyperlinkNameTemplate: 'Goto-#MONTHCODE#-#DAYNUMBER#',
textFrameLabelMatcher: /calendarDates(\d+)/,
monthCodes: ['JAN', 'FEB', 'MAR', 'APR', 'MAY', 'JUN', 'JUL', 'AUG', 'SEP', 'OCT', 'NOV', 'DEC'],
};
function main() {
var doc = app.activeDocument,
counter = 0
if (
doc.selection.length == 0
|| !doc.selection[0].hasOwnProperty('cells')
|| doc.selection[0].cells == undefined
|| doc.selection[0].cells.length == 0
) {
alert('Please select one or more table cells and try again.');
return;
}
var cells = doc.selection[0].cells,
table = cells[0].parent;
// make sure we have a table object
while (
table.constructor.name != 'Table'
&& table.constructor.name != 'Document'
)
table = table.parent;
if (
table.constructor.name != 'Table'
|| !table.isValid
)
throw Error('Could not get valid table.');
cellsLoop:
for (var i = 0; i < cells.length; i++) {
var cell = cells[i];
if (!cell.isValid)
continue;
// get the day number
var dayNumber = Number(cell.contents);
if (
isNaN(dayNumber)
|| dayNumber == 0
)
continue;
// get the month code from the text frame label
var labelParts = table.parent.label.match(settings.textFrameLabelMatcher);
if (labelParts.length < 2)
// no match
continue;
var monthNumber = Number(labelParts[1]);
if (
monthNumber < 1
&& monthNumber > 12
)
// not a month number
continue;
var monthCode = settings.monthCodes[monthNumber - 1];
var destinationPageName = settings.pageNameTemplate
.replace('#MONTHCODE#', monthCode)
.replace('#DAYNUMBER#', dayNumber);
var hyperlinkName = settings.hyperlinkNameTemplate
.replace('#MONTHCODE#', monthCode)
.replace('#DAYNUMBER#', dayNumber);
// 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 j = existingHyperlinkSources.length - 1; j >= 0; j--)
existingHyperlinkSources[j].remove();
// set up new hyperlink
var hyperlinkSource = doc.hyperlinkTextSources.add(cell.texts[0]),
hyperlinkDestination = doc.hyperlinkPageDestinations.add(destinationPage, { hidden: false }),
hyperlink = doc.hyperlinks.add(hyperlinkSource, hyperlinkDestination);
hyperlink.name = hyperlinkName;
counter++;
}
alert('Added ' + counter + ' hyperlinks.');
}; // end main
app.doScript(main, ScriptLanguage.JAVASCRIPT, undefined, UndoModes.ENTIRE_SCRIPT, 'Add Calendar Hyperlinks');