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

Error Executing Javascript from Console/VB.Net

JNorris
Registered: Sep 7 2011
Posts: 31
Answered

I'm having issues performing this folder-level JS from the console.
 
I get an error when this function is run:
 
function DumpBookmark(bkm, nLevel)
{
var s = "";
for (var i = 0; i < nLevel; i++) s += " ";
if (bkm.name != "Root") {
bkm.execute();
number[k] = this.pageNum;
bknames[k] = bkm.name;
k++;
}
if (bkm.children != null)
for (var i = 0; i < bkm.children.length; i++)
DumpBookmark(bkm.children[i], nLevel + 1);
}
 
DumpBookmark(this.bookmarkRoot, 0);
 
Why wouldn't this work from the console?

My Product Information:
Acrobat Pro 10.0, Windows
JNorris
Registered: Sep 7 2011
Posts: 31
Oh, that is just a portion of the code by the way. It is in a trusted function with privileges.
try67
Expert
Registered: Oct 30 2008
Posts: 2398
Be more specific. What exactly goes wrong? Are you getting any error messages?

- AcrobatUsers Community Expert - Contact me personally at try6767 [at] gmail [dot] com
Check out my custom-made scripts website: http://try67.blogspot.com

JNorris
Registered: Sep 7 2011
Posts: 31
bkm.execute is not working I assume. The Array I use for the bookmarks' corresponding page numbers (number) come up as NaN instead of the actual page numbers.
JNorris
Registered: Sep 7 2011
Posts: 31
this.bookmarkRoot is being considered undefined. Why is that?
try67
Expert
Registered: Oct 30 2008
Posts: 2398
Because when the application is loading and the folder-level script executes, the "this" object is not pointing to a document.

- AcrobatUsers Community Expert - Contact me personally at try6767 [at] gmail [dot] com
Check out my custom-made scripts website: http://try67.blogspot.com

JNorris
Registered: Sep 7 2011
Posts: 31
How do I point it to the document? I can't use event.target; it returns an undefined error.
try67
Expert
Registered: Oct 30 2008
Posts: 2398
Accepted Answer
There isn't a document object in that moment. You can pass a document object to a folder-level function as a parameter when you call it.

- AcrobatUsers Community Expert - Contact me personally at try6767 [at] gmail [dot] com
Check out my custom-made scripts website: http://try67.blogspot.com

JNorris
Registered: Sep 7 2011
Posts: 31
Thanks try67. I set var d= app.activeDocs and passed d[0] as my object. Assuming this was correct as it worked.
try67
Expert
Registered: Oct 30 2008
Posts: 2398
That's a dangerous approach. If the application is opened without a document it will throw an error.

- AcrobatUsers Community Expert - Contact me personally at try6767 [at] gmail [dot] com
Check out my custom-made scripts website: http://try67.blogspot.com

JNorris
Registered: Sep 7 2011
Posts: 31
How would I pass the doc object then? And I don't understand your warning; the script is only run after files are opened so where might it err?
try67
Expert
Registered: Oct 30 2008
Posts: 2398
Is the call to DumpBookmark() located in the folder-level script itself, or somewhere else?
If in the folder-level script, then it gets executed each time the app is opened, and if there's no open file at that moment, it will error out.

- AcrobatUsers Community Expert - Contact me personally at try6767 [at] gmail [dot] com
Check out my custom-made scripts website: http://try67.blogspot.com

JNorris
Registered: Sep 7 2011
Posts: 31
The function is in a folder-level script. But the call will be from a VB.net application after a file is opened.
try67
Expert
Registered: Oct 30 2008
Posts: 2398
So there's no problem...

- AcrobatUsers Community Expert - Contact me personally at try6767 [at] gmail [dot] com
Check out my custom-made scripts website: http://try67.blogspot.com

JNorris
Registered: Sep 7 2011
Posts: 31
Good to know. Thanks again.
JNorris
Registered: Sep 7 2011
Posts: 31
I know this thread is answered but another question if you will. When executing a command from VB.Net is it not possible to call a JS that get's the Current Page Number as in the above function because I am getting an Invalid Get Error. My rewritten function is:

function DumpBookmark(bkm, nLevel, jso)
{
for (var i = 0; i < nLevel; i++)
if (bkm.name != "Root") {
bkm.execute();
number[k] = jso.pageNum;
bknames[k] = bkm.name;
k++;
}
if (bkm.children != null)
for (var i = 0; i < bkm.children.length; i++)
DumpBookmark(bkm.children[i], nLevel + 1, jso);
}

jso is the doc object being passed from the vb.



I know I can manipulate the PDF through VB, but I already have the JS if it's possible.
try67
Expert
Registered: Oct 30 2008
Posts: 2398
I don't know how the VB.Net connection to JS works, but the pageNum property belongs to a Document object. What error message are you getting, exactly?

- AcrobatUsers Community Expert - Contact me personally at try6767 [at] gmail [dot] com
Check out my custom-made scripts website: http://try67.blogspot.com

JNorris
Registered: Sep 7 2011
Posts: 31
It was weird. I was correctly obtaining and passing the doc object but getting an ==> Invalid Get Error for pageNum. All of the sudden it started working, and I believe it's because the File wasn't being opened or displayed correctly (I had the Application hidden with VB code). As soon as I removed the Hide script, it was able to make the link.