How to save extension preferences -- question about write_default & read_default

Hey there,

I am developing an extension and I am looking for a way to save the preferences.
It looks like using the “write_default” and “read_default” is a very convenient way. The API is also very friendly.

But it looks like not persistent. I did some tests in the ruby console like this

Sketchup.write_default("MyPluginName", "Preferences", "HelloWorld")    #return true
Sketchup.read_default("MyPluginName", "Preferences", "")   #return "HelloWorld"

All good by now. Then I restarted the SketchUp and tested this in the console.

Sketchup.read_default("MyPluginName", "Preferences", "")   #return ""

It seems that the setting is not saved. Did I miss something?

Thanks a lot in advance!


The failure to save default values on Windows is often caused by incorrectly installing SketchUp.
This can cause files to have the wrong permissions and not getting written to etc…

If your problem is Windows related then you can ‘Repair’ SketchUp and avoid the issue…
Here’s how…
Whilst logged into Windows as your normal user-account, ensure that SketchUp is closed.
Find the SketchUp installer’s exe file - usually in your Downloads folder.
Select the installer’s exe file, right-click > context-menu > “Run as administrator”
With an existing installation choose ‘Repair’ in the dialog that opens [‘Install’ if it’s a new installation]
When it completes restart SketchUp…
Repeat your test with the write/read of the default values - restart it and test again…
Any improvement ?
[Never ‘Run’ an installer exe file by double-clicking it - even if your user-account has admin powers it is not the same thing…]

C:\Users\NAME\AppData\Local\SketchUp\SketchUp 2019\SketchUp\PrivatePreferences.json
is the actual file where these ‘default’ key/values are saved to, and read from…
Obviously you need to change the NAME to your own Windows user-account and your SketchUp version to suit…
This AppData folder-path is usually ‘hidden’, but pasting the corrected path in a Windows Explorer window should open a file or its parent subfolder…


Firstly, please learn how to properly post code in the forum …

Secondly, (and yes I realize you were just testing,) but the preferences JSON files are a shared filespace.

This means that everyone cannot be using plain descriptive names for their settings keys. Ie “HelloWorld” is too generic and could clash if someone else uses the same identifier.

Just as extensions need to have uniquely named registrar files and folders, they also need to use the same unique names for settings keys and attribute dictionary names. (All 3: files, dictionaries and settings are in shared spaces.)

So Tom, let us say that you choose to use “DrawFun” as your unique namespace identifier. (This is equivalent to an author or company name. Ie, a “fictitious name” if you will.)

So, your “HelloWorld” extension registrar file in the “Plugins” folder would be named "DrawFun_HelloWorld.rb". The extension subfolder must use the same name (but without the .rb file type.) Ie, "Plugins/DrawFun_HelloWorld" would be the extension subfolder.

Then you must use the same identifier ("DrawFun_HelloWorld") for this extension’s preferences key and any attribute dictionaries that your extension attaches to model objects.

All of the code in all of your extension’s files must be module wrapped using the same identifiers like so …

module DrawFun
  module HelloWorld

    # This extension's code ...

  end # extension submodule
end # top level namespace module

The fully qualified identifer to your extension is: DrawFun::HelloWorld.
(Basically the underscore is replaced with Ruby’s :: scope operator.)

Thirdly … lets get back to the preamble of your topic’ title … “How to save extension preferences” …

There are quite a few topics covering this more generally including what is the best way.

I feel that using “write_default” and “read_default” has not really been “the recommended wayby developers. Let us say that it is an old way that has long been available by the API.

But it has quirks… read_default evaluates the stored values in Ruby which can lead to weirdness.

Many developers have instead switched to using their own JSON files that are separate from SketchUp’s. I posted example of methods only 3 weeks ago …

1 Like

Thanks, TIG!
I fixed this issue after the “Repair”.
Now I can find the saved preferences in the PrivatePreferences.json file.

One interesting finding is that the update of the PrivatePreferences.json is triggered when the SketchUp is starting - I thought it would happen in the “write_default” call or the close stage.

Thanks, DanRathbun!
I have updated the post - with formatted code and identifier.

Though I have solved the read_default/write_default issues I would prefer to use a separated JSON in my coming development based on your suggestions.