Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
Under Edit>Preferences (Ctrl +k) > Security (enhanced), is "Enable protectect mode at startup" unchecked? If not, uncheck it and restart Acrobat.
Copy link to clipboard
Copied
Are you running the script from the console?
Copy link to clipboard
Copied
Not the console. I have it running from the document-level javascripts.
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
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
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
Under Edit>Preferences (Ctrl +k) > Security (enhanced), is "Enable protectect mode at startup" unchecked? If not, uncheck it and restart Acrobat.
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
Happy New Year. When that box is checked you can't access the file system silently.
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
If you try to put a trusted function in a document level script it will throw a security error.
Get ready! An upgraded Adobe Community experience is coming in January.
Learn more