Copy link to clipboard
Copied
Hi, currently I have a script I found that allows me to export with custom names etc. The script also checks if the file with same name already exists, and if it does, then it pops up an alert "file name already exists" Then it stops exporting.
I want to be able to continue to export and overwrite/replace the existing files.
I can't seem to figure out the Conditional statements with if, else, else if etc
This is the current code, any help would be great thanks!
// Check if PDF-file allready exists
if (myFile.exists == false){
// Export the file
myDoc.exportFile(ExportFormat.pdfType, myFile, false, myPreset);
}else{
alert("PDF-file allready exists !");
}
}
Copy link to clipboard
Copied
Hi David,
The code you have will overwrite an existing file but not inside the if statement that checks for an existing file like it currently is.
in my mind you have a couple of options. If you want the dialog box to remain, but after clicking OK over write the file, use this code.
// Check if PDF-file allready exists
if (myFile.exists == false){
// Export the file
myDoc.exportFile(ExportFormat.pdfType, myFile, false, myPreset);
}else{
alert("PDF-file allready exists !");
myDoc.exportFile(ExportFormat.pdfType, myFile, false, myPreset);
}
}
If you just want it to over write the file without the dialog box, remove the whole block and just use the export line.
myDoc.exportFile(ExportFormat.pdfType, myFile, false, myPreset);
Copy link to clipboard
Copied
Thank you, however this only gives the okay to overwrite, but what if I want it to cancel?
Also this prompts the dialog up for each pdf exported. so if I have 7 pages, it asks me 7 times. How do I have it that it ask once and will apply to all?
Thanks
Copy link to clipboard
Copied
Hi David,
If you want a choice to move ahead with the export or cancel then you can use the confirm dialog instead of an alert. Something like the following
// Check if PDF-file allready exists
if (myFile.exists == false)
{
// Export the file
myDoc.exportFile(ExportFormat.pdfType, myFile, false, myPreset);
}
else
{
var a = confirm("PDF-file allready exists. Do you want to overwrite it?")
if(a == true)
myDoc.exportFile(ExportFormat.pdfType, myFile, false, myPreset);
}
Regarding the message being showed in a loop we will need to have a look at the whole code to fix that.
-Manan
Copy link to clipboard
Copied
Hi Manan,
also consider the case where the already written PDF is opened with e.g. Acrobat and the file cannot be overwritten.
Regards,
Uwe
Copy link to clipboard
Copied
Thanks Uwe, for pointing out this use case. Well in this case i think we will have to enclose our export call within a try catch block and report the issue which will include any other case which might result in overwrite failure, unless these is any other cleaner way to deal with this.
-Manan
Copy link to clipboard
Copied
David,
The "confirm" dialog will allow you to choose to continue or cancel if the file exist.
Not sure about "How do I have it that it ask once and will apply to all?" with out seeing the rest of your code.
// Check if PDF-file allready exists
if (myFile.exists == false){
// Export the file
myDoc.exportFile(ExportFormat.pdfType, myFile, false, myPreset);
}else{
var result = confirm("PDF-file allready exists!\rYes to Continue or No to Cancel.", false);
if (result == true){
myDoc.exportFile(ExportFormat.pdfType, myFile, false, myPreset);
}
}
Regards
Mike
Get ready! An upgraded Adobe Community experience is coming in January.
Learn more