app.response answer determines function change var

Community Beginner ,
Sep 18, 2018 Sep 18, 2018

Copy link to clipboard

Copied

I am not sure if this is doable or not, thinking about this the wrong way or i am just completely missing something. I normally like to try to figure out where i went wrong but this has gotten me stuck.

I am trying to take the standard trim bookmark name function and make is user changeable to select what part of the bookmark is being changed via a popup question. I set up folder level. When i try to run in Adobe Pro 9.0 i receive "An internal error occurred" message. In console i receive "redeclaration of const TrimBookmark2:Console:Exec". Any help would be appreciated

function TrimBookmark()

{

    var Title = new String(bkm.name);

    bkm.name = cResponse;

    // bkm.name = Title.substr(0, NumChars);

    // bkm.name = Title.substr(-NumChars,NumChars);

    // bkm.name = Title.slice(NumChars,Title.length);

    // bkm.name = Title.slice(0,-NumChars);

  

    if (bkm.children != null)

    {    

     for (var i = 0; i < bkm.children.length; i++)

     {    

          TrimBookmark(NumChars, bkm.children, nLevel + 1);

}}}  

function TrimQuestion()

{

var cResponse = app.response({

cQuestion: "Enter A to set bookmark to the FIRST number of characters" + "\n" + "\n" +

"Enter B to set bookmark to the LAST number of characters" + "\n" + "\n" +

"Enter C to trim the BEGINING of the bookmark" + "\n" + "\n" +

"Enter D to trim  the END of the bookmark",

cTitle: "Select Option",

cLabel: "Select Option:"

});

if (cResp == "A") {

event.value = Title.substr(0, NumChars);

}

else if (cResp == "B") {

event.value = Title.substr(-NumChars,NumChars);

}

else if (cResp == "C") {

event.value = Title.slice(NumChars,Title.length);

}

else if (cResp == "D") {

event.value = Title.slice(0,-NumChars);

}

var root = this.bookmarkRoot;

var NumCharsToTrimTo = 2;

TrimBookmark(NumCharsToTrimTo, root, 0);

}

app.addMenuItem({

     cName: "TrimBookmark",

     cUser: "Trim Bookmark",

     cParent: "View",

     cExec: "TrimQuestion();",

     cEnable: "event.rc = (event.target != null);"

});

TOPICS
Acrobat SDK and JavaScript

Views

196

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

correct answers 1 Correct Answer

Community Beginner , Sep 19, 2018 Sep 19, 2018
Thanks guys. It was the variable. I was trying to carry the variable from one function across to the other and I could not understand how not to do that. I ended up just writing 4 separate functions and made one function select  the correct function i needed.Thanks your help

Likes

Translate

Translate
Adobe Community Professional ,
Sep 18, 2018 Sep 18, 2018

Copy link to clipboard

Copied

You have two functions with the same name. That's not allowed.

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
Community Beginner ,
Sep 18, 2018 Sep 18, 2018

Copy link to clipboard

Copied

if it is a double function then why does this work. Wouldn't that be the same thing?

function TrimBookmarkTitle(NumChars, Bm, nLevel)

{

    var nLevelMax = 2;

    if (nLevel > nLevelMax)

    {

        return;

    }

    var Title = new String(Bm.name);

    Bm.name = Title.substr(0, NumChars);

    if (Bm.children != null)

    {    

     for (var i = 0; i < Bm.children.length; i++)

     {    

          TrimBookmarkTitle(NumChars, Bm.children, nLevel + 1);

}}}

function FinishTrim()

{

var cResponse = app.response({

cQuestion: "Enter the number of characters to trim",

cTitle: "Trim",

cLabel: "Trim:"

});

if (cResponse == null) {event.value = "";}

var root = this.bookmarkRoot;

var NumCharsToTrimTo = cResponse;

TrimBookmarkTitle(NumCharsToTrimTo, root, 0);

}

// add the menu item

app.addMenuItem({

     cName: "TrimBookmarkTitle",

     cUser: "Trim Bookmark",

     cParent: "View",

     cExec: "FinishTrim();",

     cEnable: "event.rc = (event.target != null);"

});

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
Adobe Community Professional ,
Sep 18, 2018 Sep 18, 2018

Copy link to clipboard

Copied

The code you have posted (in the first post) is missing some information.

First, there is no "TrimBookmark2" in your code.  Unless you removed a space in the error message and the "2" is the line number.  Complaining about the "TrimBookmark" function would indicate there is probably application level code (i.e. a JavaScript file) that already defines this function, For example, where did the code in you 2nd post come from? is that code in a JS file that Acrobat is loading?

To test for this, simply type "TrimBookmark" into the console window immediately after starting and press Ctrl-Enter to run it.  If it returns anything but "undefined" then it's already defined.

Next, None of the arguments used in the "TrimBookmark" function are listed in the function declaration. Bernd points this out in his post.

Thom Parker - Software Developer at PDFScripting
Use the Acrobat JavaScript Reference early and often

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
Adobe Community Professional ,
Sep 18, 2018 Sep 18, 2018

Copy link to clipboard

Copied

Sorry, I thought you called both functions the same in the original code. I see now that's not the case, they are just very similar.

There are plenty of other errors in it, as was pointed out, though.

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
Adobe Community Professional ,
Sep 18, 2018 Sep 18, 2018

Copy link to clipboard

Copied

At this line:

  event.value = Title.substr(0, NumChars);

What value will you set? What is Title? What is NumChars?

What is the standard trim bookmark name function?

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
Community Beginner ,
Sep 19, 2018 Sep 19, 2018

Copy link to clipboard

Copied

LATEST

Thanks guys. It was the variable. I was trying to carry the variable from one function across to the other and I could not understand how not to do that. I ended up just writing 4 separate functions and made one function select  the correct function i needed.

Thanks your help

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