Go module wrapper for the CFPreferences API's in macOS.
- Read and write macOS preferences using native Go types
- Automatic type conversion between Go types and CoreFoundation types
- Support for all common data types: strings, numbers, booleans, dates, arrays, dictionaries, and binary data
- Use JSON Pointer paths to access nested structures
go get github.com/jheddings/go-cfprefsimport "github.com/jheddings/go-cfprefs"
// Read a preference value
value, err := cfprefs.Get("com.apple.finder", "ShowPathbar")
if err != nil {
log.Fatal(err)
}
// Read a nested value using JSON Pointer path
value, err = cfprefs.Get("com.example.app", "config/server/port")
if err != nil {
log.Fatal(err)
}The Set function accepts any native Go type and automatically converts it to the appropriate CoreFoundation type:
import "github.com/jheddings/go-cfprefs"
// Write a string
err := cfprefs.Set("com.example.app", "username", "john_doe")
// Write a number
err = cfprefs.Set("com.example.app", "count", 42)
// Write a boolean
err = cfprefs.Set("com.example.app", "enabled", true)
// Write a float
err = cfprefs.Set("com.example.app", "pi", 3.14159)
// Write a date
err = cfprefs.Set("com.example.app", "lastAccess", time.Now())
// Write an array
err = cfprefs.Set("com.example.app", "items", []any{"apple", "banana", "cherry"})
// Write a dictionary
err = cfprefs.Set("com.example.app", "config", map[string]any{
"theme": "dark",
"fontSize": 14,
"autoSave": true,
})
// Write a nested value using JSON Pointer path
err = cfprefs.Set("com.example.app", "config/server/port", 8080)// Delete a top-level key
err := cfprefs.Delete("com.example.app", "username")
// Delete a nested value using JSON Pointer path
err = cfprefs.Delete("com.example.app", "config/server/port")// Check for a top-level key
exists, err := cfprefs.Exists("com.example.app", "username")
// Check for a nested value using JSON Pointer path
exists, err = cfprefs.Exists("com.example.app", "config/server/port")Preference keys can be specified as simple names or as JSON Pointer paths to access nested values. Use / to separate object keys and array indices.
settings— top-level keyuser/name— nested object fielditems/0— first element of an arrayconfig/database/host— deeply nested field
For more details, see the JSON Pointer RFC.
There is a basic CLI that acts as a demonstration of this module, as well as used for testing.
This project is licensed under the terms of the MIT license. See LICENSE for details.