• 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: Simple question.... writing to file
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Simple question.... writing to file


  • Subject: Re: Simple question.... writing to file
  • From: email@hidden
  • Date: Thu, 10 Feb 2005 09:06:22 -0500

I have to write a diferent content to an existing text file, and ... the new content is shorter than the previous content, the tail of the old text remains in the file.

How do I "empty" the content of a text file before writing the new content?

You actually want to write over the file's existing text. To do that, you need to use 'set eof reference to 0' prior to any 'write ... to reference' function calls.
See the 'StandardAdditions.osax' library, via 'Script Editor's 'Window, Library' menu item, and look for 'set eof'.


Below is a simple, step by step, example - where a Desktop based 'myFile.txt' file is created with a sentence; the file is then opened, examined, and its contents replaced with a question and then saved; and again, the file is opened and its contents viewed.

---- Code starts here ----

set theFile to ((path to desktop folder) as string) & "myFile.txt"

-- Create a text file.
set FILE_REF01 to open for access file theFile with write permission
write "Hallo mein Freund in Brasilien." to FILE_REF01
close access FILE_REF01

delay 2

-- View created text file.
set FILE_REF02 to open for access file theFile with write permission
set theText02 to read FILE_REF02
display dialog theText02
set eof FILE_REF02 to 0 -- Sets 'end of file' to beginning of files text.
-- If 'set eof FILE_REF02 is 0' were omitted or commented out,
-- any further 'write .... to FILE_REF02's would be added to,
-- not write over, any of the file's existing text.
write "Wie geht es Ihnen?" to FILE_REF02 -- Should write over existing files text.
close access FILE_REF02


delay 2

-- Verifies original text was over written.
set FILE_REF03 to open for access file theFile with write permission
set theText03 to read FILE_REF03
display dialog theText03
close access FILE_REF03

-- Delete text file from Desktop.
do shell script ("rm -rf " & (POSIX path of theFile))

---- Code ends here ----
--

SJWL
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Applescript-users mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden


  • Prev by Date: Re: The future for AppleScript [was Re: where are the shell scripts ?]
  • Next by Date: Re: The future for AppleScript [was Re: where are the shell scripts ?]
  • Previous by thread: Re: Simple question.... writing to file
  • Next by thread: "choose list" question....
  • Index(es):
    • Date
    • Thread