• 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: Read/Write Text Files
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Read/Write Text Files


  • Subject: Re: Read/Write Text Files
  • From: red_menace <email@hidden>
  • Date: Tue, 30 Mar 2010 20:16:00 -0600

I agree that using a text file for preferences isn't really the way to go.

Using a text file for mixed data isn't really the way to go either, better to read/write a record - this will preserve the data classes.  One of the problems you were having with the negative numbers is that the minus/dash is not considered when getting the "words" of something - for example, run the following:

words of "22 71 -18 -35 6"


The following is an example that writes and then reads a data record from a file:

-- set up the file name and data record
set theFile to (choose file name default name "AppleScript Test Data")
set theData to {theDate:date "Monday, April 5, 2010 12:00:00 AM", thePath:((path to desktop) & "Example.txt") as text, theColors:{"red", "blue", "green"}, theNumbers:{22, 71, -18, -35, 6}}

-- write the data to the file
do shell script "touch " & quoted form of POSIX path of theFile -- make sure it is there
try
set theOpenFile to open for access theFile with write permission
set eof of theOpenFile to 0 -- overwrite
write theData to theOpenFile as record
close access theOpenFile
on error errorMessage -- make sure file is closed
log errorMessage
try
close access theOpenFile
end try
end try

-- read the data from the file and set some variables
set x to read theFile as record -- don't need "open for access" if just reading
set myDate to theDate of x
set myFilePath to thePath of x
set myColors to theColors of x
set myIntegers to theNumbers of x

-- show stuff
log x
display dialog third item of myColors --> "green"
display dialog third item of myIntegers --> -18


- Dave



On Tue, 30 Mar 2010 12:38:18, Oakley Masten wrote:

I want to read and write a preferences file for one of my scripts.

Is there a better way to read the lines?
Is there a better way to coerce the data into a list than using "Words of"



 _______________________________________________
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

  • Follow-Ups:
    • Re: Read/Write Text Files
      • From: Michelle Steiner <email@hidden>
  • Prev by Date: Re: iPhoto and selection
  • Next by Date: Re: Read/Write Text Files
  • Previous by thread: Re: Read/Write Text Files
  • Next by thread: Re: Read/Write Text Files
  • Index(es):
    • Date
    • Thread