PrintToPDF should have OverWrite option

The PrintToPDF statement would be more consistent with the FileSave statement if it too was able to have the ability to overwrite any existing file with the same name.

As it is now, I believe I must first do a FileExists(, then a FileErase, then the PrintToPDF (or perhaps always do the FileErase with an Error checking.) Or am I missing something?

The PrintToPDF statement sets up all the options and then passes control to an Apple API that actually creates the PDF file. This Apple API does not have an option to overwrite the file.

I suppose the PrintToPDF statement could include a copy of the FileErase code. The only minor downside of this is that if there was an error creating the PDF, the original PDF file would be erased. But I think this is probably fine. In fact, I’m not even sure if there is any possibility of such an error.

… Ok, I’ve gone ahead and made this change. Seems to work fine.

Until the next version comes out, you’ll have to use FileErase.

let pdfName = "test.pdf"
if fileexists(pdfName)
    fileerase pdfName
endif
printtopdf pdfName

or…

let pdfName = "test.pdf"
fileerase pdfName
if error nop endif
printtopdf pdfName

Should it be an option to overwrite, as it is with saving files? That would probably be more of a problem to write, however.

Should it be an option to overwrite, as it is with saving files?

There is no overwrite option for saving files that I am aware of.

I beg to disagree: When I use a statement like this

databaseexportcsv info("desktopfolder")+"MyData.csv"

two times in a row, I get only one file “MyData.csv” — not two.

P.S. And there is no warning if I really want to replace the first file. So This is a case of overwrite.

What I said above applies for the export statement, too.

And the documentation for the filesave statement shows another example for “overwrite”:

This statement saves data directly into a disk file. … If the file does not already exist it will be created. If it does already exist its contents will be replaced!

I believe the keyword here is “option”. If a file with the name you have chosen already exists, it will overwrite whether you want it to or not. If you are saving via a dialog, it will warn you if you are about to overwrite, and give you the option of canceling.

I believe the keyword here is “option”.

That was my understanding. I interpreted Bruce’s post as a request to be able to turn this on or off, for example:

PrintToPDF filename,"overwrite","yes"

But I can’t think of any other statement that has an option like that. In every case, including the ones mentioned by @KJM, overwrite isn’t optional, it always overwrites. Starting with the next release, PrintToPDF will work the same way.

For any of these statements, if you don’t want it to overwrite, you could check for file exists before performing the operation.

if fileexists(someFile)=false()
    databaseexportcsv someFile
endif

In the Finder, if you try to save or move a file with the same name to the same place, you will be asked if you want to continue, replace, or keep both. That is what I meant.

In the Finder

The Finder is not a programming language.

If you want to implement code so that it works this way, you certainly can.

let pdfName = "output.pdf"
if file exists(pdfName)
    alertsheet pdfName+" already exists, replace it?","Buttons","Yes,No"
    if info("dialogtrigger") = "No" return endif
endif
printtopdf pdfName

The code above is for the upcoming version of Panorama, with the change I’ve made. In the current version you would need to add a fileerase statement like this:

let pdfName = "output.pdf"
if file exists(pdfName)
    alertsheet pdfName+" already exists, replace it?","Buttons","Yes,No"
    if info("dialogtrigger") = "No" return endif
    fileerase pdfName
endif
printtopdf pdfName

This code will also work with the upcoming version, the fileerase won’t hurt anything.

I just tested my procedure with PrintToPDF and it overwrote the previous file with no problems.