• Hello MLAers! We've re-enabled auto-approval for accounts. If you are still waiting on account approval, please check this thread for more information.

Saving Preferences for Classic Mac Apps

I’m wondering what the canonical way was to save the user preferences was for Classic macOS was - I thought I could just write a file but I think by design the Toolbox APIs need to have a file handle provided by the open / save dialog. I guess that apps save their prefs somehow in the resources? Does anyone have any info or experience in implementing this? I’m using Code Warrior 6 on MacOS 8.6 but am struggling to find any examples of how to do this.
 
I thought I could just write a file

Yes. You can just open a file using the standard C library (fopen). The Macintosh toolbox is slightly more complicated but gives you greater control. https://archive.org/details/inside-macintosh-1992-1994/1992-files/

In System 7 and later:
1. Use FindFolder to find the volume (drive) and directory (folder) id of the System Folder's Preferences Folder
2. Using that location and a preferences filename of your choice, call HOpenDF (note 1). If you get a fnfErr, then file doesn't exist. Create it using HCreate and then call HOpenDF again. This returns the refNum ('file handle') you expected from the open/save dialog.
3. Use FSWrite to write to your preferences file, or FSRead to read from it.
4. Use FSClose to close the file after reading or writing.

Note 1: As you pointed out, you can use the data fork or you can add a resource fork to your preference file and store resources in it.
 
Thanks that was really helpful! While digging into it I found that PowerPlant has an LPreferencesFile class which nicely wrapped a lot of that for me.
 
Back
Top