Skip to main content
Known Participant
July 18, 2022
Answered

What is the error meaning?

  • July 18, 2022
  • 2 replies
  • 569 views

Dear All,

The below script provided by "vinny38", when i run the below script "Undefined is not an abject error" showing, i am new in the script platform. How to clear the run time error? please advice

 

if (app.documents.length > 0) {

var myDocument = app.documents[0];

//----------------------------Open Dialog box----------------------------//

var myStylesNames = [];

for (i = 0; i < myDocument.allParagraphStyles.length; i++) {

myStylesNames.push(myDocument.allParagraphStyles.name);

}

var myDialog = app.dialogs.add({

name: "Align to dots script",

canCancel: true

});

with(myDialog.dialogColumns.add()) {

staticTexts.add({

staticLabel: "Choose the paragraph style applied to the cells you want to align on dot"

});

}

with(myDialog.dialogColumns.add()) {

var myDDSTyles = dropdowns.add({

stringList: myStylesNames,

selectedIndex: 0

});

}

var myResult = myDialog.show();

if (myResult == true) {

var myParaStyle = myStylesNames[myDDSTyles.selectedIndex];

}

myDialog.destroy();

//---------------------------- add a tab before (if doesn't exist) ----------------------------//

app.findGrepPreferences = null;

app.changeGrepPreferences = null;

app.findGrepPreferences.properties = {

findWhat: "^(\\d)",

appliedParagraphStyle: myParaStyle,

}

app.changeGrepPreferences.changeTo = "\\t$1";

app.activeDocument.changeGrep();

app.findGrepPreferences = null;

app.changeGrepPreferences = null;

//---------------------------- create array ----------------------------//

var myText = [];

var allText = myDocument.stories.everyItem().tables.everyItem().cells.everyItem().paragraphs.everyItem().getElements();

for (i = 0; i < allText.length; i++) {

if (allText.appliedParagraphStyle.name == myParaStyle) {

myText.push(allText);

}

}

//---------------------------- Set tab alignment and apply to cells ----------------------------//

var tabStopProperties = {

alignment: TabStopAlignment.CHARACTER_ALIGN,

alignmentCharacter: "."

};

for (var i = 0; i < myText.length; i++) {

var cellWidth = myText.parent.width;

var cellLeftInset = myText.parent.leftInset;

var cellRightInset = myText.parent.rightInset;

var position = (cellWidth - cellRightInset - cellLeftInset - 1.058) / 2;

myText.tabStops.everyItem().remove();

myText.tabStops.add();

myText.tabStops[0].properties = tabStopProperties;

myText.tabStops[0].position = position;

}

} else {

alert("Please open a document");

}

 

Thanks

kanagakumar

This topic has been closed for replies.
Correct answer Peter Kahrel

They'll still have to select InDesign as the target application 🙂

2 replies

m1b
Community Expert
Community Expert
July 18, 2022

Hi @kanagakumar, I managed to get it up and running with a few tweaks. Probably was corrupted by text formatting. Also was missing a check for undefined myText[i]. - Mark

 

if (app.documents.length > 0) {
    var myDocument = app.documents[0];
    //----------------------------Open Dialog box----------------------------//
    var myStylesNames = [];
    for (i = 0; i < myDocument.allParagraphStyles.length; i++) {
        myStylesNames.push(myDocument.allParagraphStyles[i].name);
    }
    var myDialog = app.dialogs.add({
        name: "Align to dots script",
        canCancel: true
    });
    with (myDialog.dialogColumns.add()) {
        staticTexts.add({
            staticLabel: "Choose the paragraph style applied to the cells you want to align on dot"
        });
    }

    with (myDialog.dialogColumns.add()) {
        var myDDSTyles = dropdowns.add({
            stringList: myStylesNames,
            selectedIndex: 0
        });
    }
    var myResult = myDialog.show();
    if (myResult == true) {
        var myParaStyle = myStylesNames[myDDSTyles.selectedIndex];
    }
    myDialog.destroy();
    //---------------------------- add a tab before (if doesn't exist) ----------------------------//
    app.findGrepPreferences = null;
    app.changeGrepPreferences = null;
    app.findGrepPreferences.properties = {
        findWhat: "^(\\d)",
        appliedParagraphStyle: myParaStyle,
    }
    app.changeGrepPreferences.changeTo = "\\t$1";
    app.activeDocument.changeGrep();
    app.findGrepPreferences = null;
    app.changeGrepPreferences = null;
    //---------------------------- create array ----------------------------//
    var myText = [];
    var allText = myDocument.stories.everyItem().tables.everyItem().cells.everyItem().paragraphs.everyItem().getElements();
    for (i = 0; i < allText.length; i++) {
        if (allText[i].appliedParagraphStyle.name == myParaStyle) {
            myText.push(allText);
        }
    }
    //---------------------------- Set tab alignment and apply to cells ----------------------------//
    var tabStopProperties = {
        alignment: TabStopAlignment.CHARACTER_ALIGN,
        alignmentCharacter: "."
    };
    for (var i = 0; i < myText.length; i++) {

        if (myText[i].parent == undefined)continue;
        var cellWidth = myText[i].parent.width;
        var cellLeftInset = myText[i].parent.leftInset;
        var cellRightInset = myText[i].parent.rightInset;
        var position = (cellWidth - cellRightInset - cellLeftInset - 1.058) / 2;
        myText[i].tabStops.everyItem().remove();
        myText[i].tabStops.add();
        myText[i].tabStops[0].properties = tabStopProperties;
        myText[i].tabStops[0].position = position;
    }
} else {
    alert("Please open a document");
}
m1b
Community Expert
Community Expert
July 18, 2022

Also shout out to @vinny38 for the original script! 🙂

Known Participant
July 18, 2022

Please see the screen shot blow:

 

Peter Kahrel
Community Expert
Community Expert
July 18, 2022

In the top left of your screen you see a dropdown in which 'ExtendScript Toolkit CC' is selected. You should select your InDesign version in that dropdown.