All Projects → sketch-hq → Sketchapi

sketch-hq / Sketchapi

Licence: mit
The JavaScript plugin library embedded in Sketch

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to Sketchapi

Sketch swatches
A swatches plugin for Sketch.
Stars: ✭ 446 (-43.11%)
Mutual labels:  sketch-plugin, sketch
Html Sketchapp Cli
Quickly generate Sketch libraries from HTML documents and living style guides, powered by html-sketchapp
Stars: ✭ 631 (-19.52%)
Mutual labels:  sketch-plugin, sketch
Sketch Plugin Manager
Keeping your Sketch plugins up to date.
Stars: ✭ 335 (-57.27%)
Mutual labels:  sketch-plugin, sketch
Keys For Sketch
Advanced shortcut manager for Sketch app.
Stars: ✭ 281 (-64.16%)
Mutual labels:  sketch-plugin, sketch
Sync.sketchplugin
Keep your design team in sync!
Stars: ✭ 357 (-54.46%)
Mutual labels:  sketch-plugin, sketch
Sketch Lint
Check the compliance of your design guidelines within seconds
Stars: ✭ 291 (-62.88%)
Mutual labels:  sketch-plugin, sketch
Chromatic Sketch
Sketch plugin for creating good-looking and perceptually uniform gradients and color scales.
Stars: ✭ 445 (-43.24%)
Mutual labels:  sketch-plugin, sketch
Sketch Gifme Plugin
Embed gifs and videos in your Sketch files!
Stars: ✭ 274 (-65.05%)
Mutual labels:  sketch-plugin, sketch
Sketch Styles Generator
Generate hundreds of Sketch Shared Styles in a matter of seconds.
Stars: ✭ 537 (-31.51%)
Mutual labels:  sketch-plugin, sketch
Chain
Dynamic color relations in Sketch.
Stars: ✭ 346 (-55.87%)
Mutual labels:  sketch-plugin, sketch
Sketch Sf Ui Font Fixer
A Sketch plugin that adjusts the character spacing on text layers using iOS 9's SF UI Text/SF UI Display fonts to what it would be when used in an iOS app.
Stars: ✭ 492 (-37.24%)
Mutual labels:  sketch-plugin, sketch
Sketch Mate
These plugins will make you best friends with Sketch.
Stars: ✭ 508 (-35.2%)
Mutual labels:  sketch-plugin, sketch
Css Buddy
A Sketch 3 plugin that allows you to use CSS on layers.
Stars: ✭ 279 (-64.41%)
Mutual labels:  sketch-plugin, sketch
Sketch Artboardtricks
A variety of artboard-related utilities for Sketch, primarily rearranging them into a grid, numbering them, etc.
Stars: ✭ 316 (-59.69%)
Mutual labels:  sketch-plugin, sketch
Google Sheets Content Sync Sketch Plugin
Sync content within a Google Sheets document, to text layers — based on their names. Edit and collaborate on your content in Google Sheets, then sync in back to your sketch files.
Stars: ✭ 277 (-64.67%)
Mutual labels:  sketch-plugin, sketch
Plugin Directory
Official Sketch Plugin directory
Stars: ✭ 3,412 (+335.2%)
Mutual labels:  sketch-plugin, sketch
Sketchcontentsync
Sync sketch files with google docs.
Stars: ✭ 270 (-65.56%)
Mutual labels:  sketch-plugin, sketch
Sketch Connection Flow Arrows
Plugin for generating easy to use connection flow arrows in Sketch
Stars: ✭ 275 (-64.92%)
Mutual labels:  sketch-plugin, sketch
Sketch Image Compressor
A Plugin for Sketch that compresses your bitmap assets, to keep filesize to a minimum.
Stars: ✭ 338 (-56.89%)
Mutual labels:  sketch-plugin, sketch
Swatches
Make colors with Sketch
Stars: ✭ 396 (-49.49%)
Mutual labels:  sketch-plugin, sketch

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 Sketch 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].

Usage

The full documentation is available on developer.sketch.com/reference/api.

Here's a very simple example script:

// access the Sketch API - comes bundled inside Sketch, so no "installation" is required
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

This section of the readme is related to developing SketchAPI locally. If you're just interested in using SketchAPI to write Sketch Plugins you can stop reading here.

You'll need the following tools available on your system to work on this repository:

  • Node 10 or greater
  • Visual Studio Code (recommended)

Overview

SketchAPI is written in JavaScript, as a collection of files in the ./Source folder.

Webpack is used to bundle the files, and we include this in each release of Sketch. However you can build, run and test SketchAPI locally too - continue reading to find out how.

Scripts

The following npm scripts are available. Carry on reading below for more in-depth guides.

Script Description
npm run build Build SketchAPI into the build folder
npm run test Run the integreation tests using skpm
npm run lint Lint the source code
npm run format-check Check the format with Prettier
npm run api-location:write Tell Sketch to use your local SketchAPI
npm run api-location:delete Undo npm run api-location:write

Build and run

Following these steps will allow you to build the source files, and inject the changes into your local copy of Sketch to see the results.

Any plugins you have installed or code you invoke in Sketch's Plugins > Run Script dialogue box will run against your changed version of SketchAPI.

  1. Checkout this repository.
  2. Make your copy of Sketch use your local version of SketchAPI, rather than the one it has built-in.
    npm run api-location:write
    
  3. Ensure you've installed the dependencies with npm, and then build SketchAPI.
    npm install
    npm run build
    
  4. Start Sketch and your changes will be picked up.

⚠️ If you re-build SketchAPI by running npm run build again while Sketch is open you won't see your changes automatically reflected. You'll need to restart Sketch for this to happen.

⚠️ Once you've finished working on SketchAPI don't forget to stop Sketch using your customised version, to do this run:
npm run api-location:delete.

Testing

The *.test.js files in this repository are integration tests that run in Sketch's plugin environment, using the skpm test runner.

To run these tests using your current version of the Sketch as the host environment invoke:

npm run test

Alternatively if you want to run the tests with a specific app binary run:

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

ℹ️ There is no need to re-build SketchAPI between test runs or use defaults write to set the API location; the test runner handles re-compiling test and source files and injecting them into Sketch.

Documentation

The API documentation is part of the developer.sketch.com repository.

Make sure to update the API and the action reference accordingly when you make changes to the API.

Acknowledgements

We would like to thank:

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].