Using GetJSObject to execute Javascript
Copy link to clipboard
Copied
I often use Acrobat automation in Delphi applications I write, usually without any problem.
At the moment, I'm trying to write a Delphi application to assist other developers retrieve the list of menu item names available to use with CAcroApp.ExecuteMenuItem.
So far, I've found this script which gets a list of menu item names
function FancyMenuList(m, nLevel)
{
var s = "";
for (var i = 0; i < nLevel; i++) s += " ";
console.println(s + "+-" + m.cName);
if ( m.oChildren != null )
for ( var i = 0; i < m.oChildren.length; i++ )
FancyMenuList(m.oChildren, nLevel + 1);
}
var m = app.listMenuItems();
for ( var i=0; i < m.length; i++ ) FancyMenuList(m, 0);
and I can execute that by hand (in the Acrobat gui) in the JS console window fine, and copy the output to the clipboard.
What I'd like do do is to run the script and retrieve its output back into my Delphi app, and that's where I'm stuck.
I can successfully call GetJSObject() from a CAcroPDDoc and call the methods of the various Acrobat JS objects listed in the JavaScript™ for Acrobat® API Reference. What I have not been able to find, though, is to use GetJSObject to execute an arbitrary script such as the one above and retrieve the output programatically. I noticed that in the Api Reference there is an example of using Console.PrintLn to execute a script, but I can't see any obvious way of retrieve the resulting console window output. Any suggestions?
I'm using Acrobat 8, btw.
TIA, Martyn
Copy link to clipboard
Copied
Consider using the ExecuteThisJavascript method of the Fields collection. You set the return value by assigning it to event.value in the JavaScript that you execute. More information is in the documentation.
Copy link to clipboard
Copied
As an alternative to using ExecuteThisScript, you can also install a folder level script and execute that via the JSObject. See this tutorial for more information:
Acrobat, JavaScript and VB walk into a bar... - KHKonsulting LLC

