Copy link to clipboard
Copied
Need some help here. I am writing some javascript on a acrobat form using a folder level script to see if a file exists. This does not seem to work for me for some reason. If don't have LiveCycle will the code below still work? Is there a setting I am missing?
Folder Level
var IfFileExists = app.trustedFunction(function(filename) {
app.beginPriv();
var existingDoc = false;
try {
var checkDoc = app.openDoc(filename);
checkDoc.closeDoc();
existingDoc = true;
} catch(e) {
existingDoc = false;
}
return existingDoc;
app.endPriv();
});
Doc Level
var onumber = this.getField("info_Order#");
var FileDir = ("/Volumes/PFS/" + onumber.value + ".fdf");
if(typeof(IfFileExists) == "function") {
try {
if(IfFileExists(FileDir) == true) {
app.alert("file is there");
} else {
app.alert("file is NOT there");
}
}
catch(e) {
app.alert("file is NOT there");
}
}
This function doesn't return true/false. It returns a Stream object or throws an exception, so use it like this:
try {
util.readFileIntoStream(FileDir);
app.alert("The file is there");
} catch (e) {
app.alert("The file is NOT there");
}
Copy link to clipboard
Copied
Your IfFileExists method looks like it can work, but only if the file is disclosed. Also, keep in mind it will only work with PDF files, not FDF files or any other file type (unless you convert that file to PDF).
Copy link to clipboard
Copied
Thanks for the quick reply. New to Acrobat scripting. The files are all FDF files, so this will probably not work. Maybe I need to setup a database to use this method. Can you point me in the right direction?
Copy link to clipboard
Copied
You can't program databases in Acrobat JS.
You can use the importAnFDF command, but if you specify to it a non-existing file-path it will cause an Open dialog to appear, which might not work for you...
Another option is to use util.readFileIntoStream(). It will return a Stream object if successful, or will throw an Exception if the file doesn't exist.
Copy link to clipboard
Copied
I tried this but it doesn't work. It skips to the else argument?
var onumber = this.getField("info_Order#");
var FileDir = ("/Volumes/PFS/" + onumber.value + ".fdf");
stmData = util.readFileIntoStream(FileDir);
if (stmData == true) {
app.alert("The file is there");
} else {
app.alert("The file NOT there");
}
Copy link to clipboard
Copied
This function doesn't return true/false. It returns a Stream object or throws an exception, so use it like this:
try {
util.readFileIntoStream(FileDir);
app.alert("The file is there");
} catch (e) {
app.alert("The file is NOT there");
}
Copy link to clipboard
Copied
Thanks. This works. The try and catch is new to me. Thanks for all your help.
Find more inspiration, events, and resources on the new Adobe Community
Explore Now