-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathIcon Stamper.sketchplugin
69 lines (54 loc) · 1.92 KB
/
Icon Stamper.sketchplugin
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
var graphics = getSelection(),
sourceArtboardSize = 1024,
percentageX = graphics.frame().x() / sourceArtboardSize,
percentageY = graphics.frame().y() / sourceArtboardSize;
if (graphics != null) {
var groupName = graphics.name();
NSApp.sendAction_to_from('copy:', null, context.document);
var fastLoop = context.document.artboards().objectEnumerator();
while (board = fastLoop.nextObject()) {
var f = board.frame();
var size = f.width();
var offset = 100;
if (size < sourceArtboardSize) {
// This is a kluge.
// If you paste layers in a artboard and those layers are larger than
// the board and large enough to be "over" another artboard, the layers
// will be pasted to the wrong artboard. I know, weird.
// Because of that we'll move the artboard a safe distance on the x axis
// and then move it back to it's original place.
f.addX(offset);
board.select_byExpandingSelection(true, false);
NSApp.sendAction_to_from('paste:', null, context.document);
var l = board.layers();
var newGroup = l.objectAtIndex(l.count() - 1);
newGroup.multiplyBy(board.frame().width() / sourceArtboardSize);
newGroup.frame().x = size * percentageX;
newGroup.frame().y = size * percentageY;
newGroup.setName(groupName);
// Move the artboard back.
f.addX(offset);
}
}
// Center the canvas
context.document.currentView().centerRect(context.selection[0].absoluteRect().rect());
}
function getSelection () {
var el = null;
var s = context.selection;
function selectionError () {
NSApplication.sharedApplication().displayDialog_withTitle("You'll need to select a group that contains your icon graphics", "Invalid Selection");
}
if (s.count() == 0) {
selectionError();
}
else {
if (s[0].className() == 'MSLayerGroup') {
el = s[0];
}
else {
selectionError();
}
}
return el;
}