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();
sf-sketch-rpc
module contains a handy dictionary with standard Sketch targets(app names):
export const SketchAppVariant = {
Production: 'Sketch',
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);
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'
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',
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',
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',
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;
var remoteContext = context.remoteContext;
if(!remoteContext) {
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;
}
});
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();
console.log(sketch.appPath);
console.log(sketch.appPath);
console.log(sketch.appPath);
console.log(sketch.appPath);
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'));
console.log(sketch.plistProp('CHApplicationSupportFolderName'));
Also, there's a handy property for bundle version:
const sketch = new SketchRPC();
console.log(sketch.version());
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.