From: grobbins@Apple.COM (Grobbins)
Newsgroups: comp.sys.mac.programmer
Subject: Re: Prefs file source code
Date: 29 Sep 92 23:58:55 GMT
Organization: Apple Computer Inc., Cupertino, CA

In article <D2150096.o1pwue@sproul.sproul.com> Sproul@sproul.sproul.com (Mark Sproul) writes:
>Several people asked for this source code. ...It now handles the PREFERENCES
>folder in the system folder properly.

No, it hardcodes the name "Preferences" and assumes that it is in the
System folder.  Applications should instead use FindFolder under
Systems 6 or 7 to locate the preferences folder.  FindFolder is
documented starting on page 9-42 of Inside Mac VI.

Pasted below is the Q&A from issue 11 of develop magazine on
preference files.

Grobbins         grobbins@apple.com

Usual disclaimers apply.

-------------------------------------------------------

I want to place my user's preference file into the
Preferences folder in the System folder under System 7, but I
can't seem to get that folder's DirID and other information
so my file will appear there! Additionally, how do I get to
that folder if the user changes the names of the System
Folder and Preferences folder? Also, once it's there, am I
assuming correctly that the best way to find it again is to
make an alias record to track the file ID?
___

System 7 introduced the routine FindFolder for locating the
Preferences folder. Just make this call

   err := FindFolder(kOnSystemDisk, kPreferencesFolderType,
            kCreateFolder, prefVRefNum, prefDirID);

and, if err is noErr, then prefVRefNum and prefDirID will
contain the vRefNum and dirID of the Preferences folder. This
can be used with HCreateResFile, HOpenResFile, PBHGetFInfo,
or other File Manager calls to get to your preferences file.
If a Preferences folder does not already exist, the
kCreateFolder parameter instructs FindFolder to make one and
return the vRefNum and dirID of the new folder as prefVRefNum
and prefDirID.

FindFolder is documented in Chapter 9 of Inside Macintosh
Volume VI, under "The System Folder and its Related
Directories." While the FindFolder trap is only implemented
under System 7 (check the gestalt selector
gestaltFindFolderAttr, documented in Chapter 3 of Inside
Macintosh VI, to see if FindFolder is available), if you're
using MPW 3.2 (or the current Think compilers) glue is
automatically included in your compiled code, making it safe
to call FindFolder under System 6. The glue will check if
FindFolder is available, and if it is not, will return the
System folder's vRefNum and dirID as as prefVRefNum and
prefDirID for the kPreferencesFolderType selector. Go ahead
and use the System folder values as the location for the
preferences file under System 6.

If you're not using a development system that provides the
FindFolder glue, your code should check the FindFolder
gestalt attribute to see if FindFolder is available. If
FindFolder is available, call it. FindFolder is defined as

    FUNCTION FindFolder(vRefNum: INTEGER;folderType: OSType;
                        createFolder: BOOLEAN;
        VAR foundVRefNum: INTEGER;VAR foundDirID: LONGINT):OSErr;
        INLINE $7000,$A823;

If FindFolder is not available, call SysEnvirons to find the
System folder's WDRefNum, call PBGetWDInfo or GetWDInfo to
convert the WDRefNum to a true vRefNum and dirID, and use
those System folder numbers for the location of the
preferences file. Example code for this is in the Q&A Stack,
under Operating System:File System:Code for identifying
vRefNum and dirID of Macintosh System Folder.

To locate the Preferences folder, use the above procedure
rather than try to keep an alias of the Preferences folder or
of the preferences file. However, if there are any other
files in the System Folder upon which the application depends
(such as dictionaries) those should be tracked with aliases,
stored as 'alis' resources in the preferences file. See
Chapter 27 of Inside Macintosh Volume VI for information on
using aliases.