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

Set Global Variable to "this" (currently open document)

Community Beginner ,
Oct 16, 2021 Oct 16, 2021

Copy link to clipboard

Copied

Hi All -

I'm imaginging this is pretty easy to do but for the life of me I can't figure it out. How can I set a global variable to be the currently open document ("this") so that I can easily reference it in various functions to modify it?

Thanks in advance.

TOPICS
JavaScript

Views

1.7K

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 Expert ,
Oct 16, 2021 Oct 16, 2021

Copy link to clipboard

Copied

E.g.:

current_doc = this;

 

Votes

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 Expert ,
Oct 17, 2021 Oct 17, 2021

Copy link to clipboard

Copied

By "global" do you mean accessible throughout the file, or in other files? For the latter you would need to use an actual global variable, something like this:

global.myDoc = this;

Votes

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 ,
Oct 17, 2021 Oct 17, 2021

Copy link to clipboard

Copied

Hi Try67,

 

Thanks for the reply. Although I code quite a bit I don't have formal training so I sometimes misuse terms. My bad.

 

What I mean is a variable that I can easily access across multiple functions (all part of same JavaScript file). I thought I'd still need a global variable to do that.

 

For instance I thought if I wanted to use: var r = 3.14 from function A in function B, I'd have to do global.r = 3.14.

 

I could have sworn I tried the global.mainDoc = this and it failed . . . Will try again.

Votes

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 Expert ,
Oct 17, 2021 Oct 17, 2021

Copy link to clipboard

Copied

If both functions are in the same file you don't need a global variable. You can use a local one, defined at the doc-level, via Tools - JavaScript - Document JavaScripts.

Votes

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 ,
Oct 17, 2021 Oct 17, 2021

Copy link to clipboard

Copied

What if I'm doing everything via a text editor ? Ultimately this will be deployed on a couple hundred computers in my office.

 

Basically trying to avoid anything within Acrobat menus and do it all separately to ensure it is easily deployable across systems.

 

Thanks again.

Votes

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
LEGEND ,
Oct 18, 2021 Oct 18, 2021

Copy link to clipboard

Copied

Bear in mind that "this" is not always the current document ! Check the API Reference, it's more complicated than you might think!

Votes

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 Expert ,
Oct 18, 2021 Oct 18, 2021

Copy link to clipboard

Copied

It doesn't matter what editor you use. If the script is embedded in the file then it will go with it whenever you send it. However, keep in mind that many PDF viewers will not process such scripts correctly, so it will probably not work in them... Use Adobe Acrobat (Reader) for best results, but not on a mobile device.

Votes

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 ,
Oct 19, 2021 Oct 19, 2021

Copy link to clipboard

Copied

So - I'm not convinced that the variables just carry through to different functions if in same file. Here's my code:

function SetTitleForOriginalDoc()
{
     this.info.Title = "Original Doc"; // tag original PDF as "Original Doc" - silently, not visible
}

function CountBookmarks(bkm, nLevel)
{
    count = 0;
    if (bkm.children != null)
    {
        count = bkm.children.length;
        for (var i = 0; i < bkm.children.length; i++)
        {
             count += CountBookmarks(bkm.children[i], nLevel + 1);
        }
    }
    return count;
}

//Main Function
function DoItNow() 
{
     SetTitleForOriginalDoc()
     CountBookmarks(this.bookmarkRoot, 0)
          console.clear(); console.show();
          console.println("Number of bookmarks found: " + count);
}

// add the menu item
app.addMenuItem(
{
     cName: "DumpBookmark",
     cUser: "Bookmarks ToC",
     cParent: "Edit",
     cExec: "DoItNow();",
     cEnable: "event.rc = (event.target != null);"
});

 

I'm getting an error about undefined variable "count" despite it being clearly defined (and taken entirely from) the JavaScript API Reference for counting # of bookmarks in a file..... Thoughts?

Votes

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 Expert ,
Oct 19, 2021 Oct 19, 2021

Copy link to clipboard

Copied

You have not defined count in your main function.

 

Use this:
count = CountBookmarks(this.bookmarkRoot, 0);

 

Votes

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 Expert ,
Oct 19, 2021 Oct 19, 2021

Copy link to clipboard

Copied

Add this as the very first line of your code:

var count;

Votes

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 ,
Oct 20, 2021 Oct 20, 2021

Copy link to clipboard

Copied

LATEST

Interesting. Tried both suggestions. 

Try67: Yours came up with the # of bookmarks as "undefined" (specific output was "Number of bookmarks found: undefined".

 

Bernd Alheit - that did the trick but I'm wondering how easy it'll be to then use "count" in other functions to manipulate things. I'll be doing some basic math tasks on the number of bookmarks but if it's defined explicitly in the main function, the only way to reuse it would be to make the other functions take an input of "count" and call it within main, no? For example BasicMath(count) in main function followed by a function called "BasicMath (n) { . . . ."

Thanks to both of you. Huge help from a novice trying to get something done 🙂

Votes

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