Macotron

macotron.prefs

Read and write the current user's preferences, the values that defaults read and defaults write reach, without a subprocess.

// Hide desktop icons, then restart Finder so it notices.
macotron.prefs.set("com.apple.finder", "CreateDesktop", false);
macotron.shell.run("/usr/bin/killall", ["Finder"]);

Domains

A domain is usually an app's bundle ID, such as com.apple.finder. "-g" and "NSGlobalDomain" mean the global domain, the same as for defaults. Reads and writes are for the current user on any host. A read of an app domain does not fall back to the global domain.

Values convert as in macotron.plist: a date is milliseconds since 1970 and data is a base64 string.

Reading

get(domain: string, key?: string): any | null

The value of key, or null when the key is not set. Without key, the whole domain as an object. A key that nobody has changed is usually absent, so treat null as the system default.

Booleans come back as they were written. A value that defaults write stored without a type, such as "false", comes back as a string, so compare loosely when another tool may have written the key.

Writing

set(domain: string, key: string, value: any): boolean

Writes value and saves the domain to disk. A null value deletes the key. Whole numbers are written as integers, other numbers as reals, and booleans as booleans. Returns false when the value is not a property list, for example an array that holds null, or when the write fails.

Most apps read their preferences when they start. An app that is already running can ignore a new value until it is restarted, and some system settings only take effect at the next login.

See also