-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathresize.js
213 lines (165 loc) · 4.38 KB
/
resize.js
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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
var HORIZONTAL = 'horizontal';
var VERTICAL = 'vertical';
var BOTH = 'both';
var YES_BUTTON = '1000';
var dir;
var padding;
//var baseLayer;
var layers = [];
function resize(direction, difference) {
dir = direction;
padding = difference || 0;
var base;
if( selection.count() > 0 ) {
if( selection.count() == 1) {
base = doc.currentPage().artboards().firstObject();
}
// Is there any layers with constrained proportions and want the user continue if it does?
if( checkForConstrainProportions() ) {
switch(dir){
case HORIZONTAL:
resizeHorizontal(base);
notify('The content have been resized horizontally');
break;
case VERTICAL:
resizeVertical(base);
notify('The content have been resized vertically');
break;
case BOTH:
reziseBoth(base);
notify('The content have been resized');
break;
}
}
}
else if( selection.count() == 0 ) {
alert('Not enough selected','You need to select at least one layer.');
}
}
function resizeHorizontal(base) {
var baseLayer = base || getWidest();
layers = getResizebleLayers(baseLayer);
var baseW = baseLayer.frame().width();
for( var i=0; i<layers.length; i++ ) {
layers[i].frame().width = baseW + padding;
}
}
function resizeVertical(base) {
var baseLayer = base || getTallest();
layers = getResizebleLayers(baseLayer);
var baseH = baseLayer.frame().height();
for( var i=0; i<layers.length; i++ ) {
layers[i].frame().height = baseH + padding;
}
}
function reziseBoth(base) {
var baseLayer = base || getLargest();
layers = getResizebleLayers(baseLayer);
resizeHorizontal(baseLayer);
resizeVertical(baseLayer);
}
function getWidest() {
var maxWidth = 0;
var widestLayer;
for(var i=0; i<selection.count(); i++) {
var w = selection[i].frame().width();
if( w > maxWidth){
maxWidth = w;
widestLayer = selection[i];
}
}
return widestLayer;
}
function getTallest() {
var maxHeight = 0;
var tallestLayer;
for(var i=0; i<selection.count(); i++) {
var h = selection[i].frame().height();
if( h > maxHeight ){
maxHeight = h;
tallestLayer = selection[i];
}
}
return tallestLayer;
}
function getLargest(){
var maxWidth = 0;
var maxHeight = 0;
var largestLayer;
for( var i=0; i<selection.count(); i++) {
var w = selection[i].frame().width();
var h = selection[i].frame().height();
if( w > maxWidth && h > maxHeight ){
maxWidth = w;
maxHeight = h;
largestLayer = selection[i];
}
}
return largestLayer;
}
function getResizebleLayers(largest) {
var resizeLayers = [];
for( var i=0; i<selection.count(); i++) {
if(selection[i] != largest){
resizeLayers.push(selection[i]);
}
}
return resizeLayers;
}
function checkForConstrainProportions() {
var numConstrain = 0;
var continueScript = true;
for (var i = selection.count() - 1; i >= 0; i--) {
if(selection[i].constrainProportions()){
numConstrain++;
}
}
if( numConstrain >0 ) {
var numerus = (numConstrain == 1) ? 'layer has' : 'layers have';
var result = alert('Constrained Propotions', numConstrain + ' ' + numerus + ' constrained propotions. Do you want to continue anyway?',['Yes','No','Remove constraint']);
log(result);
continueScript = (result == YES_BUTTON);
// The user clicked "Remove constraints"
if(result == '1002') {
removeConstrainProporions();
continueScript = true;
}
}
return continueScript;
}
function removeConstrainProporions() {
for (var i = selection.count() - 1; i >= 0; i--) {
if(selection[i].constrainProportions()){
selection[i].constrainProportions = false;
}
}
}
// Alert
function alert(title, msg, buttons) {
var alert = COSAlertWindow.new();
alert.setMessageText(title);
alert.setInformativeText(msg);
if (buttons instanceof Array) {
var t;
for( t in buttons ){
alert.addButtonWithTitle( buttons[t] );
}
}
else alert.addButtonWithTitle(buttons || 'OK');
return alert.runModal();
}
function alertForFeedback(title, msg, label, defaultValue) {
var alert = COSAlertWindow.new();
alert.setMessageText(title);
alert.setInformativeText(msg);
alert.addTextLabelWithValue(label);
alert.addTextFieldWithValue(defaultValue);
alert.addButtonWithTitle('OK');
alert.addButtonWithTitle('Cancel');
var responseCode = alert.runModal();
var difference = Number(alert.viewAtIndex(1).stringValue());
return [responseCode, difference];
}
function notify(msg) {
doc.displayMessage(msg);
}