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

app.alert undefined when using "Run a Javascript"

Community Beginner ,
Feb 04, 2025 Feb 04, 2025

Hello,

 

I am attempting to get app.alert to display a message onscreen via the "Run a Javascript" menu option. Currently the contents do not matter, so I have been using the sample code provided in https://opensource.adobe.com/dc-acrobat-sdk-docs/library/jsapiref/JS_API_AcroJS.html#alert

My code is as follows:

 

app.alert({
cMsg: "Error! Try again!",
cTitle: "Acme Testing Service"
});

 

When this code is run with the right click menu > "Run a Javascript", it returns:

Line: 1: Code: 2(0x2): app is undefined
Line: 1: Code: 21(0x15): undefined is not an object

 

When run through the console, the code functions as expected.

When saved to C:\Program Files\Adobe\Acrobat DC\Acrobat\Javascripts the code runs on first start up of Adobe Acrobat and functions as expected.

 

I initially thought that app requires a privilege escalation, so I created a Trusted Function following the requirements set out in the documentation.  I was also unable to get this to work when run with the "Run a Javascript" menu option, but this time my error had changed to:

Line: 86: Code: 24(0x18): trustedAlert is not a function

I was unable to solve this issue as well, and decided to ask for help here.

 

I would like to get app.alert running without having to deal with trusted functions for the sake of simplicity, but would be happy with anything that can solve my problem.

 

Images of code, failure, and both successes are provided. If more information is needed feel free to ask for it.

Thanks!

TOPICS
General troubleshooting , JavaScript
1.1K
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
1 ACCEPTED SOLUTION
Community Expert ,
Feb 05, 2025 Feb 05, 2025

So the 3D JavaScript model is completely different from the Acrobat JavaScript model. It lives in it's own world.  Has it's own objects and functions. 

I haven't written anything for the 3d model in years, so don't know if there is a popup message window, but you can access the Acrobat model from the 3D model through the "Host" object. 

 

Try this code:

 

host.app.alert("Hello World");

 

 

 

 

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

View solution in original post

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 ,
Feb 04, 2025 Feb 04, 2025

Folder level scripts run when Acrobat starts so success on startup is correct.  Scripts in the console run when you execute them so Success on console is correct.  Where exactly are you right-clicking to "run a javascript"?  app means the Acrobat application so if you're not running it from Acrobat or within a PDF viewer, then app is undefined and not an object if it hasn't been properly defined.

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 ,
Feb 04, 2025 Feb 04, 2025

Hello,

The option is in the right click menu in Adobe Acrobat Pro. Attached is a photo of the menu I am talking about.

 

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
Community Expert ,
Feb 04, 2025 Feb 04, 2025

Where are you right-clicking to get that menu?  Is it the new 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 Beginner ,
Feb 04, 2025 Feb 04, 2025

Hello,

This is within a 3D PDF created from a SolidWorks Assembly. I didn't include this information originally as I had forgotten that the right click context menu changes when dealing with 3D Content. 

This is also within "old" Acrobat.

 

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
Community Expert ,
Feb 04, 2025 Feb 04, 2025

The code you posted is perfectly fine and will run in any JavaScript context in the Acrobat environment.

You have not actually explained how you would like the app.alert to be run. Or the scripting context of the failures you've reported. 

For the failures, did you place the script in a PDF document? for example on a form field? or in a document level script?

If so can you post the PDF?

Here's an article that may be of help. 

https://www.pdfscripting.com/public/Editing-Fields-Properties.cfm

 

 

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 ,
Feb 05, 2025 Feb 05, 2025

Hello,

I am trying to get this to run within the context of a 3D PDF Content Area. Right now, my goal is that when a user selects a part, what Adobe calls a node, then app.alert runs and displays some content to the user. For clarity I have attached both a sample 3D PDF, and a more complete example of my code. My issue is that using the right click context menu inside the 3D Content area and selecting "Run a Javascript", the console says that:

Line: 1: Code: 2(0x2): app is undefined
Line: 1: Code: 21(0x15): undefined is not an object

 

Does this mean that within the 3D PDF Javascript environment 'app' is not a usable API function? If so, can I get around this issue with a trusted function, or are those also not usable within this context? If trusted functions are not available, how can I get information to be accessible to a user without having them open the Javascript debugger and read from console.

 

The steps I have been using are as follows:

1. Open a 3D PDF, like shape.pdf that I provided, with Adobe Acrobat Pro

2. Right click within the 3D content area.

3. From the context menu that appears, select "Run a Javascript"

4. Browse to the Javascript that I want to run, in this case provided as Testing.txt, as uploading of .js files is forbidden.

5. Select a part from within the 3D PDF content.

6. Open the Javascript debugger to see that the code has failed out stating that 'app' is undefined.

 

I have also provided the file trustAlert.txt, which is my attempt to get a trusted function working. I have commented out the function call within Testing.txt as it is not immediately relevant to my primary question.

 

Let me know if you need any additional information.

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
Community Expert ,
Feb 05, 2025 Feb 05, 2025

So the 3D JavaScript model is completely different from the Acrobat JavaScript model. It lives in it's own world.  Has it's own objects and functions. 

I haven't written anything for the 3d model in years, so don't know if there is a popup message window, but you can access the Acrobat model from the 3D model through the "Host" object. 

 

Try this code:

 

host.app.alert("Hello World");

 

 

 

 

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 ,
Feb 05, 2025 Feb 05, 2025

Nice work Thom.  It works!

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 ,
Feb 05, 2025 Feb 05, 2025
LATEST

Amazing, thank you so much!

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 ,
Feb 05, 2025 Feb 05, 2025

I don't know anything about the 3D environment in Acrobat but he right-click > Run JavaScript in that context seems to allow the connection to a JavaScript file that runs outside of Acrobat.  Why not create a function in a folder level script that gets called from a menu item or custom toolbar button?  Here's a script that adds My Alert to the Edit menu and runs an alert.  A trusted function is not required for a simple alert, although it remove the JavaScript Warning message from the alert.  I tested this with your PDF and it works:

My_Alert_function = app.trustedFunction( function ()
{
app.beginPriv();

app.alert({
cMsg: "Error! Try again!",
cTitle: "Acme Testing Service"
});

app.endPriv();
})

app.addMenuItem({
cName:"My Alert",
cParent:"Edit",
cEnable: 'event.rc = (app.doc != null)',
nPos:0,
cExec:'My_Alert_function();'
})

 

 

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