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.
Copy link to clipboard
Copied
E.g.:
current_doc = this;
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;
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.
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.
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.
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!
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.
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?
Copy link to clipboard
Copied
You have not defined count in your main function.
Use this:
count = CountBookmarks(this.bookmarkRoot, 0);
Copy link to clipboard
Copied
Add this as the very first line of your code:
var count;
Copy link to clipboard
Copied
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 🙂