Re: Closing a file you left open (error -49)
Re: Closing a file you left open (error -49)
- Subject: Re: Closing a file you left open (error -49)
- From: Nigel Garvey <email@hidden>
- Date: Wed, 20 Apr 2011 11:08:19 +0100
Ruffin Bailey wrote on Tue, 19 Apr 2011 14:15:38 -0400:
>set theText to "some text"
>set theFilePath to (path to desktop as string) & "test.txt" as string
>set theFileReference to open for access theFilePath with write permission
>try
> write theText to theFileReference
>
> -- do more stuff
>
> write theText to theFileReference
> close access theFileReference
>on error
> say "error"
> close access theFileReference
>end try
>
>If something borks in "do more stuff", I'm stuck with an endlessly open
>file.
I'd prefer to see that path as a file reference ('set theFileReference
to open for access file theFilePath with write permission'), but just
using a path seems to work nowadays. Otherwise, the script above is good
enough to catch any errors that are likely to occur while the file's
open for access and to close that particular access. If the file's still
open afterwards, it's from a previous run when your script wasn't so
foolproof.
Things to bear in mind are:
1. There can be several access channels open to the same file at the
same time, each with its own file mark (ie. index into the file).
2. Only one write-permission access channel can exist to a file at any
one time.
3. Only the application which opened an access can use it. This will
normally be the application running the script, unless you've put the
'open for access' command in a 'tell' block for another application.
4. When an application quits, all its open file accesses are
automatically closed.
So, if you've made a mess while developing your script in AppleScript
Editor, and the 'open for access' command wasn't addressed to another
application, all you have to do is save your work, quit AppleScript
Editor, and then open it again. Otherwise close the other application.
Where this is difficult to do — say the offending script was run in
Script Menu — logging out and then logging back in again will do the trick.
NG
_______________________________________________
Do not post admin requests to the list. They will be ignored.
AppleScript-Users mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
Archives: http://lists.apple.com/archives/applescript-users
This email sent to email@hidden