|
If you want to write scripts for Sketch you have to know how to access the objects in the document and how to manipulate them. This section gives a brief introduction to the structure of Sketch's modules and objects from script author's point of view.
A more detailed description of Sketch's internals can be found in the Developer's Guide.
Sketch uses a naming convention for this. Methods with capitalized names, e.g. "SetRadius", are public methods and Methods with lowercase name, e.g. "set_radius", are protected.
You only have to deal with undo info if you modify an object that is part of a document. These objects include instances of classes derived from GraphicsObject (class hierarchy) and the objects that define the properties of a graphics object, like patterns.
All methods that modify such an object return an undo info object. What this object looks like is irrelevant here, all you need to know for now is that you have to pass it immediately on to the document method AddUndo.
A typical example:
context.document.AddUndo(object.Translate(offset)) |
The Translate method translates an object by offset, a point object that stands for a 2D-vector.
There are some exceptions to the rule that methods that modify the document return undo info.
Firstly, the document methods that modify the selection, that is, that modify which objects are selected not the selected objects themselves, don't return undo info because changing the selection is not considered changing the document.
Secondly, public document methods that modify the selected objects themselves already take care of undo info themselves. The reason for this is that they are called directly as a result of a menu command, button or key-press event.
For more information about undo information in Sketch, have a look at the corresponding section in the Developer's Guide.
The Developer's Guide covers Sketch's internals in more detail. Although it's incomplete it already contains a lot of information about the structure of Sketch's sources, the class hierarchy and some of the base classes, coordinate systems, plugins and more.
And, of course: Use The Source, Luke!
|
|
|