Okay, now that I have looked at the OP's demo file I have realised that the problem was that the hyperlinks were all faulty—none had destinations at all, so they couldn't be adjusted. I cleaned up the file and re-wrote the script so that now it generates suitable hyperlinks based on a URL template string, and it grabs details from the page, including day number, month number and hour of day (using find paragraph styles), and creates a hyperlink based on these details. (It gets the month number from the applied master page name.)
Well, for the sake of people learning scripting, here is the final script below. - Mark
/**
* Create hyperlinks for calendar elements.
* Will remove existing hyperlinks previously
* created by this script.
* Expects the document to have paragraph styles
* for daily time and daily day number, as well
* as master spreads named after the months.
* @7111211 m1b
* @version 2022-11-28
* @discussion https://community.adobe.com/t5/indesign-discussions/how-to-find-replace-a-part-of-url-in-indesign/m-p/13374674
*/
var settings = {
// year
year: 2023,
// the URL string
urlTemplate: 'https://calendar.google.com/calendar/u/0/r/eventedit?action=TEMPLATE&dates=#yyyy#mm#ddT#hh0000/#yyyy#mm#ddT#ii0000',
// paragraph style names
dailyTimeStyleName: 'Daily time',
dailyDayNumberStyleName: 'Daily day number',
};
function main() {
var doc = app.activeDocument,
hyperlinks = doc.hyperlinks,
matchOldHyperlinks = /T\d\d$/;
// first remove any hyperlinks
// create previous by this script
for (var i = hyperlinks.length - 1; i >= 0; i--)
if (matchOldHyperlinks.test(hyperlinks[i].name))
hyperlinks[i].remove();
// reset grep prefs
app.findGrepPreferences = NothingEnum.NOTHING;
app.changeGrepPreferences = NothingEnum.NOTHING;
// find criterion
app.findGrepPreferences.findWhat = '^\\d{2}$';
app.findGrepPreferences.appliedParagraphStyle = settings.dailyDayNumberStyleName;
// find the dayNumbers
var dayNumbers = doc.findGrep();
if (dayNumbers.length == 0) {
alert('Could not find any day numbers in ' + doc.name + '.');
return;
}
// store all the day numbers in array indexed by page's documentOffset
var days = [],
masterNames = ['B-JANUARY', 'C-FEBRUARY', 'D-MARCH', 'E-APRIL', 'F-MAY', 'G-JUNE', 'H-JULY', 'I-AUGUST', 'J-SEPTEMBER', 'K-OCTOBER', 'L-NOVEMBER', 'M-DECEMBER'];
for (var i = 0; i < dayNumbers.length; i++) {
var page = textPage(dayNumbers[i]);
days[page.documentOffset] = {
dayNumber: dayNumbers[i].contents,
monthNumber: indexOf(page.appliedMaster.name, masterNames) + 1
}
}
// find the daily times
app.findGrepPreferences.appliedParagraphStyle = settings.dailyTimeStyleName;
var dailyTimes = doc.findGrep(),
counter = 0;
if (dailyTimes.length == 0) {
alert('Could not any daily times in ' + doc.name + '.');
return;
}
// find placeholders in the url template
var findYear = /#yyyy/g,
findMonth = /#mm/g,
findDay = /#dd/g,
findHourStart = /#hh/g,
findHourEnd = /#ii/g;
for (var i = 0; i < dailyTimes.length; i++) {
var documentOffset = textPage(dailyTimes[i]).documentOffset,
// get the url elements
monthNumber = ('0' + days[documentOffset].monthNumber).slice(-2),
dayNumber = days[documentOffset].dayNumber,
hourStart = dailyTimes[i].contents,
hourEnd = ('0' + (Number(hourStart) + 1)).slice(-2),
// make the url based on the template
url = settings.urlTemplate
.replace(findYear, String(settings.year))
.replace(findMonth, monthNumber)
.replace(findDay, dayNumber)
.replace(findHourStart, hourStart)
.replace(findHourEnd, hourEnd),
name = [settings.year, monthNumber, dayNumber].join('-') + 'T' + hourStart;
// remove hyperlink if already exists
if (doc.hyperlinks.itemByName(name).isValid)
doc.hyperlinks.itemByName(name).remove();
var existingHyperlinkSources = dailyTimes[i].findHyperlinks();
for (var j = existingHyperlinkSources.length - 1; j >= 0; j--)
existingHyperlinkSources[j].remove();
// set up hyperlink
var hyperlinkSource = doc.hyperlinkTextSources.add(dailyTimes[i]),
hyperlinkDestination = doc.hyperlinkURLDestinations.add(url, { hidden: false }),
hyperlink = doc.hyperlinks.add(hyperlinkSource, hyperlinkDestination);
hyperlink.name = name;
counter++;
}
alert('Added ' + counter + ' hyperlinks.');
return;
/**
* Returns the document offset
* of the text's page.
* @9397041 {text} text - an Indesign text object.
* @Returns {Number}
*/
function textPage(text) {
if (text.parentTextFrames[0].parentPage)
return text.parentTextFrames[0].parentPage;
};
/**
* Returns index of obj in arr.
* Returns -1 if not found.
* @9397041 {any} obj
* @9397041 {Array} arr
* @Returns {Number}
*/
function indexOf(obj, arr) {
for (var i = 0; i < arr.length; i++)
if (arr[i] == obj)
return i;
return -1;
};
}; // end main
app.doScript(main, ScriptLanguage.JAVASCRIPT, undefined, UndoModes.ENTIRE_SCRIPT, 'Add Calendar Hyperlinks');