When saving a PDF file, if the file exists and I want to replace it, but it is open in Preview or Adobe, the save fails.
How best to detect that the file is open already and warn the user?
… doesn’t work and also doesn’t seem to error, for example.
I’m assuming that this is for macOS since you mentioned Preview.
Check out the built in command line program lsof (list open files).
If you pass a file path to that command in a shell, it will tell you what application(s) currently have the file open.
something like this?
dim f as FolderItem=SpecialFolder.Desktop.child("photo 1.jpg")
dim txt as TextOutputStream
if I have photo open in Preview I get the “failed” message
Not sure he cares WHO has it open, just if SOMETHING does
But if nothing has the file open, won’t that code corrupt the file by writing “xxx” to it?
Or is the exception thrown on the TextOutputStream.Create line?
[quote=355162:@Jared Feder]But if nothing has the file open, won’t that code corrupt the file by writing “xxx” to it?
Or is the exception thrown on the TextOutputStream.Create line?[/quote]
This was an example… he wants to SAVE something over the file… and catch the exception if it can’t
and Yes… the CREATE is what throws the exception
Christian… how does sending it to TRASH solve the problem?
Well, if the file can’t be opened for writing, it’s blocked. In that case you can move it to the trash and make a new file at the old location
This way the app still has a valid file handle to read/write and you can write the new file.
You do not know Jeff intentions… perhaps he wants to insure he DOESN’T destroy the open file…
not to mention that you may just transfer the exception from “his app” to “preview” or “adobe”
- Adobe or Preview opens a File
- Xojo app moves file to trash
- Xojo app replace file with new content
- Xojo app quits
- Adobe/Preview detects file has been changed
- Asks user to save changes
- User mistakes, and says Yes
- File content is now back to ORIGINAL content, NOT what Xojo app wrote
This is Windows and Mac.
I quite like the textoutputstream solution.
Its perfectly acceptable to trash the file, if trashing it works the next thing to happen will be it gets replaced by a real PDF file.
Preview overwrites the file in trash. I just tested it.
Normally we fight against Indexing or AntiVirus app having the file open, so moving to trash is my usual workaround in that case.
The file can be recovered, the relevant app can continue to use it and user is happy to not see an error message.
This works for my needs.
The user has already been asked if they want to overwrite by the system saveasdialog
So I have no issue trashing it. I’ll untrash it in a moment with a new file.
[code] If pdffile <> Nil then
//got a file
//can we write?
dim ft as FolderItem=pdffile
dim txt2 as TextOutputStream
bCanWrite = false
msgbox "File is in use. Close it or try with a different name"
Does the FolderItem.IsWriteable not report false when a file is open preventing to write to it?
(I don’t know, actually, just a guess; I don’t have time to try).
IsWriteable and IsReadable can be unreliable
You’re better off to just try to read or write and handle the errors