• 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: Reading and writing records
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Reading and writing records


  • Subject: Re: Reading and writing records
  • From: Luther Fuller <email@hidden>
  • Date: Fri, 24 Aug 2007 09:45:08 -0500

On Aug 24, 2007, at 8:34 AM, Chris wrote:
Luther Fuller wrote:
On Aug 24, 2007, at 7:33 AM, Chris wrote:
I know how to read and write a list of records to a file using the "as list" qualifier.

But I've been unable to discover the right incarnation to read and write one record at a time.
Is this possible? Does anyone have a code snippet for writing a list of records one at a time
and reading them back one at a time until eof?

I'm curious. What kind of file?
I read and write records from/to a file, but it's a .plist (xml) file.
Easy to do with System Events. The files are preference files, info.plist files within an application bundle, &c.
If you need to read/write some data, especially if it's already organized into records, this works very well.



If you have a code snippet I'd be interested.

A Quick Tutorial ...

You will need a copy of "Property List Editor" to read the files you create. It's in Developer Tools.

AVOID all knowledge about XML. You don't need it. It adds confusion. When you open a .plist file with Property List Editor AVOID any use of the Dump button. It shows you the XML and adds to the confusion.

There is no command in System Events to create a new .plist file. You have to create an empty or default .plist file and store it in your application bundle's /Contents/Resources/ folder. When you need a new .plist file copy and rename this file as necessary.

Reading ---------------
	-- fileAlias is an alias to the .plist file
	tell application "System Events"
		set filePath to (POSIX path of fileAlias)
		set fileRecord to (value of property list file filePath)
	end tell

'fileRecord' is now a record whose value is the root record of the file. The entire file, in other words.

You can also read a single key from a .plist file, provided you know it exists. For example ...
tell application "System Events"
POSIX path of fileAlias
value of property list item "someKey" of property list file the result
end tell


The result can be a String, Dictionary, Array, Number, Boolean, Date, Data.
(where String = Unicode Text, Dictionary = Record, Array = List)


Writing ---------------
You can write the entire contents of the .plist file ...
	tell application "System Events"
		set filePath to (POSIX path of fileAlias)
		set (value of property list file filePath) to fileRecord
	end tell

or you can write the value of single key, if it exists ...
tell application "System Events"
POSIX path of fileAlias
set (value of property list item "someKey" of property list file the result) to someValue
end tell


Modifying ---------------
When you read a .plist file into the record 'fileRecord', you may want to write it back to the file after modifying or updating it. For example ...


set fileRecord to {|newKey|: newValue} & fileRecord & {|defaultKey|: defaultValue}

1. fileRecord may or may not have had a 'newKey', but it now does have a 'newKey' which has value 'newValue'.
2. If fileRecord did not have a 'defaultKey', it now does have a 'defaultKey' and it value is 'defaultValue', but if fileRecord did have a 'defaultKey', it retains its original value.
3. Using || around key names avoids conflict with variable names in your AppleScript code.


Perhaps I could say more. Or not.

_______________________________________________
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: Reading and writing records
      • From: "Gary (Lists)" <email@hidden>
References: 
 >Reading and writing records (From: Chris <email@hidden>)
 >Re: Reading and writing records (From: Luther Fuller <email@hidden>)

  • Prev by Date: Re: Problem installing ImageMagick
  • Next by Date: Re: Problem installing ImageMagick
  • Previous by thread: Re: Reading and writing records
  • Next by thread: Re: Reading and writing records
  • Index(es):
    • Date
    • Thread