PreferencesModule

Hi,

I’m tryin to get PreferencesModule by Paul Lefebvre to work.
It works nice until I add new preference (not in preference-file) and it raises PreferenceNotFoundException.
I don’t understand what is right way to add new preference. It works if I delete old preference file but then I lose old preferences.

Thanks

Jukka

Read the comments Paul made in the Notes section…

Yes I have read.
I add new value like: Preferences.Set(“PrefName”) = value
And it works, but if I have read preference file with Preferences.Load and this “PrefName” is not in file, this raises exception, and I don’t know how to handle it so that it adds it as new preference “PrefName”…

You MUST add the new preference to the file!

OR

Create the new preference and than use save method

As a new preference is not in the list you have to give it a default value like:

[code] PreferencesModule.Initialize(“AAdm”)

If not Preferences.Load then
Preferences.Set(“DBPath”) = “”
Preferences.Set(“TmpPath”) = “”
Preferences.Set(“ABPath”) = “”
Preferences.Set(“dataBoxWidth”) = “10%,10%,25%,25%,25%”
end if[/code]

There is this problem that if preferences file exists, it’s loaded, so these will not be set if it’s not in this file:

Preferences.Set("DBPath") = ""
Preferences.Set("TmpPath") = "" 
Preferences.Set("ABPath") = "" 
Preferences.Set("dataBoxWidth") = "10%,10%,25%,25%,25%"

But, I’ve changed this Prefs method “Get”:

If mPreferences <> Nil Then If mPreferences.HasName(name) Then Return mPreferences.Value(name) Else Preferences.Set("name") = "" 'Raise New PreferenceNotFoundException(name) End If Else Return -1 End If

So it does not raise exception, just creates new…
Am I getting in trouble with this, seems to work now?

Jukka

If you keep having trouble, have a look at “TT’s SmartPreferences” inside macoslib (https://github.com/macoslib/macoslib). It is cross-platform, though on Windows and Linux it’ll use a Mac-compatible xml (plist) file, not the registry. It has an easy-to-use API, that works almost like a Dictionary.

App.Prefs.Value("new key") = the_path dim x as string = App.Prefs.Value("other key", "default value")

The default value lets you easily check if some key is not yet set, too, because then you get the passed default value back.
There’s also a “HasKey” function.

If you could extract SmartPrefs from macoslib I would be so much more inclined to use it.

Try this:

http://files.tempel.org/RB/TTsSmartPreferences.zip

May need updating to work on 64 bit MacOS, and is probably also throwing warnings if you “Verify” your code.

If you make improvements, please send them back to me.

Noteworthy: macOS 10.12.x does sometimes remove plist files that are not compressed.
This mostly (only?) happens when there is an macOS update. It seems the updater checks if there uncompressed plist files in the preferences folder. If so, it just removes them. When the plist file is correctly handled (read: with macOS API) it doesn’t remove them.

The MBS plugins can do this.
Not sure if TTsmartpreferences does this (correctly).

You can see the difference between a compressed and uncompressed plist file by opening it in a text reader. If it is readable, it isn’t compressed.

Of course it does. I believe mine was even the first freely available prefs class that did this. Same for my Prefs Editor (http://apps.tempel.org/PrefsEditor/)

If you are a registered Einhuger Components user then I have a Preferences file handler that is here:

SimonPrefs

Just drop it into your project. There is a Notes section that shows how to use the class.

I can not find where the preferences are stored. I can not find any folder or files that have the names referred to. How do you delete and/or rename a preference.

That depends on which preferences module you selected, and which platform you are looking on.
Each of the different approaches here have a Notes object in the module that document how to use them.

MacOS 10.12.5 using XOJO 2017 R1.1 and Paul Lefebvre’s PreferencesModule.

[quote=321896:@Christoph De Vocht]Noteworthy: macOS 10.12.x does sometimes remove plist files that are not compressed.
This mostly (only?) happens when there is an macOS update. It seems the updater checks if there uncompressed plist files in the preferences folder. If so, it just removes them. When the plist file is correctly handled (read: with macOS API) it doesn’t remove them.[/quote]
Yikes! I know they’ve been advising against using your own solution for some time; but this…