Edited 2 months ago by ExtremeHow Editorial Team
SketchPluginsCustomizationDevelopmentExtensionsToolsWorkflowEnhancementsScriptingThird-Party
This content is available in 7 different language
Sketch is a popular design tool that allows designers to create beautiful user interface designs. One of Sketch's key strengths is its extensibility through plugins. Plugins can help automate tasks, add new features, and better adapt Sketch's functionality to your design workflow. This detailed guide will introduce you to the process of creating plugins in Sketch and customizing them.
Before diving into plugin creation, it's important to understand what plugins are and why they're useful. A plugin in Sketch is essentially a script that interacts with Sketch's API to perform certain tasks. This can range from simple tasks like changing colors to complex tasks like creating a complete UI component.
The Sketch API allows developers to interact with documents, pages, layers, and more, making it possible to build powerful tools. These tools help streamline design processes, saving both time and effort.
Before you start creating plugins, you need to set up your development environment. Here are the steps to get started:
Now let's move on to creating a simple Sketch plugin. This example will show you how to create a plugin that adds a red rectangle to your Sketch document.
Start by creating a new folder with a meaningful name for your plugin, such as “RedRectanglePlugin”. This folder will contain all the files your plugin needs.
manifest.json
file tells Sketch about your plugin. It contains information such as the plugin's name, version, and the script it will execute.
{
"name": "Red Rectangle Plugin",
"identifier": "com.example.redrectangleplugin",
"version": "1.0",
"description": "A simple Sketch plugin to add a red rectangle.",
"author": "Your Name",
"commands": [
{
"name": "Add Red Rectangle",
"identifier": "addRedRectangle",
"script": "./addRedRectangle.js"
}
]
}
Place manifest.json
file inside your plugins folder.
Create a new JavaScript file named addRedRectangle.js
inside your plugin folder. This script will add a red rectangle to the existing page in Sketch. Here's a simple example:
function onAddRedRectangle(context) {
var sketch = require('sketch/dom');
var document = sketch.getSelectedDocument();
var page = document.selectedPage;
// Create a new rectangle
var rectangle = new sketch.Rectangle({
parent: page,
frame: { x: 0, y: 0, width: 100, height: 100 },
style: { fills: [{ color: '#FF0000', fillType: 'Color' }] }
});
page.layers.push(rectangle);
}
module.exports = onAddRedRectangle;
Save this script in your plugins folder.
To install your plugin, follow these steps:
Test: Open any document in Sketch, go to Plugins > Red Rectangle Plugin > Add Red Rectangle. You should see a red rectangle appear on your canvas.
Now that you've created a simple plugin, let's look at how you can customize it further.
Sometimes, you may want to give users the ability to configure how a plugin works. For this, you can create a UI for settings using Sketch's built-in tools.
function onAddRectangleWithSettings(context) {
var sketch = require('sketch/dom');
var UI = require('sketch/ui');
UI.getInputFromUser(
"Rectangle Size",
{ type: UI.INPUT_TYPE.string, initialValue: "100" },
(err, value) => {
if (err) { return; }
var document = sketch.getSelectedDocument();
var page = document.selectedPage;
var size = parseInt(value, 10);
var rectangle = new sketch.Rectangle({
parent: page,
frame: { x: 0, y: 0, width: size, height: size },
style: { fills: [{ color: '#FF0000', fillType: 'Color' }] }
});
page.layers.push(rectangle);
}
);
}
module.exports = onAddRectangleWithSettings;
In this example, when you run the plugin, it will ask the user for the preferred size of the rectangle.
Sometimes the default Sketch APIs may not be enough to achieve your goal. You may want to use third-party JavaScript libraries to extend the functionality.
Here's how you can use a third-party library:
require()
to include and use it in your script.
var _ = require('./lodash.min.js');
function main(context) {
var numbers = [1, 2, 3, 4, 5];
var doubled = _.map(numbers, function(n) {
return n * 2;
});
console.log(doubled);
}
module.exports = main;
In this example, we are using the Lodash library to double an array of numbers.
As with any other software development, testing and debugging is very important in plugin development as well. Here are some tips:
console.log()
to print variables and output to the console, which can be viewed in the Sketch using Plugins > Run Script > Show Tools > Show Console.Once you are satisfied with your plugin, you may want to share it with the community. Here is how you can publish your plugin:
README.md
file with instructions on how to install and use your plugin. Provide code and explanations for any specific settings.Creating and customizing plugins in Sketch can greatly extend its functionality and tailor it to your specific needs. By following this guide, you've learned how to set up your environment, create a basic plugin, customize it, use third-party libraries, and even publish your creations. With practice and creativity, you can make significant contributions to the Sketch plugin ecosystem, making design processes more efficient and innovative. Happy coding!
If you find anything wrong with the article content, you can