• 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: Very basic "write to file" query
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Very basic "write to file" query


  • Subject: Re: Very basic "write to file" query
  • From: Richard Morton <email@hidden>
  • Date: Thu, 5 Feb 2004 15:17:35 +1100

On 05/02/2004, at 5:27 AM, email@hidden wrote:
It works fine in Panther... and someone is getting error "(-48)
duplicate filename" in Jaguar...

try
set file_ to ((path to desktop) & "test file") as Unicode text
open for access file_ write permission 1

The variable 'file_' is a Unicode string. 'open for access' asks for a file spec (or whatever it's called these days ;). What you have works for some of the people some of the time, but this is reliable - from System 7.1 onwards:

open for access file file_ with write permission


copy the result to file_reference

You can also eliminate this line by setting the variable directly in the previous line:

set file_reference to open for access file file_ with write permission


write "test" starting at 0 to file_reference

'starting at 0' is not required if that's what you want to do, but either way it could get messy if the file already exists & has more in it than "test". If you have a file that contains the text "popsicles" and you write to it using the line above, you will end up with, ah, a different word.

The safe way to overwrite an existing file is to:

set eof of file_reference to 0

Then:

write "test" to file_reference

If you wish to append the text to any existing contents, use this:

write "test" to file_reference starting at eof


close access file_reference
on error err_msg
close access file file_
display dialog err_msg buttons {"-"} default button 1

FWIW, another way of handling this or any other error is to propagate it up the chain. Instead of displaying a dialog, just pass the error on:

on error err_msg number err_num
close access file file_
error err_msg number err_num

end try

Cheers,

N Doh-Skeletal
_______________________________________________
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: Very basic "write to file" query
      • From: email@hidden
References: 
 >Very basic "write to file" query (From: email@hidden)

  • Prev by Date: Re: Soft returns in string variable?
  • Next by Date: Re: Soft returns in string variable?
  • Previous by thread: Re: Very basic "write to file" query
  • Next by thread: Re: Very basic "write to file" query
  • Index(es):
    • Date
    • Thread