Acrobat User Community Forums

You are not logged in.     Log in to your AUC account.     Don't have an account? Sign up today

#1 2008-01-08 15:26:57

andrewalb
Member
Registered: 2007-08-16
Posts: 19

Referencing an open document from a menu level sub-function

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


My Product Information:
Acrobat Pro 8.1 / Windows

Offline

 

#2 2008-01-14 16:10:17

thomp
Member

Registered: 2007-04-23
Posts: 1027

Re: Referencing an open document from a menu level sub-function

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)"
})

Offline

 

Board footer

Powered by PunBB
© Copyright 2002–2005 Rickard Andersson

AcrobatUsers.com  >>  User Groups • News • Events • Articles • Blogs • How To • Resources • Member Log in