In my ApplicationData folder, I have a pdf file, which I open (in it’s default application).
Is there a way to close this file, without closing the application running it?
Basically something like:
If SpecialFolder.ApplicationData.Child(“myData”).Child(“my.pdf”) is open, then close it.
I am completely at a loss as to how to do that, or if it is even possible on OS X?
Thank you all in advance.
You can use lsof through a Shell to figure out if it’s open and by what app. You can either try AppleScript to close it or ask the user to do it.
I do not think IsOf will be of much use in a sandboxed environment, if it can be run at all. As for closing a file that is open by another app, that is probably completely off limits.
Isof is not bullet proof either, even out of sandbox. Not all apps retain a hold on a file after loading it. So finding out which app has open what is often impossible.
I do quite a bit of shell work in my one MAS app, but you can’t do anything that requires su or root access.
You can do a ps -aef | grep to see what process id its running and then you can issue a kill -9 on that process id to shut it down. Its a bit of work but achievable.
I’d recommend against killing an app without warning, especially with kill -9. If some app did that to me, I’d trash it immediately.
OP was referring to a PDF open via Preview I was estimating. Who cares that preview thinks?
@Mike, I can understand Kem’s point; Imagine you had a bunch of stuff open in Preview, and some app kept quitting Preview. I’d get plenty pissed and start weighing the value of the other application. Also don’t forget that with Window Restoration, Preview would try to re-open the PDF once the user re-opened Preview.
What’s the purpose of displaying it in it’s default application? If you want to display the PDF to the end user, you can use a HTMLViewer or with some declares, you can launch a QuickLook viewer.
The point is; you’re in control of when the PDF file is open or closed.
The alternative is to write a bunch of Apple Scripts, one for each application that might have the PDF file open (providing these apps can be scripted of course), with the goal of closing just the specific document window (and hopefully thus releasing access on the PDF file).
I was opening the PDF file in it’s default application, as I would imagine the user would rather view it in that?
I’m happy to try other options, such as quick look. Are the declares available anywhere?
I have found that when a PDFs file is opened in an HTMLViewer, the cursor displays a small dotted rectangle next to it, and also that scrolling isn’t as smooth?
I emailed you yesterday, did you receive it?
As an example, the Xojo IDE does not.
I can eject an external hard disk where I have an project open in the IDE and Xojo does not warn me (not the Finder). Xojo will warn at save time (I forgot how).
I don’t see either of these issues in the HTMLViewer. This is probably related to the other issue on HTMLViewer that you have.
@Mike Cotrone , coz am old and grumpy! Now where’s my pills…
@Richard Summers My bad, I’m using the QLPreviewPanelMBS function from the MBS plugin and not declares to display the QuickLook window within my apps.