• 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
Re: Writing To A File
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Writing To A File


  • Subject: Re: Writing To A File
  • From: Richard Morton <email@hidden>
  • Date: Tue, 30 Sep 2003 14:08:28 +1000

On Tuesday, September 30, 2003, at 06:48 AM, Steve Mills wrote:
On Monday, Sep 29, 2003, at 14:52 US/Central, Randy Beaudreault wrote:
...I'm wondering if my syntax is correct or is this a bug with the Standard Additions?

The file access commands have always been problematic.

I've always found them pretty reliable actually. :) Some data types other than plain strings have problematic in some AS versions, but they've always worked fine for strings.


One thing you need to do is always call the commands from the same target. Like if you do this:

tell app "Finder"
set refNum to (open for access "disk:file" with write permission)
end tell

tell app "TextEdit"
write "blah" to refNum
end tell

It won't work, because the open file's reference number belongs to Finder, not TextEdit.

Yep. As Paul B pointed out, 'open for access' has nothing to do with the Finder. These commands are part of the Standard Additions osax.


As for writing to existing files, that's also a huge problem.

Nah. ;) I have used variations on these two handlers in many projects for years. They always work for me and do so under any version of AS:

-- overWriteFile -- by Richard Morton 2001
-- Write to a file, overwriting existing contents, creating file
-- if required. Pass a valid path string and the string to write.
to overWriteFile at pathString from writeString
try
set ofaNum to open for access file pathString with write permission
set eof of ofaNum to 0
write writeString to ofaNum
close access ofaNum
return pathString
on error errMsg number errNum
try
close access file pathString
on error
end try
error errMsg number errNum
end try
end overWriteFile


-- writeWithAppend -- by Richard Morton 2001
-- Write to a file, appending to existing contents, creating file
-- if required. Pass a valid path string and the string to write.
to writeWithAppend at pathString from writeString
try
set ofaNum to open for access file pathString with write permission
write writeString to ofaNum starting at eof
close access ofaNum
return pathString
on error errMsg number errNum
try
close access file pathString
on error
end try
error errMsg number errNum
end try
end writeWithAppend


What I've found works best in most cases is to make sure the file doesn't already exist before opening it for write by deleting it either with the Finder or Unix.

There is no reason why existing files can't be written to - it does work, I do it all the time. JD has suggested starting out by making sure the file's closed before trying to open it. This makes the process pretty bulletproof.

set pathString to (path to desktop as string) & "test.txt"

try
close access file pathString
end try


...I could be thinking of trying to open a file a 2nd time after the script had errored while it had it open before.

This is a problem - from memory it returns a less than helpful error. It is most likely what you've struck. Use JD's technique, or use a handler (like those above) that will close the file even if it does error - just make sure your code cleans up after itself.

Write Off
<http://home.netc.net.au/~sunreal/FooDooLounge/index.html>
_______________________________________________
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.

  • Follow-Ups:
    • Re: Writing To A File
      • From: Steve Mills <email@hidden>
    • Re: Writing To A File
      • From: Paul Berkowitz <email@hidden>
References: 
 >Re: Writing To A File (From: Steve Mills <email@hidden>)

  • Prev by Date: Using iCab? Need help!
  • Next by Date: Re: Writing To A File
  • Previous by thread: Re: Writing To A File
  • Next by thread: Re: Writing To A File
  • Index(es):
    • Date
    • Thread