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.
dim f as FolderItem=SpecialFolder.Desktop.child("photo 1.jpg")
dim txt as TextOutputStream
try
txt=TextOutputStream.Create(f)
txt.Write("xxx")
txt.close
Catch
msgbox "Failed"
end try
if I have photo open in Preview I get the “failed” message
Not sure he cares WHO has it open, just if SOMETHING does
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”
or worse…
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
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
try
txt2=TextOutputStream.Create(ft)
txt2.Write("xxx")
txt2.close
Catch
bCanWrite = false
msgbox "File is in use. Close it or try with a different name"
end try
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).