Skip to main content
subieguy2
Inspiring
May 3, 2019
Question

TypeError: is undefined? Works fine on another document

  • May 3, 2019
  • 1 reply
  • 1522 views

I am running this script to look at a documents layers, if it contains a layer "Hypertext" then store the page number and it's corresponding layers. Then filter out a few layers. Then what layer items are left get bookmarks created under headers and assigned a JavaScript.

This script works fine on several other documents. Now I am having an error message and not sure why.

Console output:

TypeError: hypertextArray[isolatedPages] is undefined

65:Document-Level:bookmarks

JavaScript in it's entirety:

var layerAction = [

    ["141 (Lt Orange)", "oneFourOne();"],

    ["500 (Med Pink)", "fiveZeroZero();"],

    ["522 (Lt Purple)", "fiveTwoTwo();"],

    ["577 (Lt Green)", "fiveSevenSeven();"],

    ["651 (Blue Gray)", "sixFiveOne();"],

    ["5655 (Lt Gray)", "fiveSixFiveFive();"],

    ["SUB 651 (Blue Gray) (Ethernet)", "subSixFiveOne();"]

];

var hypertextArray = [];

var isolatedPages = [];

for (var i = 0; i < 1; i++) {

    if (this.bookmarkRoot.children[0].name == "All Schematic Tools") {

        // Bookmarks have been created... Don't do it again

        break;

    } else {

        // Look for pages that contain a layer named Hypertext

        for (var p = 0; p < this.numPages; p++) {

            var ocgArray = this.getOCGs(p);

            // Only check the page if it contains layers... Skip flattened pages

            if (ocgArray != null) {

                for (var i = ocgArray.length - 1; i >= 0; i--) {

                    if (ocgArray.name == "Hypertext") {

                        var pageNumberPass = parseInt(p);

                        isolatedPages.push(pageNumberPass);

                        //console.println(pageNumberPass);

                    }

                }

            }

        }

        for (j = 0; j < isolatedPages.length; j++) {

            //console.println(isolatedPages);

            hypertextArray.push([isolatedPages], []);

            var isolatedOCG = this.getOCGs(isolatedPages);

            for (z = 0; z < isolatedOCG.length; z++) {

                if (isolatedOCG.name == "Color Legend" ||

                    isolatedOCG.name == "Media Info" ||

                    isolatedOCG.name == "Opacity" ||

                    isolatedOCG.name == "No Color" ||

                    isolatedOCG.name == "Text" ||

                    isolatedOCG.name == "CONNs" ||

                    isolatedOCG.name == "Conns" ||

                    isolatedOCG.name == "Splice Circles" ||

                    isolatedOCG.name == "Splice Lines" ||

                    isolatedOCG.name == "Symbols/Abbreviation Tables" ||

                    isolatedOCG.name == "Title Block" ||

                    isolatedOCG.name == "Rails/Feather Bars" ||

                    isolatedOCG.name == "651 (Highways)" ||

                    isolatedOCG.name == "Controller Fill" ||

                    isolatedOCG.name == "Section Borders" ||

                    isolatedOCG.name == "Border and Crop Marks" ||

                    isolatedOCG.name == "Guides" ||

                    isolatedOCG.name == "Hypertext") {

                } else {

                    hypertextArray[isolatedPages].push([isolatedOCG.name]);

                }

            }

        }

        ////////////////  CREATING BOOKMARKS  //////////////////

        var counter = isolatedPages.length;

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

        for (var i = hypertextArray.length - 1; i >= 0; i--) {

            var layerNamesArray = hypertextArray;

            if (hypertextArray.length > 1) {

                this.bookmarkRoot.createChild("Schematic Tools - Volume " + counter);

                // Create the bookmark at the top most position

                var headerBookmark = this.bookmarkRoot.children[0];

                // Make the bookmark text Bold and Italic

                headerBookmark.style = 3;

                counter--;

                for (var x = 0; x < layerNamesArray.length; x++) {

                    if (layerNamesArray.length > 1) {

                        // Create a bookmark under the volume bookmark

                        headerBookmark.createChild({

                            // The name of the bookmark (matches the layer name)

                            cName: layerNamesArray,

                            // JavaScript function to apply to the bookmark

                            cExpr: "oneFourOne();",

                        });

                        //console.println("layerNamesArray[" + i + "][" + x + "] = " + layerNamesArray);

                    }

                }

            }

        }

        this.bookmarkRoot.createChild("All Schematic Tools");

        // Create the bookmark at the top most position

        var headerBookmark = this.bookmarkRoot.children[0];

        // Make the bookmark text Bold and Italic

        headerBookmark.style = 3;

        ///////////////////////////////////////////////////  ALL ON BOOKMARK  ////////////////////////

        headerBookmark.createChild({

            // The name of the bookmark

            cName: "All On",

            // JavaScript function to apply to the bookmark

            cExpr: "allOn();",

        });

        ///////////////////////////////////////////////////  ALL OFF BOOKMARK  ////////////////////////

        // Create a bookmark under the volume bookmark

        headerBookmark.createChild({

            // The name of the bookmark

            cName: "All Off",

            // JavaScript function to apply to the bookmark

            cExpr: "allOff();",

        });

        ///////////////////////////////////////////////////  GRID LOCATION BOOKMARK  //////////////////

        // Create a bookmark under the volume bookmark

        headerBookmark.createChild({

            // The name of the bookmark (matches the layer name)

            cName: "Grids On/Off",

            // JavaScript function to apply to the bookmark

            cExpr: "gridLocation();",

        });

    }

}

////////////////  SEARCH CREATED BOOKMARKS - ASSIGN JAVASCRIPT FUNCTION  //////////////////

// Loop for top level bookmarks

for (var i = 0; i < this.bookmarkRoot.children.length; i++) {

    //console.println(this.bookmarkRoot.children.name);

    var bk = this.bookmarkRoot.children;

    if (bk.children != null) {

        // Loop for nested bookmarks

        for (var j = 0; j < bk.children.length; j++) {

            //console.println(bk.children.name);

            for (var z = 0; z < layerAction.length; z++) {

                var layerFunction = layerAction;

                for (var zz = 0; zz < layerFunction.length; zz++) {

                    if (bk.children.name == layerFunction[zz]) {

                        bk.children.setAction(layerFunction[(zz + 1)]);

                    }

                    //console.println("layerFunction[" + z + "][" + zz + "] = " + layerFunction[zz]);

                }

            }

        }

    }

}

Sometimes the spacing is off in the inserted code for the forum.... so just to clarify this is the line that the console is telling me has a TypeError and is undefined:

hypertextArray[isolatedPages].push([isolatedOCG.name]);

Any help or insight would be appreciated! Thanks in advance!

This topic has been closed for replies.

1 reply

try67
Community Expert
Community Expert
May 3, 2019

What's the point of the outer-most for loop? And why are you re-using the "i" variable inside this loop?

subieguy2
subieguy2Author
Inspiring
May 3, 2019

I am using that outer loop as way to ghetto way of checking to see if the bookmarks have been created already (just checking for the top most bookmark which if the script had run previously then that would be "All Schematic Tools").

As far as reusing the i inside of that loop....that is because I have been working on this in pieces and combined it.... just didn't catch that I was reusing it within. I renamed the variables so that none of them match in my loops and I still get the same error.

try67
Community Expert
Community Expert
May 3, 2019

You need to add debugging code, such as printing out the values of the various variables you're using to the console and try and see when (and how) it goes wrong. It's very difficult to help you further without having a sample file to run it on.