Re: file is already open & file wasn't open
Re: file is already open & file wasn't open
- Subject: Re: file is already open & file wasn't open
- From: deivy petrescu <email@hidden>
- Date: Sat, 21 Apr 2007 13:44:39 -0400
On Apr 20, 2007, at 18:23, Gil Dawson wrote:
Hi, folks--
I'm trying to port a procedure that works in Tiger to MacOS 9.2.2
(Script Editor 1.8.3) and I'm having trouble remembering the
ancient incantations...
This works in 10.4.9:
to Append onto someFile from someText --rev 1/1/07
try
open for access file someFile with write permission
on error errm number errn
if errn = -38 then -- file was already open
close access file someFile
open for access file someFile with write permission
else
error errm number errn
end if
end try
set FileNr to result
write someText to FileNr starting at ((get eof FileNr) + 1)
close access FileNr
end Append
However, in 9.2.2, this:
set someFile to
"Max106:Applications:MacHTTP:Projects:GoogleMaps:GilLog.log"
tell application "Finder" to ¬
exists file someFile
try
set FileNr to open for access file someFile with write permission
on error errm number errn
log "Error= " & errm & ". Number=" & errn
if errn is in {-49, -38} then -- file was already open
try
close access file someFile
open for access file someFile with write permission
on error errm2 number errn2
log "Error2= " & errm2 & ". Number=" & errn2
end try
else
error errm number errn
end if
end try
Produces the following log:
tell application "Finder"
exists file
"Max106:Applications:MacHTTP:Projects:GoogleMaps:GilLog.log"
--> true
end tell
tell current application
open for access file
"Max106:Applications:MacHTTP:Projects:GoogleMaps:GilLog.log" with
write permission
(*Error= File file
Max106:Applications:MacHTTP:Projects:GoogleMaps:GilLog.log is
already open.. Number=-49*)
close access file
"Max106:Applications:MacHTTP:Projects:GoogleMaps:GilLog.log"
(*Error2= File
Max106:Applications:MacHTTP:Projects:GoogleMaps:GilLog.log wasn't
open.. Number=-38*)
end tell
So I can't open it because it is already open and I can't close it
because it wasn't open. I must be overlooking something.
Anyone remember what it is? What is 9.2.2 error code -49?
--Gil
I believe the problem you are facing is caused by your code.
You are opening a file and you are trying to close another, at least
according to AS.
When you write :
"open for access file someFile with write permission"
You are not opening someFile, actually AS creates a reference to the
file someFile, a number, and it works with that reference instead of
the file.
So your command generates a number say 113, now AS will work with 113
not with someFile.
Once you have this problem, quit whatever is your Script Editor and
relaunch it.
Now you can try again, but you should use:
"set ofasF to open for access file someFile with write permission"
Now you should write and close ofasF instead of someFile.
That should take care of your problem. However, better yet, be rude
and impolite, do not ask for permission just write to the file... go
ahead and just write:
write (space & someText) to FileNr starting at eof
Hope this helps.
Deivy
_______________________________________________
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