Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

How to make two javascript callback functions to work in the same running context inside Acrobat?

Community Beginner ,
Apr 21, 2023 Apr 21, 2023

I am a researcher.  I am trying to develop a labeling tool in acrobat with javascript.  The tool should be able to create a bound box, the four sides of which can be moved by a mouse.

 

The code works fine during the developing process, until I move the calling code into the callback function of a menu item.  The questionable code is this line:

app.addMenuItem({cName:"Add a box"cParent: "Edit"cExec: "mybox = new BoxWithLabel({xL: 300, xR: 450, yD: 300, yU: 400, this: this}); mybox.showBox(); mybox.buttonLeft.setAction(\"MouseUp\"\"mybox.onButtonMouseUp('left');\");"});
 
The callback will be executed by acrobat, when I click the menu item.  It will create an object mybox.  Everything works fine until I click the left button of the bounding box, which will activate the call back function: mybox.onButtonMouseUp('left');
 
For reasons that I don't know, Acrobat throw the following errors:
GeneralError: 
Doc.canAccessFieldTree:685:Console undefined:Exec
Missing Mesh Restriction
 
In the javascript console, I can debug my code.  I saw the code reach a line that it shouldn't reach.
04.1.jpg
 
I respond by adding a return at this position of the code.  But it doesn't fix the problem.  Here is the debugging window.
04.2.jpg
 
With or without the return, the results of the code are the same.  My button disappeared and acrobat reported an error.
05.jpg
 
What should I do?  Any feedback helps.  Thanks a lot for reading my post.  Thanks.
 
Because this code is used for a research project, I don't want to open-source it before we publish a paper.  So I didn't attach the source code here.  But I can reply an email with the source code, if you need to reproduce the error.  Thanks.
My email address is: wulin@kust.edu.cn
TOPICS
Crash or freeze , Edit and convert PDFs , JavaScript
1.7K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Apr 22, 2023 Apr 22, 2023

What's the canAccessFieldTree method? Is it something you defined? If so, where is it placed, and how is it associated with the doc variable (which I assume points to a Doc object)?

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Apr 22, 2023 Apr 22, 2023

I didn't defined canAccessFieldTree.  I guess it is defined by acrobat.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Apr 23, 2023 Apr 23, 2023
LATEST
quote

I didn't defined canAccessFieldTree.  I guess it is defined by acrobat.


By @Lin29507547y6ua

 

That makes sense. The other part of the error message is "Missing Mesh Restriction".  I'm pretty sure this message is related to the 3D JavaScript model. Quite often, Acrobat reports wrong errors. I've seen the 3D model errors get reported for exceptions on document model functions.  Not very helpful. 

 

 

Thom Parker - Software Developer at PDFScripting
Use the Acrobat JavaScript Reference early and often

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Apr 22, 2023 Apr 22, 2023

So, you're asking us about an internal problem with a complex system of your own design?  You've created you're own set of objects and terminalogy. How are we supposed to know what you are talking about?

 

Given that I don't know anything about your code I can't say anything specific, but there are some general Acrobat scripting technique issues that may help you. 

 

The error shown isn't from code in the menu, mouseup action, or a folder level script. It's from code run in the console.  It's reporting that an object member named "canAccessFieldTree" is not defined. I'm assuming that "Doc" is a variable representing the a document object. It's important that you've run code in the console that defines functions and variables because of context, which I'll explain below. 

 

First, a helpful practice is to put any code that is more than one very simple line into a fucntion.  For example the code executed by the menu item. It's also very helpful, if the code is debuged, to place these functions in a folder level script.

Next, the Acrobat scripting environment is context sensitive. So it's important to understand and control the context of your code.  For exmaple, it may be that a particular operation requires privilege, in that case the code for that operation needs to be place in a privileged context, such as a folder level trusted function. Context also controls the meaning of "this". When you executed code in the console window it was in the context of the current document object, so "this" was the documement object. It also means that any functions or variables defined by that code only exists for that document object. If you changed documents, the menu code probably wouldn't work at all.

The context is different for code run in a folder level script, and code run from a menu item or toolbar. This may be why your document level variable isn't being set properly.  For a menu item script, the current document is in the "event.target" property. It is also in the undocumented "app.doc" property. The context can also change when one fucntion calls another. So any necessary data that may be context sensitive needs to be explicitly passed it the called function.    

 

Another possible issue is your box. It appears to be a set of buttons and possibly a rectangle annotation. If this is true, then you'll need to handle the coordinates spaces properly or the system will break down with a page that is rotated and/or has different media and crop boxes.

In fact, it all seem overly complex. A simpler solution is to use a single button as the box. Start a timer on mouse down that runs a function to track the mouse position and use it to move the button. On mouseup, kill the timer. 

  

 

Thom Parker - Software Developer at PDFScripting
Use the Acrobat JavaScript Reference early and often

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Apr 22, 2023 Apr 22, 2023

Thanks for your immediate and sincere reply.  I found my bug.  I made the bug by moving the code from the document level to the folder level.  I had thought the bug is because of the code I newly wrote.  I debugged it in the wrong direction.  Thanks for pulling me back on the right track.  Thanks.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines