Run an Acrobat JavaScript Merge Script from a Toolbar Button
Question details
The user wants to run an Acrobat JavaScript PDF merge script without repeatedly opening the console and pasting the code.

- Product
- Adobe Acrobat
- Device & OS
- not provided
- Scenario
- Run an existing PDF merge script from a custom tool or keyboard shortcut.
- Observed behavior
- The desired workflow is to launch the merge script directly instead of pasting and executing it in the JavaScript console each time.
Confirm that your Acrobat edition supports the JavaScript APIs used by the merge script, and locate the folder-level JavaScripts directory for your Acrobat version and operating system. Review the script's file paths and permissions before making it available as a toolbar command.
Register the merge script as an Acrobat toolbar button
Use a folder-level script to add a button that runs the merge command without opening the console.
Operations such as file access, app.openDoc, and app.newDoc can require a trusted context. Use Acrobat's trusted-function mechanism for privileged operations rather than weakening security settings.
Store a document's numPages value before closing it. Accessing the document after it is closed can cause a DeadObject error.
Place the merge code in a .js file in Acrobat's documented folder-level JavaScripts directory. Define a function that runs the merge and checks the required file paths.
Wrap operations that require elevated privileges in an app.trustedFunction and use app.beginPriv() and app.endPriv() as required by the Acrobat JavaScript API. Ensure app.endPriv() runs even if an operation fails.
In the same .js file, call app.addToolButton with a unique button name and a cExec action that invokes the merge function. Set an informative tooltip so the button is easy to identify.
Restart Acrobat to load the folder-level script, then select the custom button on the toolbar to run the merge. If the button is not visible, check the toolbar configuration and JavaScript console for errors.

Work with PDFs and Office files in WPS Office
WPS Office is a free, lightweight alternative with a familiar interface for working with PDFs and Office documents. Its format compatibility helps make moving everyday files from Microsoft Office straightforward; it does not run or repair Acrobat JavaScript tools.

Frequently Asked Questions
Can I assign a keyboard shortcut to any Acrobat JavaScript?
Acrobat does not offer a general shortcut for arbitrary scripts. For this merge workflow, register a custom toolbar button with app.addToolButton instead.
Why does my merge script need a trusted function?
Some file-access and document operations are security-restricted. Put operations that require privilege inside an app.trustedFunction and follow Acrobat's rules for app.beginPriv() and app.endPriv().
Why does the script report a DeadObject error after closing a PDF?
A closed document object can no longer be accessed. Read and store values such as doc.numPages before calling the document's close method.
Can a script execute code by writing it to the JavaScript console?
No. A script can display text in the console, but writing code there does not execute it. Register the merge function as a toolbar command instead.




