All

How to Create and Customize Plugins in Sketch

Edited 2 months ago by ExtremeHow Editorial Team

SketchPluginsCustomizationDevelopmentExtensionsToolsWorkflowEnhancementsScriptingThird-Party

How to Create and Customize Plugins in Sketch

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.

Understanding the basics

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.

Setting up your environment

Before you start creating plugins, you need to set up your development environment. Here are the steps to get started:

  1. Install Sketch: Make sure you have the latest version of Sketch installed on your computer. Sketch is currently available for macOS.
  2. Set up a text editor: A good text editor is essential for writing code. Some popular options are Visual Studio Code, Sublime Text, and Atom. Choose the one you're most comfortable with.
  3. Familiarize yourself with JavaScript: Sketch plugins are written in JavaScript, so a basic understanding of JavaScript is essential. Learning the basics of JavaScript syntax, variables, functions, and objects is a good start.

Creating a simple Sketch plugin

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.

Step 1: Create your plugin folder

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.

Step 2: Create the manifest file

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.

Step 3: Write the plugin script

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.

Step 4: Install and test your plugin

To install your plugin, follow these steps:

  1. Open the Sketch.
  2. Go to Plugins > Manage Plugins...
  3. Click the gear icon and select Show Plugins Folder.
  4. Copy your plugin folder into the plugins folder shown.
  5. Restart the Sketch or go to Plugins > Run Scripts > Reload Plugins.
  6. Your plugin should now appear in the Plugins menu!

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.

Customizing your plugin

Now that you've created a simple plugin, let's look at how you can customize it further.

Add user settings

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.

Using a third-party library

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:

    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.

Testing and debugging

As with any other software development, testing and debugging is very important in plugin development as well. Here are some tips:

Publishing your plugin

Once you are satisfied with your plugin, you may want to share it with the community. Here is how you can publish your plugin:

  1. Prepare documentation: Create a README.md file with instructions on how to install and use your plugin. Provide code and explanations for any specific settings.
  2. Choose a license for your code: Choose an appropriate license for your code. Many developers use the MIT License or the Apache License.
  3. Host your plugin: Use a platform like GitHub to host your plugin's source code. This makes it accessible to others and gives them a chance to contribute.
  4. Submit to the Sketch plugin directory: You can submit your plugin to Sketch's plugin directory for more visibility. Make sure you comply with the directory guidelines.

Conclusion

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


Comments