Skip to content

Instantly share code, notes, and snippets.

@uhunkler
Created May 21, 2014 15:15
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save uhunkler/03e5724081961216b5cf to your computer and use it in GitHub Desktop.
Save uhunkler/03e5724081961216b5cf to your computer and use it in GitHub Desktop.
A basic table creation plugin.
/*global JSTalk:false, NSApplication:false, NSAlert:false, print:true */
// Set the basic variables for an externally run script
// which are set by default when the script is run
// as a plugin from within Sketch. "doc" is defined when the script
// is called from within Sketch.
if (doc === undefined) {
var doc, selection;
}
var sketch = null;
/**
* Sketch connector - is helpful when the script is run
* form an external editior and not from within Sketch,
* executed immediatly.
* Set the sketch object and detect if the script is run
* from within Sketch or external.
*/
(function () {
sketch = {
app: null,
extern: false,
init: function () {
if (doc === undefined) {
this.extern = true;
this.app = JSTalk.application("Sketch3Beta");
doc = this.app.orderedDocuments()[0];
selection = doc.selectedLayers();
log = print;
} else {
this.app = NSApplication.sharedApplication();
}
},
activate: function () {
this.app.activate();
}
};
sketch.init();
}());
// sketch.activate();
/**
* Set a Sketch color to the given color values in a JavaScript object
* Example set_color(SketchColor, {r: 1, g: 1, b: 1, a: 1});
*
* @param {NSColor} obj The Sketch color
* @param {object} c The JavaScript object with r, g, b, a values
*/
var set_color = function (obj, c) {
obj.setRed(c.r);
obj.setGreen(c.g);
obj.setBlue(c.b);
obj.setAlpha(c.a);
};
/**
* Create a new Sketch layer
*
* @param {object} group MSLayerGroup, MSArtboardGroup or MSPage
* @param {string} type "text", "rectangle", "group"
* @param {string} name new layer name
* @param {int} x x position
* @param {int} y y position
* @param {int} w width
* @param {int} h height
* @return {mixed} return layer object or null
*/
var makeLayer = function (group, type, name, x, y, w, h) {
var groupType = "" + group.className();
if (!(groupType === "MSLayerGroup" || groupType === "MSArtboardGroup" ||
groupType === "MSPage")) {
return null;
}
type = type || 'text';
name = name || type + "-layer";
x = x || 0;
y = y || 0;
w = w || 100;
h = h || 20;
var _l = group.addLayerOfType(type);
_l.setName(name);
_l.absoluteRect().setRulerX(x);
_l.absoluteRect().setRulerY(y);
_l.frame().width = w;
_l.frame().height = h;
return _l;
};
/**
* Create a new box with a group containing a rectangle for bg color and border
* and a text layer.
*
* @param {object} group MSLayerGroup, MSArtboardGroup or MSPage
* @param {string} name new layer name
* @param {int} x x position
* @param {int} y y position
* @param {int} w width
* @param {int} h height
* @param {int} pad the padding = indent for the text layer
* @return {mixed} return layer object or null
*/
var makeBox = function (group, name, x, y, w, h, pad) {
var groupType = "" + group.className();
if (!(groupType === "MSLayerGroup" || groupType === "MSArtboardGroup" ||
groupType === "MSPage")) {
return null;
}
name = name || "n";
x = x || 0;
y = y || 0;
w = w || 100;
h = h || 20;
pad = pad || 5;
var cell = null,
rect = null,
rectname = "",
textele = null,
textname = "",
color = null,
fillColor = null,
nc = {r: 1, g: 1, b: 1, a: 1};
cell = makeLayer(group, "group", name, x, y, w, h);
rectname = name.replace(/^\w*/, "rect");
rect = makeLayer(cell, "rectangle", rectname, x, y, w, h);
color = rect.style().fills().addNewStylePart();
fillColor = color.color();
// set_color(fillColor, nc);
fillColor.setNSColor(NSColor.whiteColor());
textname = name.replace(/^\w*/, "text");
textele = makeLayer(cell, "text", textname, x + pad, y + pad,
w - pad * 2, h - pad * 2);
textele.adjustFrameToFit();
cell.resizeRoot();
return cell;
};
/**
* Create a new row of boxes with the given amount of boxes
* and given box width and height.
*
* @param {object} group MSLayerGroup, MSArtboardGroup or MSPage
* @param {object} def row definition
* @return {mixed} return layer object or null
*/
var makeBoxRow = function (group, def) {
var groupType = "" + group.className();
if (!(groupType === "MSLayerGroup" || groupType === "MSArtboardGroup" ||
groupType === "MSPage")) {
return null;
}
var rowname = def.rowname || "tr",
cellname = def.cellname || "td",
rowno = def.rowno || 0,
x = def.x || 0,
y = def.y || 0,
w = def.w || [100],
h = def.h || 20,
pad = def.pad || 5,
rowwidth = 0,
name = "",
cell = null,
row = null,
i;
name = rowname.replace(/\{\{.\}\}/g, rowno);
for (i = 0; i < w.length; i++) {
rowwidth += w[i];
}
row = makeLayer(group, "group", name, x, y, rowwidth, h);
for (i = 0; i < w.length; i++) {
name = cellname.replace(/\{\{c\}\}/g, i);
cell = makeBox(row, name, x, y, w[i], h, pad);
x += w[i];
}
row.resizeRoot();
return row;
};
/**
* The main function, automatically executed
*/
(function main () {
var page = doc.currentPage();
var table, cell,
row = 0,
x = 0,
y = 200,
w = 150,
h = 30,
tableName = "table.one";
var page = doc.currentPage();
table = makeLayer(page, "group", tableName, x, y);
var rowDef = {
numberOfRows: 2,
rowname: "tr.r{{r}}",
cellname: "td.c{{c}}",
rowno: 0,
x: x,
y: y,
pad: 5,
h: h,
// w: [100, 200, 200, 100, 200, 200, 100]
w: [100, 200, 200]
};
var i,
row;
for (i = 0; i < rowDef.numberOfRows; i++) {
row = makeBoxRow(table, rowDef);
rowDef.rowno += 1;
rowDef.y += rowDef.h;
}
return 0;
}());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment