Copy link to clipboard
Copied
Hi
I am a bit stuck and hoping someone can help me figure out where I am going wrong.
I have added my script below and the part that I keep getting an error on is line line 62 but I believe the actualy error is in line 60 where I am defining the variable mySelection for adding in the hyperlink.
The error I get says "Error Number: 45. Error String: Object is invalid"
I have tested the index variables for the start and the end of the selection and they are correct. I have another script for applying a hyperlink that works in a very similar but slightly different way – it applies the hyperlink to text I have highlighted – and the logic is the same here but I can't see where I am going wrong?
const doc = app.activeDocument;
createTOC();
function createTOC() {
//********************************************** TEST FOR STYLES **********************************************
testStyles()
//********************************************** SETUP THE DOCUMENT **********************************************
//Create a text frame on page 3 for the TOC to go into
var textFrame = doc.pages[3].textFrames.add();
textFrame.properties = {
geometricBounds: [-171.601, 244.375, 33.155, 361.25],
strokeWidth: 0,
fillColor: "None",
contents: "",
itemLayer: "Text",
};
//Apply the paragraph style to the TOC frame
textFrame.texts[0].appliedParagraphStyle = doc.paragraphStyleGroups.itemByName("Inside Front Pages").paragraphStyles.itemByName("TOC_Contents");
//****************************************** Capture the Recipe Name and Cirlcle Letter Pairs *********************************************
//this functions returns a multidimensional array – each entry has the recipe name followed by the circle letters
var recipeData = getRecipeData();
//********************************* CREATE THE TOC *************************************
var arrayLength = recipeData.length;
for (var j = 0; j < 5; j++) {
var tocRecipeName = recipeData[j][0];
var tocCircleLetters = recipeData[j][1];
//**************************** ADD IN CROSS REFERENCE **********************************
//put in the destination
var h = doc.hyperlinkTextDestinations.itemByName(tocRecipeName);
//Where is the location for the cross reference to be inserted
var t = doc.pages[3].textFrames[1].insertionPoints[-1];
//What format should the cross reference use
var xRefForm = doc.crossReferenceFormats.item("Page Number");
//Add the values for the destination and type of cross refernce format
var s = doc.crossReferenceSources.add(t, xRefForm);
//create the cross reference
var xref = doc.hyperlinks.add(s, h);
//********************************* INSERT THE RECIPE NAME *************************************
//select the text frame on the contents page
var myTextFrame = doc.pages[3].textFrames[1];
//Define the text to be inserted
var myNewText = "\t" + tocRecipeName;
//Put the recipe name into the text frame
myTextFrame.parentStory.insertionPoints.item(-1).contents = myNewText;
//********************************* ADD HYPERLINK TO THE RECIPE NAME *************************************
//get the idex position for the start and end of the Recipe Name
var indexEnd = myTextFrame.characters.length;
var recipeLength = tocRecipeName.length;
var indexStart = indexEnd - recipeLength;
//**************************** ADD IN THE HYPERLINK **********************************
//select the text by their index numbers previously defined.
var mySelection = myTextFrame.characters.itemByRange(indexStart, indexEnd);
//apply the character style to it.
mySelection.appliedCharacterStyle = "Hyperlink";
//The below defines the source of the hyperlink to be the highlighted text "mySelection"
var source = app.documents[0].hyperlinkTextSources.add(mySelection);
//Destination"s" plural is used because we are selecting (via its name) a single hyperlink destination from the ARRAY of possible destinations.
var dest = app.documents[0].hyperlinkTextDestinations.itemByName(tocRecipeName);
//this applies the hyperlink referencing the source and the destination
app.documents[0].hyperlinks.add(source, dest, {
name: "TOC_" + tocRecipeName
});
}
}
1 Correct answer
I would start by checking that recipe length > 0
But more $.writelns/alerts will help you narrow down where things are falling apart. You can also check:
if (!mySelection.isValid) {
alert('toc: ' + tocRecipeName + ": " + indexStart + ": " + indexEnd;
}
Copy link to clipboard
Copied
Can you share a sample InDD file to test or probably create a smaller script to reproduce the error easily in a self created document.
-Manan
Copy link to clipboard
Copied
I would start by checking that recipe length > 0
But more $.writelns/alerts will help you narrow down where things are falling apart. You can also check:
if (!mySelection.isValid) {
alert('toc: ' + tocRecipeName + ": " + indexStart + ": " + indexEnd;
}
Copy link to clipboard
Copied
I had another look at it this morning and I have figured it out. I thought I had the right numbers for the index but I was one number bigger than it needed to be. After I fixed that it is all working now. Thanks for the suggestions @brian_p_dts

