Gregory Gan
Gregory Gan

Reputation: 105

AppleScript: "file is already open" but "file wasn't open"

Because of my slightly obsessive personality, I've been losing most of my productive time to a single little problem.

I recently switched from Mac OS X Tiger to Yosemite (yes, it's a fairly large leap). I didn't think AppleScript had changed that much, but I encountered a problem I don't remember having in the old days. I had the following code, but with a valid filepath:

    set my_filepath to (* replace with string of POSIX filepath, because typing
                          colons was too much work *)
    set my_file to open for access POSIX file my_filepath with write permission

The rest of the code had an error which I resolved fairly easily, but because the error stopped the script before the close access command, and of course AppleScript left the file reference open. So when I tried to run the script again, I was informed of a syntax error: the file is already open. This was to be expected.

I ran into a problem trying to close the reference: no matter what I did, I received an error message stating that the file wasn't open. I tried close access POSIX file (* filepath string again *), close access file (* whatever that AppleScript filepath format is called *), et cetera. Eventually I solved the problem by restarting my computer, but that's not exactly an elegant solution. If no other solution presents itself, then so be it; however, for intellectual and practical reasons, I am not satisfied with rebooting to close access. Does anyone have insights regarding this issue?

I suspect I've overlooked something glaringly obvious.


Edit: Wait, no, my switch wasn't directly from Tiger; I had an intermediate stage in Snow Leopard, but I didn't do much scripting then. I have no idea if this is relevant.

Upvotes: 1

Views: 1751

Answers (3)

Ted Wrigley
Ted Wrigley

Reputation: 3194

When I've had this problem, it's generally sufficient to quit the Script editor and reopen it; a full restart of the machine is likely excessive. If you're running this from the Script Menu rather than Script Editor, you might try turning off the Script Menu (from Script Editor) and turning it back on again. The point is that files are held by processes, and if you quit the process it should release any lingering files pointers.

I've gotten into the habit, when I use open for access, of using try blocks to catch file errors. e.g.:

set filepath to "/some/posix/path"

try
    set fp to open for access filepath
on error errstr number errnom
    try
        close access filepath
        set fp to open for access filepath
    on error errstr number errnum
        display dialog errnum & ": " & errstr
    end try
end try

This will try to open the file, try to close it and reopen it if it encounters and error, and report the error if it runs into more problems.

Upvotes: 2

user12288916
user12288916

Reputation:

An alternative (and what I usually do) is that you can also comment out the open for access line and just add in a close access my_file to fix it.

Upvotes: 0

regulus6633
regulus6633

Reputation: 19030

Agreed that restarting is probably the easiest solution. One other idea though is the unix utility "lsof" to get a list of all open files. It returns a rather large list so you can combine that with "grep" to filter it for you. So next time try this from the Terminal and see if you get a result...

lsof +fg | grep -i 'filename'

If you get a result you will get a process id (PID) and you could potentially kill/quit the process which is holding the file open, and thus close the file. I never tried it for this situation but it might work.

Have you ever had the Trash refuse to empty because it says a file is open? That's when I use this approach and it works most of the time. I actually made an application called What's Keeping Me (found here) to help people with this one problem and it uses this code as the basis for the app. Maybe it will work in this situation too.

Good luck.

Upvotes: 1

Related Questions