Copy link to clipboard
Copied
Hi! I'm trying to write a script for InDesign but there is an error on line 41 (right at the end): it says 'undefined' is not an object. Can someone helpe me fix it? Thank you!
// Adjust paragraph style to even/odd pages script
// Written for Adobe InDesign
// Function to check if a page is even or odd
function isEvenPage(page) {
return page.side == PageSideOptions.LEFT_HAND;
}
// Function to change paragraph style based on even/odd page
function adjustParagraphStyles(doc) {
var pages = doc.pages;
for (var i = 0; i < pages.length; i++) {
var page = pages[i];
var pageTextFrames = page.textFrames;
for (var j = 0; j < pageTextFrames.length; j++) {
var textFrame = pageTextFrames[j];
var paragraphs = textFrame.paragraphs;
for (var k = 0; k < paragraphs.length; k++) {
var paragraph = paragraphs[k];
var styleName = paragraph.appliedParagraphStyle.name;
if (isEvenPage(page)) { // Even page
if (styleName == "N. Artigo - p.impar") {
paragraph.appliedParagraphStyle = doc.paragraphStyles.itemByName("N. Artigo - p.par");
}
} else { // Odd page
if (styleName == "N. Artigo - p.par") {
paragraph.appliedParagraphStyle = doc.paragraphStyles.itemByName("N. Artigo - p.impar");
}
}
}
}
}
}
// Main script execution
var doc = app.activeDocument;
app.doScript(adjustParagraphStyles, undefined, undefined, UndoModes.FAST_ENTIRE_SCRIPT, "Adjust paragraph style to even/odd pages");
Copy link to clipboard
Copied
Hi @Ana2211691434k7 , You are setting the doc variable outside of the functions, but then including it as a parameter with the adjustParagraphStyles function. Try removing the doc parameter:
function adjustParagraphStyles() {
Copy link to clipboard
Copied
Thank you, but that didn't seem to work. I'm out of my league here, I'll go back to doing things manually for now 🙂 But thanks a lot!
Copy link to clipboard
Copied
If the text is not flowing between pages, and there’s no overset text, it could be a simpler script. Try this:
function adjustParagraphStyles() {
var doc = app.activeDocument;
var ps = doc.paragraphStyles.itemByName("N. Artigo - p.impar")
if (!ps.isValid) {
alert("Missing Style")
return
}
var pages = doc.pages;
var para;
for (var i = 0; i < pages.length; i++) {
//page is even
if(i%2==1){
para = doc.pages[i].textFrames.everyItem().paragraphs.everyItem().getElements();
for (var j = 0; j < para.length; j++){
para[j].appliedParagraphStyle = ps;
};
}
}
}
// Main script execution
app.doScript(adjustParagraphStyles, undefined, undefined, UndoModes.FAST_ENTIRE_SCRIPT, "Adjust paragraph style to even/odd pages");
Before
After
Copy link to clipboard
Copied
Copy link to clipboard
Copied
The error makes it seem like you did not get all of the code, Should be
para = doc.pages[i].textFrames.everyItem().paragraphs.everyItem().getElements();
and not this which is triggering the error
para = doc.pages[i].textFrames.everyItem().paragraphs.everyItem
Copy link to clipboard
Copied
Also if you want to get a style out of a group try this:
function adjustParagraphStyles() {
var doc = app.activeDocument;
//var ps = doc.paragraphStyles.itemByName("N. Artigo - p.impar");
var ps = doc.paragraphStyleGroups.itemByName("Texto Códigos").paragraphStyles.itemByName("N. Artigo - p.impar");
if (!ps.isValid) {
alert("Missing Style")
return
}
var pages = doc.pages;
var para;
for (var i = 0; i < pages.length; i++) {
//page is even
if(i%2==1){
para = doc.pages[i].textFrames.everyItem().paragraphs.everyItem().getElements();
for (var j = 0; j < para.length; j++){
para[j].appliedParagraphStyle = ps;
};
}
}
}
// Main script execution
app.doScript(adjustParagraphStyles, undefined, undefined, UndoModes.FAST_ENTIRE_SCRIPT, "Adjust paragraph style to even/odd pages");
Copy link to clipboard
Copied
By the way @Ana2211691434k7, I will add a couple of notes about using "app.doScript":
1. it is absolutely the correct way to go, as it bunches up the entire script operation as a single undo/redo.
2. it is bad for troubleshooting errors because every error that is thrown in the called function (here, "main") will be reported as an error with the doScript line. To get around this, you can start the debugger (if using VSCode, add line "debugger;") and step through the code, or just comment out the app.doScript line and temporarily just call "main();", and un-comment when the error is fixed.
- Mark
Copy link to clipboard
Copied
Thank you for the insight!
Find more inspiration, events, and resources on the new Adobe Community
Explore Now