Skip to main content
Participating Frequently
February 3, 2023
Answered

Hyperlinking script

 

Hi All,

 

I am working on creating a planner on indesign. I have a page with has a calendar of February 2023 like below, I would like to hyperlink the numbers in the corner to all the daily pages I have created for February, is there an easy way to do this please using a script? I have 12 months I would need to do this for. 

This topic is closed to new replies. Start a new post to keep the conversation going.
m1b
m1bCorrect answer
Community Expert
February 3, 2023

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');

 

 

27 replies

Known Participant
June 13, 2024

Daily pages script works very well. So I thank you very much.
Now my new planner has more than one page. It did not work well. In it the first page takes the first date and the second page takes the second one, but I see the link 1 of the first and the second page. If you want to do this, it can be done. Thank you very much for helping

Translate
Known Participant
June 17, 2024
Jan-2024.indd
Translate
m1b
Community Expert
Community Expert
February 3, 2023

Hi @wahid28246070iu4o, do you have any scripting experience? It is possible, but we'd need much more specific information about your document. Here's a workflow: you could select cells of a table and run a script. The script could ask you for the month prefix and you enter, for example, "FEB". Then the script makes new hyperlinks and destinations that go to Page named [month prefix]-[content of cell], so for the cell on day 1, the contents is "1" and it links to the page FEB-1.

Just an idea.

- Mark

Translate
Participating Frequently
February 3, 2023

This is the document I have currently

Translate
m1b
Community Expert
Community Expert
February 4, 2023

Edited as was using a Mac, works now!! Thank you soo much!!!

 


👍

Translate