Getting / Setting a Komodo preference

Posted by toddw on 2009-02-04 16:46
OS: All / Any | Product: Komodo | tags: komodo preference set
Question: 

How do I change a specific Komodo preference?

Answer: 

Komodo's uses a number of preferences to control how the program is run. Most preferences are modified through the Komodo preferences dialog, though there are some that are not exposed through the UI.

The preferences are based on a default "prefs.xml" which can be found inside your Komodo installation directory, and then any preference changes to the base preferences are stored into your Komodo user data directory. This file stores a number of different preference types:

You can see the base OpenKomodo (Komodo Edit) "prefs.xml" file here:
http://svn.openkomodo.com/openkomodo/view/openkomodo/trunk/src/prefs/pre...

These preferences can be modified by changing the XML files manually (and then restarting Komodo), or can be changed using the Komodo API, such as through a JavaScript macro.

Some example JavaScript macros (which can be added to your Komodo toolbox) for changing preferences are listed below:

Getting a boolean preference

var pref_name = ko.interpolate.interpolateStrings(["%(ask: Boolean pref name)"]);
if (pref_name) {
    var prefs = Components.classes['@activestate.com/koPrefService;1'].getService(Components.interfaces.koIPrefService).prefs;
    if (prefs.hasBooleanPref(pref_name)) {
        alert("'" + pref_name + "': " + prefs.getBooleanPref(pref_name));
    } else {
        alert("No boolean pref exists for '" + pref_name + "'");
    }
}

Setting a boolean preference

var pref_name = ko.interpolate.interpolateStrings(["%(ask: Pref name)"]);
if (pref_name) {
    var pref_value = ko.interpolate.interpolateStrings(["%(ask: Value: 1)"]);
    if (pref_value !== null) {
        var prefs = Components.classes['@activestate.com/koPrefService;1'].getService(Components.interfaces.koIPrefService).prefs;
        //dump("Setting pref '" + pref_name + "' to '" + pref_value + "'\n");
        if (pref_value == "1" || pref_value.toLowerCase() == "true") {
            prefs.setBooleanPref(pref_name, true);
        } else {
            prefs.setBooleanPref(pref_name, false);
        }
    }
}

Setting a string preference

var pref_name = ko.interpolate.interpolateStrings(["%(ask: Pref name)"]);
if (pref_name) {
    var pref_value = ko.interpolate.interpolateStrings(["%(ask: Value)"]);
    if (pref_value !== null) {
        var prefs = Components.classes['@activestate.com/koPrefService;1'].getService(Components.interfaces.koIPrefService).prefs;
        //dump("Setting pref '" + pref_name + "' to '" + pref_value + "'\n");
        prefs.setStringPref(pref_name, pref_value);
    }
}

Setting a number preference

var pref_name = ko.interpolate.interpolateStrings(["%(ask: Pref name)"]);
if (pref_name) {
    var pref_value = ko.interpolate.interpolateStrings(["%(ask: Value)"]);
    if (pref_value !== null) {
        var prefs = Components.classes['@activestate.com/koPrefService;1'].getService(Components.interfaces.koIPrefService).prefs;
        //dump("Setting pref '" + pref_name + "' to '" + pref_value + "'\n");
        prefs.setLongPref(pref_name, parseInt(pref_value));
    }
}

API methods on a Komodo PreferenceSet

To see a full list of API methods when writing a JavaScript macro, you'll need to query the preference into a Komodo preference set, example:

var prefs = Components.classes['@activestate.com/koPrefService;1'].getService(Components.interfaces.koIPrefService).prefs;
var prefset = prefs.QueryInterface(Components.interfaces.koIPreferenceSet);
prefset.<|> // Completions seen here.