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()"
})
/****************************************/
/****************************************/
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