Code Monkey home page Code Monkey logo

sketchplugin-remember's Introduction

SketchPlugin-Remember

A sample plugin that demonstrates how to save variables and access them later.


Note: This is only for Sketch Plugin developers and will be of no use to you otherwise as a standalone plugin!

Setup

Copy the pluginDefaults.js file in your plugin folder.

Usage

Import the pluginDefaults.js script using a relative path to the script file:

@import 'pluginDefaults.js'

Create a presets variable to hold default values. These values will be used the first time your plugin is run.

var presets = {
	myName: "Sketchy Jones",
	myAge: 25
}

Initialize defaults with a unique plugin domain value and the presets created above. Do this once at the beginning of your plugin script.

var userDefaults = initDefaults("com.yoursite.plugin-name", presets)

After initializing defaults, you can access default values via the userDefaults variable:

var myName = userDefaults.myName
var myAge = userDefaults.myAge

Set default values via the userDefaults variable, then commit the changes to persist values between plugin runs and app launches:

userDefaults.myName = "Carl Sagan"
userDefaults.myAge = 81

saveDefaults(userDefaults) // commit changes

You can also add new variables to save as defaults that were not included in presets:

userDefaults.myNationality = "American"
saveDefaults(userDefaults) // commit changes

If you wish to allow your users to 'Restore Original Settings', simply overwrite defaults with presets:

saveDefaults(presets)

Please check the example plugin to see all this in action.


Ping me @abynim if you find this useful or run into issues.

Enjoy!

sketchplugin-remember's People

Contributors

abynim avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.