-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathDynamic Reposition.sketchplugin
More file actions
183 lines (140 loc) · 4.48 KB
/
Dynamic Reposition.sketchplugin
File metadata and controls
183 lines (140 loc) · 4.48 KB
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
// Reposition all content to still have the same margin after the background is resized (cmd p)
// Change to use another default name;
var bgName = 'background';
var background;
var contentLayers = [];
// Puts all layers and groups except the background in an array.
function getContent() {
var group = selection[0].layers();
for( var i=0; i<[group count]; i++) {
if(group.array()[i].name() != bgName) {
contentLayers.push(group.array()[i]);
}
}
}
// fetches the positions and repostions the groups and layers.
function positionContent() {
var bgX = background.frame().x();
var bgY = background.frame().y();
var bgW = background.frame().width();
var bgH = background.frame().height();
var margin = [];
// Position all layers
for( var i=0; i<contentLayers.length; i++ ) {
var currentLayer = contentLayers[i];
var margin = getMargins([currentLayer name]);
var top = null;
var right = null;
var bottom = null;
var left = null;
var width = null;
var height = null;
var fontSize = null;
// get format offset
for( var j=0; j<margin.length; j+=2 ) {
switch(margin[j].toLowerCase()) {
case 't':
case 'top':
top = Number(margin[j+1]);
break;
case 'r':
case 'right':
right = Number(margin[j+1]);
break;
case 'b':
case 'bottom':
bottom = Number(margin[j+1]);
break;
case 'l':
case 'left':
left = Number(margin[j+1]);
break;
case 'w':
case 'width':
width = Number(margin[j+1]);
break;
case 'h':
case 'height':
height = Number(margin[j+1]);
break;
case 'fs':
case 'fontSize':
fontSize = Number(margin[j+1]);
break;
}
}
//check if font and has a size set
if (currentLayer.className() == "MSTextLayer" && fontSize != null){
currentLayer.setFontSize(fontSize);
currentLayer.frame().height = currentLayer.lineSpacing();
}
// set new width
if( right != null && left != null && width != null ) currentLayer.frame().width = bgW - (left + right);
// set new height
if( top != null && bottom != null && height != null ) currentLayer.frame().height = bgH - (top + bottom);
// resize layer if size is set
if( width != null )currentLayer.frame().width = width;
if( height != null) currentLayer.frame().height = height;
var currW = [currentLayer frame].width());
var currH = [currentLayer frame].height();
// position layer
if( bottom != null ) currentLayer.frame().y = (bgY + bgH) - (bottom + currH);
if( top != null ) currentLayer.frame().y = bgY + top;
if( right != null ) currentLayer.frame().x = (bgX + bgW) - (right + currW);
if( left != null ) currentLayer.frame().x = bgX + left;
// align middle
if( top == null && bottom == null ) currentLayer.frame().y = (bgY + bgH)*.5 - currH*.5;
// align center
if( right == null && left == null ) currentLayer.frame().x = ((bgX + bgW)*.5) - (currW*.5);
}
[doc showMessage:"The content has been repositioned."]
}
function getMargins(name) {
var possibleDividers = [' t:',' top:',' r:',' right:',' b:',' bottom:',' l:',' left:', ' width:', ' w:', ' fontSize:', ' fs:'];
var index = 0;
for(var i=0; i<possibleDividers.length; i++) {
if(name.toLowerCase().indexOf(possibleDividers[i]) != -1) {
index = name.indexOf(possibleDividers[i]);
break;
}
}
var margins = name.slice(index).replace(/\s+/g, '');
return margins.split(":");
}
function findBackgroundByName(){
var group = selection[0].layers();
for( var i=0; i<[group count]; i++) {
if(group.array()[i].name() == bgName) {
return group.array()[i];
}
}
}
function findBackgroundBySize(){
var group = selection[0].layers();
var maxWidth = 0;
var maxHeight = 0;
var tempBG;
for( var i=0; i<[group count]; i++) {
var w = group.array()[i].frame().width();
var h = group.array()[i].frame().height();
if( w > maxWidth && h > maxHeight ){
maxWidth = w;
maxHeight = h;
tempBG = group.array()[i];
}
}
return tempBG;
}
// Needs to have a group selected to start the script.
if( [selection count] > 0 ){
background = findBackgroundByName();
if( background == null ) { background = findBackgroundBySize(); }
getContent();
positionContent();
} else {
alert("Nothing selected","Please select a group");
}
function alert(title, text) {
var app = [NSApplication sharedApplication];
[app displayDialog:text withTitle:title]
}