npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

sf-sketch-rpc

v1.0.6

Published

RPC module based on `coscript` cli for running scripts in Sketch runtime on node.js platform

Downloads

20

Readme

Sketch RPC

Provides helpful APIs for communicating with SketchApp runtimes in various ways within help of COScript CLI. This module mostly aimed to solve problems related to execution of plugins remotely outside of the Sketch runtime, but it also contains a bunch of helpful APIs to fetch misc info from Sketch and controlling it.

Installation

Install using npm:

$ npm install --save sf-sketch-rpc

Usage

In order to start using RPC module, you have to define target application's name to tell COScript CLI which SketchApp bundle should be used to communicate with.

By default, when we create SketchRPC class instance without providing any arguments to the constructor - production version of Sketch will be used as a target:

import SketchRPC from 'sf-sketch-rpc'
const sketch = new SketchRPC();
// From this point all sketch.* calls will be addressed to `/Applications/Sketch.app` bundle.

sf-sketch-rpc module contains a handy dictionary with standard Sketch targets(app names):

export const SketchAppVariant = {
  Production: 'Sketch', // Default target
  Beta: 'Sketch Beta',
  Private: 'Sketch Private'
};

For example, here we create an instance of SketchRPC class with Sketch Beta target using this dictionary:

import SketchRPC, { SketchAppVariant } from 'sf-sketch-rpc'
const sketch = new SketchRPC(SketchAppVariant.Beta);
// All calls go to `/Application/Sketch Beta.app` bundle.

Alternatively, if you have multiple versions of Sketch with non-standard naming installed in Applications folder you, can use any target by providing application's name as a string value:

import SketchRPC from 'sf-sketch-rpc'
// RPC module will communicate with application available at `/Applications/Sketch Beta 48.app` path.
const sketch = new SketchRPC("Sketch Beta 48");

NOTE: SketchRPC could work with target apps located at /Applications folder only.

API

Running Plugin Commands

Running commands from custom bundle

Reload:

import SketchRPC from 'sf-sketch-rpc'

const sketch = new SketchRPC();
sketch.bootstrapPluginBundleInstance({
  bundlePath: 'path/to/plugin.sketchplugin', // A full path to the bundle outside of Sketch's `Plugins` folder
  commandIdentifier: 'main'
},function(err,res) {
  if(err) {
    console.log(err);
    return;
  }
});

Running commands from already instantiated plugin bundles

Existing bundles:

import SketchRPC from 'sf-sketch-rpc'

const sketch = new SketchRPC();
sketch.bootstrapPluginBundleInstance({
  bundleIdentifier: 'com.example.my-plugin', // Identifier of the plugin bundle
  commandIdentifier: 'main'
},function(err,res) {
  if(err) {
    console.log(err);
    return;
  }
});

Passing contextual data to command handlers

RPC side:

import SketchRPC from 'sf-sketch-rpc'

const sketch = new SketchRPC();
sketch.bootstrapPluginBundleInstance({
  bundleIdentifier: 'com.turbobabr.colorizer',
  commandIdentifier: 'colorizeSelectedLayers',
  // Any custom data to be passed to command's handler context
  context: {
    fillColor: '#f0dd76'
  }
},function(err,res) {
  if(err) {
    console.log(err);
    return;
  }
});

Plugin command's handler:

function onRunColorizeSelectedLayers(context) {
  function hexColorToMSColor(color) {
    return MSColor.alloc().initWithImmutableObject(MSImmutableColor.colorWithSVGString(color);
  }

  var selection = context.selection;
  var document = context.document;

  // `context` object passed from RPC is available from handler's context as `remoteContext` property
  var remoteContext = context.remoteContext;
  if(!remoteContext) {
    // This could happen if command was triggered by a user directly
    document.showMessage('No remote context... ¯\_(ツ)_/¯');
    return;
  }

  for(var i=0;i<selection.count();i++) {
    var layer = selection.objectAtIndex(i);
    layer.style().fill().color = hexColorToMSColor(remoteContext.fillColor);
  }
}

text

Bootstrapping/Reloading plugin bundles

import SketchRPC from 'sf-sketch-rpc'

const sketch = new SketchRPC();
sketch.bootstrapPluginBundleInstance({
  bundlePath: "/path/to/bundle.sketchplugin"
},(err,res) => {
  if(err) {
    console.log(err);
    return;
  }

  const { stderr, stdout } = res;
  console.log(stderr);
  console.log(stdout);
});

Bringing Sketch app to the foreground

import SketchRPC from 'sf-sketch-rpc'

const sketch = new SketchRPC();
sketch.makeFirstResponder((err) => {
  if(err) {
    console.log(err);
    return;
  }

  // At this point Sketch app should be activated and brought to the foreground
});

Getting various paths for a given app target

Module contains a bunch of helpful methods to get various Sketch paths:

import SketchRPC from 'sf-sketch-rpc'

const sketch = new SketchRPC();

// Target's app bundle path
console.log(sketch.appPath);
// > /Applications/Sketch.app

// Application Support folder path
console.log(sketch.appPath);
// > ~/Library/Application Support/com.bohemiancoding.sketch3

// Plugins folder path
console.log(sketch.appPath);
// > ~/Library/Application Support/com.bohemiancoding.sketch3/Plugins

// Templates folder path
console.log(sketch.appPath);
// > ~/Library/Application Support/com.bohemiancoding.sketch3/Templates

Fetching useful data from Info.plist stored in target's app bundle

Text Info.plist Text

In order to fetch entire Info.plist file as object, use plist property:

const sketch = new SketchRPC();
console.log(sketch.plist);

Truncated log output:

{ BuildMachineOSBuild: '16D32',
  CFBuildNumber: '109',
  CFBundleDevelopmentRegion: 'English',
  CFBundleDisplayName: 'Sketch',
  CFBundleDocumentTypes:
   [ { CFBundleTypeIconFile: 'doc.icns',
       CFBundleTypeName: 'com.bohemiancoding.sketch.drawing',
       CFBundleTypeRole: 'Viewer',
       LSHandlerRank: 'Owner',
       LSItemContentTypes: [Object],
       LSTypeIsPackage: true,
       NSDocumentClass: 'MSDocument' },
       ...
   ],
   ...
}

If you need to fetch only single property, there's a convenient plistProp(key) method for that:

const sketch = new SketchRPC();

console.log(sketch.plistProp('CFBundleDisplayName'));
// > Sketch

console.log(sketch.plistProp('CHApplicationSupportFolderName'));
// > com.bohemiancoding.sketch3

Also, there's a handy property for bundle version:

const sketch = new SketchRPC();
console.log(sketch.version());
// > 41.1

License

The MIT License (MIT)

Copyright (c) 2017 Andrey Shakhmin

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.