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

importDataObject() will not attach file using a path until it is run without a path first (Windows)

New Here ,
Dec 23, 2025 Dec 23, 2025

I have the following in a document-level javascript:

//Refresh JSON file attachment
function reload_equipment_list(attachment_name, full_path) {
    if (this.dataObjects != null) {
        //Delete the existing equipment list
        this.removeDataObject(attachment_name);
    }

    //Import the new equipment list
    this.importDataObject(attachment_name, full_path); //automatic form
    //this.importDataObject(attachment_name);            //manual form
}

 

On Windows, this line (which I'm calling the automatic form) fails:

this.importDataObject(attachment_name, full_path);

I don't get an error message in the console or anything, the function just refuses to import the file.

 

If I use this form, (which I'm calling the manual form):

this.importDataObject(attachment_name);

It allows me to choose the file I want. Once I run this form at least once, the automatic form will suddenly start to work. It's almost like the function doesn't have permission to access the file directory until the manual form is run.

 

If you re-open the document, the behavior resets. You have to run the manual form once for it to behave as expected. This is not the case on Mac. The automatic form works on startup without having to run the manual form.

 

I would like to know if this is a bug or if there is some setting in the Windows version I'm missing.

TOPICS
JavaScript
563
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 ,
Dec 30, 2025 Dec 30, 2025

Under Edit>Preferences (Ctrl +k) > Security (enhanced), is "Enable protectect mode at startup" unchecked?  If not, uncheck it and restart Acrobat.

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 ,
Dec 23, 2025 Dec 23, 2025

Are you running the script from the console?

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
New Here ,
Dec 23, 2025 Dec 23, 2025

Not the console. I have it running from the document-level javascripts.

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 ,
Dec 23, 2025 Dec 23, 2025

I understand you have the function as document level script as you stated.  I was asking about the script that runs the function.  Is that from a button on the form?  If it is, then it shouldn't work according to the documentation because that is not a privileged context as @Thom Parker stated.  

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 ,
Dec 23, 2025 Dec 23, 2025

If you look at the reference, it states that this function requires privilege if the path is specified. 

Reference entry: https://opensource.adobe.com/dc-acrobat-sdk-docs/library/jsapiref/doc.html#importdataobject

 

Here's an article that discusses privilege and the trusted function:

https://www.pdfscripting.com/public/Using-Trusted-Functions.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
New Here ,
Dec 23, 2025 Dec 23, 2025

I did come across that when I was first writing this code. I added the folder containing the PDF and the JSON to Privileged Locations. Is that not enough on Windows? Because it's working on Mac.

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 ,
Dec 23, 2025 Dec 23, 2025

You must rewrite the function as a trusted function in a text file, save it as a JavaScript file (.js file extension), place this file in the application JavaScripts folder, and remove the doc level script.

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
New Here ,
Dec 29, 2025 Dec 29, 2025

I created a file called importEquipmentList.js and placed it in C:\Program Files\Adobe\Acrobat DC\Acrobat\Javascripts.

 

The js file contains:

var Trusted_ImportEquipmentList = app.trustedFunction(function() {
    app.beginPriv();
    app.alert("Privileged function");
    app.endPriv();
});

 

I have a document-level script called Units which contains:

pathvar = this.path.substring(0,this.path.lastIndexOf('/') + 1);
attachment_name = "equipment_list";
full_path = pathvar + attachment_name + ".json";

//Refresh JSON file attachment
function reload_equipment_list(attachment_name, full_path) {
    if (this.dataObjects != null) {
        //Delete the existing equipment list
        this.removeDataObject(attachment_name);
    }

    //Import the new equipment list
    Trusted_ImportEquipmentList();
}

reload_equipment_list();

When opening the document, I get the alert pop-up which tells me I have it set up correctly.

 

Next, I changed the contents of importEquipmentList.js to:

var Trusted_ImportEquipmentList = app.trustedFunction(function(attachment_name,full_path) {
    app.beginPriv();
    this.importDataObject(attachment_name,full_path);
    app.endPriv();
});

And in the document-level script, I added the parameters to the trusted function call and the call at launch.

//Import the new equipment list
Trusted_ImportEquipmentList(attachment_name,full_path);
reload_equipment_list(attachment_name,full_path);

 

Nothing happens. Same as before. No error message or anything, it just refuses to import the file.

 

I also ran 

Trusted_ImportEquipmentList(attachment_name,full_path);

from the console. I get 'undefined' as the output, but the JSON doesn't import.

 

I tried the manual form of importDataObject() in the console, chose the JSON file and it imported successfully. I then manually removed the attachment and ran the document-level script again. It worked this time. I closed the document, opened it and once again, it's not importing the JSON. Despite the privileged javascript file, it still only works if I run the manual form first.

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 ,
Dec 29, 2025 Dec 29, 2025

Try writing one trusted function for the entire process and calling the function in a document level script like this:

//Trusted function
Trusted_ImportEquipmentList = app.trustedFunction( function (oDoc, attachment_name, full_path)
{
app.beginPriv();

if (oDoc.dataObjects != null) 
{oDoc.removeDataObject(attachment_name);}

oDoc.importDataObject(attachment_name,full_path);

app.endPriv();
})

//Document level script
//Define variables
//var pathvar = this.path.substring(0,this.path.lastIndexOf('/') + 1);
//Alternative to your line above
var pathvar = this.path.replace(this.documentFileName,"");
var attachment_name = "equipment_list";
var full_path = pathvar + attachment_name + ".json";
//Call function
Trusted_ImportEquipmentList(this,attachment_name,full_path);
//You shouldn't have 'var' in front of the line above unless you're naming a variable to represent the function.

I prefer the alternative line because it is simpler, and it also works for Windows and Mac.

 

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
New Here ,
Dec 30, 2025 Dec 30, 2025

Thanks for the simpler path code! I made the changes you suggested but it's still behaving the same way. It will not work until the manual form of importDataObject is run first.

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 ,
Dec 30, 2025 Dec 30, 2025

Under Edit>Preferences (Ctrl +k) > Security (enhanced), is "Enable protectect mode at startup" unchecked?  If not, uncheck it and restart 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
New Here ,
Dec 30, 2025 Dec 30, 2025

That was the issue! I had that setting unchecked on my Mac but not on my PC. I even reverted the code back to the original and it still works. Thank you for your help and happy new year.

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 ,
Dec 30, 2025 Dec 30, 2025
LATEST

Happy New Year.  When that box is checked you can't access the file system silently.

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 ,
Dec 23, 2025 Dec 23, 2025

Even though the document is marked as privileged, the code inside the function is not.  Try making it a trusted function. I don't know if this will work. If it doesn't then you'll need to do as suggested by PDFAS, create the trusted function in a folder level script. This is usually the easiest, and most reliable way to do this anyway.  

 

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 ,
Dec 24, 2025 Dec 24, 2025

If you try to put a trusted function in a document level script it will throw a  security error.

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