Giter Site home page Giter Site logo

doc22940 / sketchapi Goto Github PK

View Code? Open in Web Editor NEW

This project forked from sketch-hq/sketchapi

1.0 1.0 0.0 8.55 MB

Javascript API for working with Sketch

Home Page: https://developer.sketch.com/reference/api

License: MIT License

JavaScript 72.52% TypeScript 2.77% Ruby 0.76% HTML 3.14% CSS 20.81%

sketchapi's Introduction

Sketch API

This is a JavaScript API for Sketch. The intention is to make something which is:

  • idiomatic JavaScript
  • an easily understandable subset of the full internals of Sketch
  • fully supported by Bohemian between releases (ie. we try not to change it, unlike our internal API which we can and do change whenever we need to)
  • still allows you to drop down to our internal API when absolutely necessary

This API is a very core layer which interfaces with Sketch itself. It's intentionally simple, and we want to keep it that way. If you feel like adding some high-level code to it, it’s probably better to add it to a community-maintained library that can be used on top of the API, and keep it separate from the core API effort.

API layers

Comments and suggestions for this API are welcome - file an issue to discuss it or send them to [email protected].

Installation

The API comes bundled inside Sketch, so no installation is required. You access it by requiring the sketch package:

var sketch = require('sketch')

Usage

The full documentation is available on [https://developer.sketch.com/reference/api].

Here's a very simple example script:

// access the Sketch API
var sketch = require('sketch')

// get the current Document and Page
var document = sketch.getSelectedDocument()
var page = document.selectedPage

var Group = sketch.Group
var Shape = sketch.Shape
var Rectangle = sketch.Rectangle

// create a new Group belonging to the current Page
var group = new Group({
  parent: page,
  frame: new Rectangle(0, 0, 100, 100),
  name: 'Test',
  selected: true,
})
// create a new rectangle Shape belonging to the previously created Group
var rect = new Shape({
  parent: group,
  frame: new Rectangle(10, 10, 80, 80),
})

// get the current selection
var selection = document.selectedLayers

console.log(selection.isEmpty)
selection.forEach(function(item) {
  console.log(item.name)
})

// deselect all the layers
selection.clear()
console.log(selection.isEmpty)

// select the rectangle we created
rect.selected = true
console.log(selection.isEmpty)

// ask the user for a string
sketch.UI.getInputFromUser(
  'Test',
  {
    type: 'String',
    initialValue: 'default',
  },
  (err, outputString) => {
    if (err) {
      return
    }
    // store the string in the settings
    // it will be remembered even when Sketch closes
    sketch.Settings.setSettingForKey('setting-to-remember', outputString)
    console.log(sketch.Settings.settingForKey('setting-to-remember'))

    sketch.UI.getInputFromUser(
      'Test',
      {
        type: 'Selection',
        possibleValues: ['Sketch', 'Paper'],
      },
      (err, outputSelection) => {
        if (err) {
          return
        }
        sketch.UI.message('Hello mum!')
        sketch.UI.alert('Title', outputSelection)
      }
    )
  }
)

For more examples, we recommend checking out the examples section of the developer website.

Happy coding!

Development

The API is organized as a series of files defining JavaScript classes, and written with ES6 syntax.

However, the version that is embedded in Sketch is a single minified and transpiled library file called SketchAPI.js.

If you want to build this library file locally, you need to have Node.js installed, and then run the following in the project's root folder:

npm install

Once that's ready, you can run:

npm start

to compile the library. By default, it will be saved to ./build/SketchAPI.js.

For your convenience, you can use

npm run watch

and a script will watch for any change in the Source folder, and build the .js file when anything has changed.

To test your changes, you need to get Sketch to use the version of SketchAPI.js you just built, instead of the one embedded inside it.

To do this you can run:

defaults write com.bohemiancoding.sketch3 SketchAPILocation "/path/to/your/SketchAPI_repo/build"

Sketch will then load the external .js files instead of the bundled version.

Testing

To run the tests, you can use

npm run test
npm run test:watch

If you want to run the tests with a specific version of Sketch, you can use

SKETCH_PATH=/path/to/sketch.app npm run test

If you want to run the tests with the current regular version of Sketch, you can use

npm run test:no-variant
npm run test:no-variant:watch

Website

The website is hosted on Netlify at https://developer.sketch.com.

The website’s contents live in the docs folder, while code related to the website’s build process lives in the docs-website folder.

For further details read the docs-website README.

Acknowledgements

We would like to thank:

sketchapi's People

Contributors

acf avatar alexgusew avatar bomberstudios avatar cameron avatar christianklotz avatar dependabot[bot] avatar freddiewrites avatar griffin-stewie avatar hellovai avatar jedrichards avatar jiaxianhua avatar kevingutowski avatar kgn avatar mathieudutour avatar matt-curtis avatar maxdavid avatar mikeabdullah avatar mkeiser avatar modkaffes avatar paulozoom avatar pieteromvlee avatar piperchester avatar pravdomil avatar randomsequence avatar robintindale avatar samdeane avatar sheffieldkevin avatar thierryc avatar wcbyrne avatar whakkee avatar

Stargazers

 avatar

Watchers

 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.