Prev Up Next
Sketch's API User Scripting

Common Tasks

Unless otherwise noted, all functions and classes mentioned here are directly accessible from the Sketch package. E.g. the CreateRGBColor function is accessible either with:
import Sketch

blue = Sketch.CreateRGBColor(0, 0, 1)

or

from Sketch import CreateRGBColor

blue = CreateRGBColor(0, 0, 1)

Creating Objects

Creating new objects for a drawing, usually takes three steps:

  1. Create the object. This is described in this section.
  2. Set graphics properties like fill color or line width. This is described below.
  3. Insert the object into the document

Primitives

Rectangles

Rectangle(trafo)

This function creates a rectangle object described by trafo. trafo is an affine transformation (represented by a a transformation object that transforms the unit square into the desired rectangle.

Transformation objects are created by the functions Trafo, Scale, Rotation and Translation.

In the most common case where you want to create a rectangle with a specific width and height at a specific position (x, y) (the position of the lower left corner of the rectangle) and edges parallel to the edges of the page, you could create the rectangle like this:

Rectangle(Trafo(width, 0, 0, height, x, y))

or like this

Rectangle(Translation(x, y)(Scale(width, height)))

Lines and Curves

All line and curve-objects in Sketch are instances of the PolyBezier class. Each PolyBezier object consists of one or more paths with each path consisting of one or more line or bézier segments.

To create a PolyBezier object you have to first create the paths and then the PolyBezier instance. A path is created by starting with an empty path returned by the CreatePath function and then appending line- and curve-segments to construct the path. How paths are constructed is covered in detail in the corresponding section in the developer's guide.

Once you have constructed your paths, you create the PolyBezier instance with:

PolyBezier(path_tuple)

The argument path_tuple must be a tuple of paths. If you have just one path path, you can use the expression (path,).

For an example, see the sample script create_star.py.

Text

Sketch's text capabilities are still very simple. A text object consists