TypeError: is undefined? Works fine on another document

Engaged ,
May 03, 2019 May 03, 2019

Copy link to clipboard

Copied

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!

TOPICS
Acrobat SDK and JavaScript, Windows

Views

282

Likes

Translate

Translate

Report

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
Most Valuable Participant ,
May 03, 2019 May 03, 2019

Copy link to clipboard

Copied

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

Likes

Translate

Translate

Report

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
Engaged ,
May 03, 2019 May 03, 2019

Copy link to clipboard

Copied

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.

Likes

Translate

Translate

Report

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
Most Valuable Participant ,
May 03, 2019 May 03, 2019

Copy link to clipboard

Copied

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.

Likes

Translate

Translate

Report

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
Most Valuable Participant ,
May 03, 2019 May 03, 2019

Copy link to clipboard

Copied

PS. That method with the loop is very confusing. You should replace all of these lines:

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 { 

     // rest of code

    }

}

With this:

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

     // rest of code

}

Likes

Translate

Translate

Report

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
Engaged ,
May 03, 2019 May 03, 2019

Copy link to clipboard

Copied

I will work at it... here is a sample file. All the page content is stripped out but the layers and bookmarks as well as page structure are all there.

https://drive.google.com/open?id=1FTolMmAbynL6l0bjWSZOzPBruar7QJax

Likes

Translate

Translate

Report

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
Most Valuable Participant ,
May 03, 2019 May 03, 2019

Copy link to clipboard

Copied

LATEST

Thanks, that's better.

Add these two lines before the line that causes the error and it will help you understand what's going on:

console.println(hypertextArray.toSource())

console.println(isolatedPages)

Likes

Translate

Translate

Report

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
Engaged ,
May 03, 2019 May 03, 2019

Copy link to clipboard

Copied

I also tried removing that outer loop and I still get the same message.

Here is what the document structure is like. 6 pages in the document. In this particular file the Hypertext layer exists on page 5.

Here is the layers window:

Here is the bookmarks window:

Likes

Translate

Translate

Report

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
Most Valuable Participant ,
May 03, 2019 May 03, 2019

Copy link to clipboard

Copied

Yeah, that was not meant to solve your issue, just to improve your code.

Again, without the actual file it's very difficult to help you debug it.

Likes

Translate

Translate

Report

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