Mailing Lists: Apple Mailing Lists
Image of Mac OS face in stamp
Re: Read Write pList Files
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Read Write pList Files




Le 31 mars 2010 à 17:09, Luther Fuller a écrit :


Subject changed from Re: (no subject)

On Mar 31, 2010, at 9:54 AM, Mark J. Reed wrote:
Typo - you called the record 'defaultPrefsRec' but then referenced it
as 'defaultPrefsRecord'.

Yes, that was a typo. Thanks.

shame there's no way to create a new preferences file from System
Events, rather than shelling out to defaults write.

I would add a

do shell script "plutil -convert xml1 " & (POSIX path of prefsFile)

so that you get an XML-formatted plist instead of the default binary
format; that way the config is human-readable, which is the main
benefit of flat text files of the sort you started out with.

An older version of Property List Editor allowed you to see the file in xml format. I found this format more confusing than helpful, so I have always ignored xml. I've never used 'plutil' and I wonder if using it to convert a file to 'xml1' format would interfere with the ability to read/write its contents as an AppleScript record. (?)


I used the original one (without the typo) :

property defaultPrefsRecord : {a:1, b:"string", c:{3, 4, 5}} -- for example
property prefsName : "prefsFileName" -- lookup info on CFBundleIdentifier to find out how to name your prefs file

set prefsFolder to (path to preferences folder from user domain)
set posixPath to quoted form of (POSIX path of prefsFolder)
do shell script "defaults write " & posixPath & prefsName & " x y" -- creates the new property list file with dummy value
tell application "Finder" to set prefsFile to (file (prefsName & ".plist") of prefsFolder) as alias
tell application "System Events" to set value of property list file (prefsFile as text) to defaultPrefsRecord

and this one to change its contents.

property prefsName : "prefsFileName" -- lookup info on CFBundleIdentifier to find out how to name your prefs file

set prefsFolder to (path to preferences folder from user domain)
set prefsFile to (prefsFolder as text) & prefsName & ".plist"

tell application "System Events" to set prefsRec to (value of property list file prefsFile)

set propb to b of prefsRec
set propb to "Thanks to Luther Fuller"
set b of prefsRec to propb
tell application "System Events" to set value of property list file prefsFile to prefsRec

I had no problem to read the prefs with Property List Editor.

Yvan KOENIG (VALLAURIS, France) mercredi 31 mars 2010 17:25:51


 _______________________________________________
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

References: 
 >(no subject) (From: Oakley Masten <email@hidden>)
 >Re: (no subject) (From: Luther Fuller <email@hidden>)
 >Re: (no subject) (From: "Mark J. Reed" <email@hidden>)
 >Re: Read Write pList Files (From: Luther Fuller <email@hidden>)



Visit the Apple Store online or at retail locations.
1-800-MY-APPLE

Contact Apple | Terms of Use | Privacy Policy

Copyright © 2011 Apple Inc. All rights reserved.