forked from johnwun/js4ai
-
Notifications
You must be signed in to change notification settings - Fork 0
/
organize.jsx
209 lines (194 loc) · 7.21 KB
/
organize.jsx
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
/////////////////////////////////////////////////////////////////
//organize v1.4 -- CS,CS2,CS3,CS4,CS5
//>=--------------------------------------
// A quick way to sort selected items by a given attribute.
// For example, use this script to arrange items from left to right based on their height.
// Another example is to change a group of items z-order position based on volumetric area.
// More complexly, you can re-assign height based on x position.
// Items can be sorted on (h)eight, (w)idth, (a)rea, (x)-axis, (y)-axis, (o)pacity, (z)-order)
// Sort directions are small-to-large, large-to-small, and random.
// Use most effectively in combination with the align palette to redistribute objects in new
// powerful ways.
//
// v1.1: changed wording for clarity,
// added option to keep existing values, vs. align evenly.
// and fixed prompt values displaying correctly. (thanks Egor)
// v1.2: Fixed inconsistant spacing issue with autoAlign option
// v 1.3: added a shortcut option in the first prompt.
// v 1.4: Much better error handling.
//>=--------------------------------------
// JS code (c) copyright: John Wundes ( [email protected] ) www.wundes.com
//copyright full text here: http://www.wundes.com/js4ai/copyright.txt
//////////////////////////////////////////////////////////////////
var warn,notValid,attrPrompt,orderPrompt,sortPrompt,dist,alert_fail, createItemSort, getInterval, go, isBad, numericSort, randomSort, rearrange, revNumericSort,getFirstPrompt,prompt_attr, prompt_order1, prompt_order2, prompt_sort,prompt_dist, attrParams, validDistVals,sortParams ;
prompt_attr = "Sorting BY: What attribute would you like to use for input?\n(h)eight, (w)idth, (a)rea, (x)-axis, (y)-axis, (o)pacity, (z)-order)\n Alternately, you can enter a 4 letter sequence here, like \"hxsy\"";
prompt_order1 = "Sort ALONG: Align your '";
prompt_order2 = "' sorted items by: \n(h)eight, (w)idth, (a)rea, (x)-axis, (y)-axis, (o)pacity, (z)-order)";
prompt_sort = "Sort DIRECTION: (L)arge to small, (S)mall to large, or (R)andom?";
prompt_dist = "Distribute objects evenly (y), or just shuffle existing values (n)?";
attrParams = {
'h': "height",
'w': "width",
'a': "area",
'x': "left",'l':"left",
'y': "top",'t':"top",
'o': "opacity",
'z': "zOrderPosition"
};
numericSort = function(a, b) {
return a - b;
};
revNumericSort = function(a, b) {
return b - a;
};
randomSort = function(a, b) {
return Math.random() - .5;
};
sortParams = {
's': numericSort,
"ns":numericSort,
'l': revNumericSort,
"ls":revNumericSort,
'r': randomSort,
"rs":randomSort
};
validDistVals = { y:true,n:false,t:true,f:false,'0':false,'1':true};
isBad = function(val) {
return val === "" || !(val != null);
};
//rearrange(selArr, createItemSort(attrParam), sortOrder, sortParam,dist);
rearrange = function(_arr, _byFilter, _sortDirection, _alongParameter, _interpolate) {
var fin, max, selArrLen, start, tempAtts, _results, _results2;
_arr.sort(_byFilter);
selArrLen = _arr.length;
tempAtts = [];
while (selArrLen--) {
tempAtts.push(_arr[selArrLen][_alongParameter]);
}
tempAtts.sort(_sortDirection);
selArrLen = _arr.length;
if (_alongParameter === "zOrderPosition") {
_results = [];
while (selArrLen--) {
_results.push((function() {
var _results2;
_results2 = [];
while (_arr[selArrLen][_alongParameter] !== tempAtts[selArrLen]) {
_results2.push(_arr[selArrLen][_alongParameter] > tempAtts[selArrLen] ? _arr[selArrLen].zOrder(ZOrderMethod.SENDBACKWARD) : _arr[selArrLen].zOrder(ZOrderMethod.BRINGFORWARD));
}
return _results2;
})());
}
return _results;
} else {
if(_alongParameter === 'top'){tempAtts.reverse();}
start = tempAtts[0]; // arr[0][_alongParameter];//
fin = tempAtts[selArrLen - 1]; // arr[selArrLen-1][_alongParameter];//
max = selArrLen;
while (selArrLen--) {
_arr[selArrLen][_alongParameter] = ((_sortDirection === randomSort || !_interpolate )? tempAtts[selArrLen] : getInterval(start, fin, max-1, selArrLen));
}
}
};
createItemSort = function(attrStr) {
var attr = attrStr;
return function(a, b) {
return Number(a[attr]) - Number(b[attr]);
};
};
notValid = function(arr,val){
return arr[val.toLowerCase()] === undefined;
}
warn = function (val,dim){
alert('"'+val+'" is not a valid value for '+dim+'.');
}
getFirstPrompt = function (){
attrPrompt = prompt(prompt_attr, "h");
if (isBad(attrPrompt)) {
return false;
}
if(attrPrompt.length==4){ //is shortcut
var pArr = attrPrompt.split('');
// test all are valid:
if(notValid(attrParams,pArr[0])){ warn(pArr[0],'BY'); return -1;}
if(notValid(attrParams,pArr[1])){ warn(pArr[1],'ALONG'); return -1;}
if(notValid(sortParams,pArr[2])){ warn(pArr[2],'DIRECTION');return -1;}
if(notValid(validDistVals,pArr[3])){ warn(pArr[3],'DISTRIBUTION.'); return -1;}
attrPrompt=pArr[0];
orderPrompt=pArr[1];
sortPrompt=pArr[2];
dist=(pArr[3]=='y' || pArr[3]=='t')?true:false;
}
if(attrParams[attrPrompt.toLowerCase()] === undefined){ alert('"'+attrPrompt+'" is not a valid value for (1) BY.'); return -1;}
return attrParams[attrPrompt.toLowerCase()];
}
getInterval = function(start, fin, len, curr) {
var f, s;
s = start;
f = fin;
if (start < 0) {
s = 0;
f = f - start;
}
return start + (((f - s) / len) * curr);
};
alert_fail = "Please open a file in Illustrator, and select some stuff to sort.";
go = function() {
var attrParam, doc, sel, selArr, selLen, sortOrder, sortParam;
doc = activeDocument;
sel = doc.selection;
selArr = [];
sortPrompt = void 0;
orderPrompt = void 0;
attrPrompt = void 0;
attrParam = void 0;
sortParam = void 0;
sortOrder = void 0;
dist = void 0;
/*---------------- get user input ----------------*/
var invalid = true;
while(invalid)
{
var fp = getFirstPrompt();
if(fp==false){return false;}
if(fp !== -1){invalid = false;
attrParam = fp;
}
}
invalid = true;
if(orderPrompt === undefined){
while (invalid) {
orderPrompt = prompt(prompt_order1 + attrParam + prompt_order2, "x");
if (isBad(orderPrompt)) {
return false;
}
if(notValid(attrParams,orderPrompt)){ warn(orderPrompt,'ALONG'); } else{ invalid = false; }
}
}
sortParam = attrParams[orderPrompt.toLowerCase()];
invalid = true;
if(sortPrompt === undefined){
while (invalid) {
sortPrompt = prompt(prompt_sort, "s");
if (isBad(sortPrompt)) {
return false;
}
if(notValid(sortParams,sortPrompt)){ warn(sortPrompt,'DIRECTION'); } else{ invalid = false; }
}
}
sortOrder = sortParams[sortPrompt.toLowerCase()];
if(dist === undefined){
dist = confirm(prompt_dist);
}
selLen = sel.length;
while (selLen--) {
selArr.push(sel[selLen]);
}
rearrange(selArr, createItemSort(attrParam), sortOrder, sortParam,dist);
return true;
};
if (app.activeDocument) {
if (activeDocument.selection.length==0){alert(alert_fail)}else{go();}
} else {
alert(alert_fail);
}