RE: Close access file question ...
RE: Close access file question ...
- Subject: RE: Close access file question ...
- From: email@hidden
- Date: Mon, 12 Aug 2002 23:33:59 EDT
There are a number of ways around this problem. Some of the
best ways have already been mentioned in response to your
query, such as surrounding the actions in try blocks (with
close file in the 'on error' portion).
Other things that may help you during the writing/debugging
cycle include:
- set up a second variable, of type list, with a list of
all open files. I usually call this list variable "ALL" (so
I can invoke "Close Access ALL" at the very end). Add to it
each time you open a file. At end of run, try closing 'ALL'
even if they have been closed already, within a try block
(trying to repeatedly close a file at end-of-run is not a
big deal, like leaving a file open is).
- generate unique temporary file names by generating a
three to five character random number with which to pad the
filename (if you were creating MyDocument.txt, create
MyDocument#####.txt instead, i.e. - MyDocument34248.txt).
This way, each time you create a new file, it gets an
unique name, and if you have open files floating around,
you can trash them and continue to work without having to
restart (you still can't empty the trash until restart, but
since the next time you create the same file, it will have
a new name, it shouldn't be an issue for headaches). This is
sloppy coding, but it works well while you are trying to figure
out how to get file opening/writing/reading/closing operations
to work without forcing you to restart constantly.
- multibracket open/write/close attempts in try blocks (overkill works):
[portion snipped from an existing script:]
set RandNum to (random number from 10001 to 99999) as string
set OurFileName to ("Bidding Note" & RandNum) as string
-- set up the file name (taken from the item name, first 20 characters if
over 20 chars long)
set OurFilePath to ("Mac HD:Desktop Folder:" & OurFileName & ".txt")
-- try opening the file
try
open for access file OurFilePath with write permission
set OurFileRefNum to the result
set All to the result as list
on error
display dialog "couldn't start/open file"
close OurFileRefNum
end try
-- try insuring the file is set to empty (even if it exists already)
try
set eof of OurFileRefNum to 0
on error
display dialog "couldn't set output EOF to zero"
end try
-- try writing to the file
try
write OurHugeString to OurFileRefNum starting at eof
on error
display dialog "Error in trying to write Ending Time data to text file."
close access OurFileRefNum
end try
-- try closing the file
try
close access OurFileRefNum
on error
display dialog "Couldn't close access to our output text file:
reattempting"
close access OurFileRefNum
-- try an universal close
close access all
end try
Best Wishes,
=-= Marc Glasgow
*----------------------------------------------------------------*
| bIf Reality impedes your Performance, try reallocating |
| your Reality_Buffer at a higher value...b |
| (c) 1983, Marc S.A. Glasgow aka The CyberPoet(tm) |
*----------------------------------------------------------------*
| Marc S.A. Glasgow |
| Technology Specialist/Mac Consultant since 1988 |
| personal email: AardWolf Consulting |
| ThePPCGod @ AOL.COM(strip spaces) <A
HREF="
http://www.cyberpoet.net/">CyberPoet.Net</A> |
| Phone (813) 910-1500 14321 Hanging Moss Cir. #DE101 |
| VM & Digital Pager (813) 969-9800 Tampa, FL, USA |
| Fax (813) 977-4939 33613-4086 |
*----------------------------------------------------------------*
Charles Wrote:
>
Hello,
>
>
Is there a simple, global or something/someway to close
>
access to any file which may be open for access?
>
>
Thanks,
>
>
- Charles
_______________________________________________
applescript-users mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/applescript-users
Do not post admin requests to the list. They will be ignored.