Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
9

app.doScript error on line 41

Community Beginner ,
Aug 17, 2023 Aug 17, 2023

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

 

TOPICS
Scripting
412
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Aug 17, 2023 Aug 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() {

 

 

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Sep 07, 2023 Sep 07, 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! 

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Sep 07, 2023 Sep 07, 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

 

Screen Shot 44.png

 

 

After

 

Screen Shot 45.png

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Sep 08, 2023 Sep 08, 2023

This is great, thank you. Much simpler. However, the first time I ran it I got the "Missing style" alert. I realized the styles were inside a folder (named "Texto Códigos") so I tried placing the styles outside of the folder and got this error. 

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Sep 08, 2023 Sep 08, 2023

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          

 

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Sep 08, 2023 Sep 08, 2023
LATEST

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");
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Aug 17, 2023 Aug 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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Sep 07, 2023 Sep 07, 2023

Thank you for the insight! 

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines