Where to write preferences

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

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

Post Reply
Simon Knight
Posts: 919
Joined: Wed Nov 04, 2009 11:41 am

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: 10043
Joined: Sat Apr 08, 2006 7:05 am
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
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn

Simon Knight
Posts: 919
Joined: Wed Nov 04, 2009 11:41 am

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: 919
Joined: Wed Nov 04, 2009 11:41 am

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: 10043
Joined: Sat Apr 08, 2006 7:05 am
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
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn

opn
Posts: 7
Joined: Fri Jun 19, 2015 9:46 am

Re: Where to write preferences

Post by opn » Thu Feb 20, 2025 10:07 am

Is there a cross platform library that helps us write "preferences" for app we make on macOS, iOS, Android etc? I'm looking at the conversation above and aiming to upload the idiosyncratic code I have been using for a while and replace it with something more robust and thought through.

Klaus
Posts: 14177
Joined: Sat Apr 08, 2006 8:41 am
Contact:

Re: Where to write preferences

Post by Klaus » Thu Feb 20, 2025 11:03 am

Hi opn,

no need for a library, put this li'l function into your stack script:

Code: Select all

function the_prefsfolder
   switch the platform
      case "MacOS"
         put specialFolderPath("preferences") into spfp
         break
      case "Win32"
         ### For all users:         
         put specialFolderPath(35) into spfp
         
         ### for the current user only_
         ### specialfolderpath(26)
         break
      case "android"
      case "iphone"
         put specialfolderpath("documents") into spfp
         break
      case "web"
         ## sorry no idea?
         ## Probably -> specialfolderpath("documents")
         break
      default
         ## Linux
         put $HOME into spfp
         break
   end switch
   return spfp
end the_prefsfolder
Hope that helps!

Best

Klaus

jmk_phd
Posts: 216
Joined: Sat Apr 15, 2017 8:29 pm

Re: Where to write preferences

Post by jmk_phd » Fri Mar 14, 2025 1:45 am

For what it's worth, I've found that the function provided by Klaus in his latest post -- which I learned from him some years ago, and which in my code is actually commented as "the Klaus function" -- still apparently works just fine in Mac and Win builds. The clinicians who employ my Mac/Win app routinely in their clinical work -- all of whom run far more recent versions of the corresponding OS than I do -- have not complained of any problem.

Inasmuch as I use that preference file to implement a licensing copy-protection scheme, I'm sure that I'd hear immediately if this strategy no longer worked and they suddenly were locked out of the full app.

jeff k

Post Reply