Skip to main content
Participant
August 17, 2023
Question

app.doScript error on line 41

  • August 17, 2023
  • 8 replies
  • 417 views

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

 

This topic is closed to new replies. Start a new post to keep the conversation going.

8 replies

m1b
Community Expert
Community Expert
August 17, 2023

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

Participant
September 7, 2023

Thank you for the insight! 

rob day
Community Expert
Community Expert
August 17, 2023

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() {

 

 

Participant
September 7, 2023

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! 

rob day
Community Expert
Community Expert
September 7, 2023

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