• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Error writing file - when file is open
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Error writing file - when file is open


  • Subject: Error writing file - when file is open
  • From: "Bob.Kalbaugh" <email@hidden>
  • Date: Sat, 22 Sep 2001 15:50:09 -0500

If someone is not too busy and can offer any help/advice...
I have a script that generates a short html file using read/write commands.
To determine the file name for saving it, I'm using the line:

set newFile to new file with prompt thePrompt default name fileName

I like this approach because in the event that the file already exists I get
the option to replace. Easy. If the choice is replace, then I just zero the
data in the file and rewrite it. I've had no problems with it until today. I
happened to have the file open (It's just a simpletext file) and guess what?
I had no trap for THAT event. (geez)

Below is my moderately successful attempt to trap the event. Alas, it will
only trap the error -49 when the file is open by SimpleText. If it's open in
Tex-Edit Plus I'm allowed to rewrite the file. TE+ gives no warning. With
BBEdit 6.0, I'm allowed to rewrite the file but when back in BBE I'm warned
that the file was changed and it couldn't reload. I'm thinking that the
probabilty of these files being open is slim, however the possibilty remains
true.

Any thoughts or suggestions? A better approach/trap? Potential side effects
or dangers with my script?

Any advice will be greatly appreciated. And for the record, I've stripped
the leading tabs from the script. I know this hinders readability, but I'm
trying to avoid email wraps.

Thanks much in advance.

_bob.kalbaugh

-- begin script --

set theContent to "This would be the html code."
set fileName to "untitled.html"
set thePrompt to "Save this document as:"
set myFlag to true

repeat until myFlag is false

try

set newFile to new file with prompt thePrompt default name fileName

set fileRef to (open for access newFile with write permission)

set myFlag to false -- to get out of loop

-- errors if file is open
on error msg number num
set myFlag to true -- stay in loop

if num is -49 then -- file is open (simpleText)

beep

-- is there another way to get the name?
set fileName to name of (get info for newFile)

display dialog "The file \"" & fileName & "\" is in use." & [opt-l]
" Choose another name." buttons {"OK"} default button 1 with icon 2

set thePrompt to "Enter a new name:"

else

if num is -128 then -- user canceled
return
error 128 -- bail out

else

beep
display dialog msg & " Error: " & num -- some other error
end if

end if

end try

end repeat

-- check to see if user is replacing a file
set myRead to get eof fileRef -- reads the file

if myRead is greater than 0 then -- file is being replaced
set eof fileRef to 0 -- set file contents to empty
end if

try
write theContent to fileRef
on error msg number num
display dialog msg & " " & num buttons {"OK"} default button 1
end try

-- is this ok to use? There's no reference,
-- but appears to work fine. (speedier)
tell application "Finder" to update

try
close access newFile
on error msg number num
display dialog msg & " Error: " & num
end try

-- end script --

_bk


  • Prev by Date: Re: OneClick
  • Next by Date: [ANN] AppleScript Sourcebook updated
  • Previous by thread: Re: Unicode string literals under MacOS 9.1, AS 1.6?
  • Next by thread: [ANN] AppleScript Sourcebook updated
  • Index(es):
    • Date
    • Thread