• 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/writing a "pref" file?
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Reading/writing a "pref" file?


  • Subject: Re: Reading/writing a "pref" file?
  • From: Luther Fuller <email@hidden>
  • Date: Wed, 17 May 2006 15:44:44 -0500

Aaron Grant asked ...
I've cobbled together a small AppleScript app to enable defaults in
Backup that's working well, but would someone be able help me with
making the app look for the existence of, read from and write to a
"pref" file? The file would need to have one of two different text
strings written to it. Below is the script I have so far:

You'll need three handlers, included at the end of this posting, and your application will have to be a bundle in order to use 'CFBundleIdentifier' and create a new preference file. Read Apple's document "BPRuntimeConfig.pdf" to understand the use of CFBundleIdentifier and lots of other stuff. You will need the application "Property List Editor" (in the /Developer/Applications/ Utilities/ folder) to inspect and create property list files.


The terminology for data types in AppleScript and Property Lists are different. Here is a table of equivalences you will need:
Property List .......... AppleScript
Dictionary = Record
String = Unicode text
Array = List


You must construct the default "NewPrefsFile.plist" using Property List Editor and put it in the /Contents/Resources/ folder in your application bundle.

on getPrefsFile()
-- returns an alias to the prefs file and creates it if necessary
tell application "Finder" to set selfRef to (path to me) as alias
tell application "System Events"
POSIX path of (file "Info.plist" of folder "Contents" of selfRef)
set prefsName to (value of property list item "CFBundleIdentifier" of property list file the result)
end tell
set prefsName to (prefsName & ".plist") as text
(path to preferences from user domain) as text
tell application "Finder"
set prefsFolder to the result as alias
if not (exists file prefsName of prefsFolder) then
(file ("NewPrefsFile.plist") of folder "Resources" of folder "Contents" of selfRef) as alias
duplicate the result to prefsFolder
set name of the result to prefsName
end if
set fullPath to (prefsFolder as text) & prefsName
return (fullPath as alias)
end tell
end getPrefsFile


on readPrefs(prefsAlias)
	-- returns the prefs file's root record
	tell application "System Events"
		POSIX path of prefsAlias
		return (value of property list file the result)
	end tell
end readPrefs

on writePrefs(prefsRec, prefsAlias)
	-- writes the prefsRec to the prefs file's root record
	tell application "System Events"
		POSIX path of prefsAlias
		set value of property list file the result to prefsRec
	end tell
end writePrefs


_______________________________________________ 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: AppleScript Inquiry (FileMaker 8 Advanced)
  • Next by Date: Re: AppleScript Inquiry (FileMaker 8 Advanced)
  • Previous by thread: Reading/writing a "pref" file?
  • Next by thread: Microsoft Access, SQL and Quark
  • Index(es):
    • Date
    • Thread