Preference Component
The Preference component allows scripts to store and retrieve persistent data. This can be used to save user preferences, configuration settings, or state information between script executions.
Usage
When implementing your script's onStart, execute, or onFinish methods, you can access the Preference component
through the provided ComponentProvider.
fun execute(provider: ComponentProvider) {
val preference = provider.preference()
// Store values
preference.putString("apiKey", "abc123xyz")
preference.putBoolean("notificationsEnabled", true)
preference.putInt("retryCount", 3)
// Retrieve values with default fallbacks
val apiKey = preference.getString("apiKey", "")
val notificationsEnabled = preference.getBoolean("notificationsEnabled", false)
val retryCount = preference.getInt("retryCount", 5)
// Check if a preference exists
if (preference.contains("lastRunTimestamp")) {
val lastRun = preference.getLong("lastRunTimestamp", 0)
provider.logger().info("Last run: $lastRun")
}
// Update the last run timestamp
preference.putLong("lastRunTimestamp", System.currentTimeMillis())
}
Best Practices
- Use clear and descriptive keys for your preferences
- Always provide sensible default values when retrieving preferences
- Store only small amounts of data - preferences are not designed for large datasets
- Don't store sensitive information like passwords in plain text