天天看点

Sketch插件开发入门

<a href="http://developer.sketchapp.com/introduction/plugin-bundles/" target="_blank">http://developer.sketchapp.com/introduction/plugin-bundles/</a>

sketch 插件就是脚本的集合。每个脚本定义了一个或者多个命令。这些命令可以拓展sketch的功能。sketch插件以 <code>.sketchplugin</code>结尾,其实就是个文件夹,把后缀删除后可以直接打开。

这个json文件声明插件的元数据.sketch会读取这个配置文件的信息,得到command的名字和command对应函数实现.

这文件里面最重要的配置项是commands和menu:

声明一组command的信息。每个command以字典形式存在。

script : 实现命令功能的函数所在的脚本

handler: 函数名,该函数实现命令的功能。sketch在调用该函数时,会传入一个context参数,context是个字典,里面包含了很多信息,下文会讲到。如果你的没有指定handler,sketch会默认是script里的onrun函数

shortcut:命令的快捷键

name:会显示在sketch的plugin 的菜单里

identifier:唯一标识,建议用com.xxxx.xxx格式

sketch加载插件的时候,会根据它指定的信息,顺序地在菜单栏中显示命令名。

<a href="http://developer.sketchapp.com/introduction/plugin-scripts/#script-context" target="_blank">入门</a>

this is a bridge that lets you write javascript scripts that can call native objective-c/cocoa.using it, you can write the logic of your plugin in javascript, but can call the actual classes and methods that implement sketch itself when you want to make it do something.

在使用中用js调objective-c的方法时,

方法调用 用 ‘.’ 语法

参数都放在‘()’里头

oc中被参数分割开的函数名,用 ‘ _ ’连接.

返回值,js统一用var类型 ,js是弱类型

比如: msplugin的接口<code>valueforkey:onlayer:</code>

oc:

cocoascript:

当我们输入插件定制的命令时,sketch会去寻找改命令对应的实现函数, 并传入context变量。context 包含以下变量:

scriptpath: <code>nsstring</code> 当前执行脚本的绝对路径

scripturl: 当前执行脚本的绝对路径,跟 scriptpath不同的是它是个 nsurl 对象

<code>msplugincommand</code>对象,代表插件中单个命令,可以用来访问当前执行的命令,也可以用来在图层中存储自己的扩展元数据。

在图层中存取的拓展数据,也可以在不同插件中共享。

selection

当前选择的document下的一组被选择的mslayers对象。

如果当前只选了一个图层,可以这样获取:

主要依赖打log.用console.app过滤日志查看,过滤标签可以是你写的插件的名字,也可以用“sketch”过滤,但是这样会得到所有命令的执行日志。

或者在代码中用视图控件显示,好处就是不用不用开第三方app.比如:

<code>alert</code>, <code>[doc showmessage]</code> ,<code>showmessage("message")</code>

有人dump出来的sketch的头文件。信息量非常丰富哈哈~

<a href="https://github.com/abynim/sketch-headers" target="_blank">https://github.com/abynim/sketch-headers</a>

以这个设计图为例

<a href="https://github.com/bouchenoiremarc/sketch-constraints/blob/master/example.sketch" target="_blank">https://github.com/bouchenoiremarc/sketch-constraints/blob/master/example.sketch</a>

<code>组</code>:

msartboardgroup:画板

msshapegroup:组里的图层都是shape ,比如wifi

mslayergroup:比如局域网信号

<code>图层基类</code> :

mslayer

<a href="http://developer.sketchapp.com/reference/mslayer/" target="_blank">http://developer.sketchapp.com/reference/mslayer/</a>

<a href="https://github.com/abynim/sketch-headers/blob/master/headers/mslayer.h" target="_blank">https://github.com/abynim/sketch-headers/blob/master/headers/mslayer.h</a>

<code>文本层</code>:

mstextlayer

<a href="https://github.com/abynim/sketch-headers/blob/master/headers/mstextlayer.h" target="_blank">https://github.com/abynim/sketch-headers/blob/master/headers/mstextlayer.h</a>

<code>图形层</code>:

<a href="https://github.com/abynim/sketch-headers/blob/master/headers/msshapepathlayer.h" target="_blank">https://github.com/abynim/sketch-headers/blob/master/headers/msshapepathlayer.h</a>

<code>msovalshape</code>:

<code>msbitmaplayer</code>:

<a href="http://developer.sketchapp.com/reference/msbitmaplayer/" target="_blank">http://developer.sketchapp.com/reference/msbitmaplayer/</a>

<a href="https://github.com/abynim/sketch-headers/blob/master/headers/msbitmaplayer.h" target="_blank">https://github.com/abynim/sketch-headers/blob/master/headers/msbitmaplayer.h</a>

1.1 获取layer的parentgroup,parent对象可能是mspage、msartboardgroup或mslayergroup

1.2 获得自己的frame,手动计算缩放比例,控制。顺便说一句,sketch有个残暴的接口:

利用sketch已经实现的一些响应接口,插件少写代码,得到的效果更好。

在sketch-header中可以看到到,sketch可以响应的 action,比如:

这些头文件,都继承了msbasealignlayersaction。msbasealignlayersaction继承msbaseaction。msbaseaction继承了nsresponder 0 0呵呵哈。

继续阅读