Where to write preferences

Deploying to Mac OS? Ask Mac OS specific questions here.

Moderators: Klaus, FourthWorld, heatherlaine, kevinmiller, robinmiller

Post Reply
Simon Knight
Posts: 543
Joined: Wed Nov 04, 2009 11:41 am
Location: Gunthorpe, North Lincs, UK

Where to write preferences

Post by Simon Knight » Thu Nov 20, 2014 12:17 pm

Hi,
I have been using some material published on these boards to create a prefs file mechanism in a utility app I am writing. At present I am writing a text file into Users/Me/Library/Preferences/MyCompanyMyAppName/Prefs_AppName.txt. This is working fine but I made the mistake of doing some web research and found the following about the preferences folder published on Apples web site:
This directory contains app-specific preference files. You should not create files in this directory yourself. Instead, use the NSUserDefaults class or CFPreferences API to get and set preference values for your app.
. The same web page states that I should probably be writing to a folder in /Library/Application Support/ :
Use this directory to store all app data files except those associated with the user’s documents. For example, you might use this directory to store app-created data files, configuration files, templates, or other fixed or modifiable resources that are managed by the app. An app might use this directory to store a modifiable copy of resources contained initially in the app’s bundle. A game might use this directory to store new levels purchased by the user and downloaded from a server.
All content in this directory should be placed in a custom subdirectory whose name is that of your app’s bundle identifier or your company.
Bearing in mind that I (we?) use Livecode to avoid terms such as NSUserDefaults am I likely to cause any problems by writing a text file to the preferences folder inside the user library?

thanks

Simon
best wishes
Skids

FourthWorld
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 7427
Joined: Sat Apr 08, 2006 7:05 am
Location: Los Angeles
Contact:

Re: Where to write preferences

Post by FourthWorld » Thu Nov 20, 2014 4:22 pm

In the olden days we could use any format that worked for us for storing preference data, and Apple's only request was that we store it in the Preferences folder. The Prefs folder was distinguished from any data in the App Support folder in that it was expected that deleting a prefs file wouldn't result in any significant loss of data for the user, while of course monkeying with the contents of App Support can have unexpected consequences.

In more recent years it appears that Apple now requires control over not only the location of your preference data, but also its format. I'm sure they have some technical justification for that, but for those of us making cross-platform apps it's just one more hurdle to overcome, extra work where the end result makes no difference to the user experience.

Oddly enough, Apple recognizes the value of allowing app devs to define their own formats for the data they store in App Support, so to accommodate Apple's new desire for control over Prefs many developers now store that info along with any other app-specific non-document data in App Support.

In LiveCode we can get the path to the App Support folder with:

specialFolderPath("asup")
Richard Gaskin
Community volunteer LiveCode Community Liaison

LiveCode development, training, and consulting services: Fourth World Systems: http://FourthWorld.com
LiveCode User Group on Facebook : http://FaceBook.com/groups/LiveCodeUsers/

Simon Knight
Posts: 543
Joined: Wed Nov 04, 2009 11:41 am
Location: Gunthorpe, North Lincs, UK

Re: Where to write preferences

Post by Simon Knight » Thu Nov 20, 2014 9:57 pm

Thanks, I have been referring to an article published in 2010.

Simon
best wishes
Skids

Simon Knight
Posts: 543
Joined: Wed Nov 04, 2009 11:41 am
Location: Gunthorpe, North Lincs, UK

Re: Where to write preferences

Post by Simon Knight » Fri Nov 21, 2014 8:51 am

Sorry me again!

A check of understanding please asI find the dictionary entry for "specialFolderPath" wanting: it lists 'asup' in the examples e.g
put specialFolderPath("asup") into appSupportFolder
and then fails to list it in the list of definitions.

'Asup' returns the path to the main system library. Unfortunately my computer login does not have write access to this system folder. Any attempts to write to the folder fail with a "can't open file" error message. I gather that this location is for global settings and should not be used for user type settings.

To get to the users library it is necessary to use "specialFolderPath("home")" with specialFolderPath("asup") or hard code the Library/Application Support/MyApp/userprefs.txt part.

best wishes

Simon
best wishes
Skids

FourthWorld
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 7427
Joined: Sat Apr 08, 2006 7:05 am
Location: Los Angeles
Contact:

Re: Where to write preferences

Post by FourthWorld » Fri Nov 21, 2014 5:47 pm

I had double-checked the "asup" value before posting, and here it returns the user's App Support. Apple may have changed that between versions (I'm still running Lion on ny main Mac for project-specific reasons).

Interestingly, it appears that when Ken Ray first tested his list supported specialFolderPath codes some years ago (see below), 'asup' used to point to where it does on your system now, to the system's App Support folder. Maybe it's a bug in Lion, or perhaps a change between the Carbon and Cocoa APIs.

Easy enough it to get past it seems: the Dictionary entry for specialFolderPath includes "support" among LC's built-in constants, which (at least on Lion) returns the path to the user's App Support. Hopefully it'll do the same for you, and if not you may need to use one of the workarounds you've found.

For more info on specialFolderPath constants, see Ken Ray's very detailed report for OS X and Windows:
http://www.sonsothunder.com/devres/live ... ile010.htm
Richard Gaskin
Community volunteer LiveCode Community Liaison

LiveCode development, training, and consulting services: Fourth World Systems: http://FourthWorld.com
LiveCode User Group on Facebook : http://FaceBook.com/groups/LiveCodeUsers/

Post Reply

Return to “Mac OS”