These forums are now Read Only. If you have an Acrobat question, ask questions and get help from one of our experts.

Referencing an open document from a menu level sub-function

AndrewAlb
Registered: Aug 16 2007
Posts: 97

I am having difficulty passing a document reference into another function.
 
I have written a function that calls several other functions and compiles their return values for processing. All the functions exist in the same app folder level javascript file. The main function is called through a menu item. I have ordered the functions from top to bottom in my js file as follows:
 
1) 'child' functions.
2) 'parent function - the main one that calls and compiles the others.
3) create menu item.
 
The difficulty I am having is that when the first function calls the second function the value of "this" is lost - i.e. the app does not recognize any open documents and consequently my function returns an error since one of its actions is a this.getField() call.
 
I attempted to work around this by placing what 'this' was into a variable and then passing that variable into the second function as an argument. While this worked in the debugging console it will not work from the menu item call - even though I call them both the exact same way.
 
I included an example of the code below that will illustrate the problem I am having (it is not my real code but it demonstrates the problem). I don't understand why 1) 'this' or even 'app.doc' will not work when I go into my second function - why is it lost? And 2) why I can't pass what 'this' is into my second function as a required argument?
 
Please try this code out and let me know if you have the same problem I do. It will work in the debugger but if you call it from the menu it will fail. I just don't understand what is going on here.
 
The only requirement for this function to work is that you have a pdf doc open that has at least one page. The end result will be a console.println statement that says: "This Document Has XX Pages."
 
The menu item is under "Tools" and is called "Call Test Function" - it will be at the bottom of the list.
 
I have it placed in the following folder:
"C:\Program Files\Adobe\Acrobat 8.0\Acrobat\Javascripts"
 
Any help will be greatly appreciated as I am completely stuck here.
  
/****************************************/
function test(doc)
{
console.println("This Document Has " + doc.numPages + " Pages.")
};
 
function calltest()
{
var A = app.activeDocs
test(A[0])
};
 
app.addMenuItem({
cName:"testfunc",
cUser:"Call Test Function",
cParent:"Tools",
cExec:"calltest()"
})
/****************************************/

Andrew D. Albrecht, MS
Solutions Developer
ING USFS

My Product Information:
Acrobat Pro 8.0999999999999996447286321199499070644378662109375, Windows
thomp
Expert
Registered: Feb 15 2006
Posts: 4411
The value of "this" is highly dependant on the context of code execution. It isn't always a pointer to the doc object. The "app.activeDocs" array only contains docs that have been "disclosed". All other docs are unavailable. So the easy fix is to pass the doc object into the first function. like this

/****************************************/
function test(doc)
{
console.println("This Document Has " + doc.numPages + " Pages.")
};

function calltest(oDoc)
{
var A = oDoc;
test(A)
};

app.addMenuItem({
cName:"testfunc",
cUser:"Call Test Function",
cParent:"Tools",
cExec:"calltest(app.doc)"
})

Thom Parker
The source for PDF Scripting Info
www.pdfscripting.com
Very Important - How to Debug Your Script