Copy link to clipboard
Copied
I'm looking to create a digital planner using Adobe InDesign for 2024. I need assistance with scripting in JavaScript for Adobe InDesign 2024 to automate the process of adding hyperlinks to a calendar. I've uploaded my InDesign file. Could someone please help me with this?
Copy link to clipboard
Copied
Currently I am using this script. It works fine but if the daily page is one. But I have daily page 4 in one date it is not working
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');
Copy link to clipboard
Copied
Currently creating this new script is not working properly
var settings = {
textFrameLabel1: 'Month', // Month na label mate
textFrameLabel2: 'DateDay', // Date na label mate
sourceTextFrameName: 'SourceTextFrame' // Update with the actual name of your source text frame
};
function main() {
var doc = app.activeDocument,
counter = 0;
// Check if there is a selection and if the selection contains cells
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;
cellsLoop: for (var i = 0; i < cells.length; i++) {
var cell = cells[i];
var monthCell = cells[i + 1]; // Assuming monthName is in the next cell
var dayNumber = getDayNumber(cell);
var monthName = getMonthName(monthCell);
if (isNaN(dayNumber) || dayNumber == 0 || monthName == '') continue;
var destinationPage = findPageByLabels(doc, monthName, dayNumber);
if (!destinationPage) {
alert('There is no page with labels "' + monthName + '" and "' + dayNumber + '". Script will skip this cell.');
continue;
}
var sourceTextFrame = doc.textFrames.itemByName(settings.sourceTextFrameName);
if (!sourceTextFrame.isValid) {
alert('Source text frame "' + settings.sourceTextFrameName + '" not found. Script will skip this cell.');
continue;
}
var hyperlinkTextSource = doc.hyperlinkTextSources.add(sourceTextFrame.texts[0]);
var hyperlinkDestination = doc.hyperlinkPageDestinations.add(destinationPage);
var hyperlink = doc.hyperlinks.add(hyperlinkTextSource, hyperlinkDestination);
counter++;
}
alert(counter + ' hyperlinks created.');
}
function findPageByLabels(doc, monthName, dayNumber) {
var pages = doc.pages;
for (var i = 0; i < pages.length; i++) {
var page = pages[i];
var textFrames = page.textFrames;
var monthFound = false;
var dayFound = false;
for (var j = 0; j < textFrames.length; j++) {
var textFrame = textFrames[j];
if (textFrame.label === settings.textFrameLabel1 && textFrame.contents === monthName) {
monthFound = true;
}
if (textFrame.label === settings.textFrameLabel2 && Number(textFrame.contents) === dayNumber) {
dayFound = true;
}
if (monthFound && dayFound) {
return page;
}
}
}
return null;
}
function getDayNumber(cell) {
var contents = cell.contents;
if (typeof contents === 'number' || /^\d+$/.test(contents)) {
return parseInt(contents);
}
return 0;
}
function getMonthName(textFrame) {
if (textFrame.contents instanceof Array && textFrame.contents.length > 0) {
return textFrame.contents[0].contents; // Assuming month name is in the first item of the array
}
return '';
}
app.doScript(main, ScriptLanguage.JAVASCRIPT, undefined, UndoModes.ENTIRE_SCRIPT, 'Add Calendar Hyperlinks');
Copy link to clipboard
Copied
Hi @Mac29594311qpi5, I looked at your document, but I don't know exactly what you want to do. Please make it very clear. Show arrows and boxes or whatever you need to to make it obvious for me. A good way would be to make a new layer showing notes for me in red color or something. Or you could create a second .indd that performs how you want it to so I can compare "before" and "after" documents.
- Mark
P.S. reminder: if you use someone's code, especially if you re-post it, don't forget to include the author's details, even if it was ChatGPT.