-
Notifications
You must be signed in to change notification settings - Fork 33
/
automagically-sort-selectedlayers-by-name.jsx
177 lines (152 loc) · 6.18 KB
/
automagically-sort-selectedlayers-by-name.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
/**
* This script tries to sort layers by name
* more a proof of concept thingy
* using this technique can hold some problems
* - in this case the "et" gets found in "consectetur" "et" "amet"
* - the last matching pattern will be the one that gets executed
*/
// Copyright (c) 2013
// Fabian "fabiantheblind" Morón Zirfas
// Permission is hereby granted, free of charge, to any
// person obtaining a copy of this software and associated
// documentation files (the "Software"), to deal in the Software
// without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense,
// and/or sell copies of the Software, and to permit persons to
// whom the Software is furnished to do so, subject to
// the following conditions:
// The above copyright notice and this permission notice
// shall be included in all copies or substantial portions of the Software.
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
// IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
// TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTIO
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
// see also http://www.opensource.org/licenses/mit-license.php
/**
* get unique Array content
* does not work on mixed arrays
* http://stackoverflow.com/questions/1960473/unique-values-in-an-array
* @return {Array} [description]
*/
Array.prototype.getUnique = function() {
var u = {}, a = [];
for(var i = 0, l = this.length; i < l; ++i) {
if(u.hasOwnProperty(this[i])) {
continue;
}
a.push(this[i]);
u[this[i]] = 1;
}
return a;
};
/**
* LayerSet Class
* @param {String} _name The name of the class corresponds to the pattern
*/
function LayerSet(_name) {
this.name = _name;
this.collection = [];// will hold layers
this.add = function(layer) {
this.collection.push(layer);
};
}
/**
* A Pattern Class
* @param {a regex string} _reg String
*/
function Pattern(_reg) {
this.reg = escapeRegExp(_reg);
}
function automagically_sort() {
app.beginUndoGroup('automagically sort by name');
/**
* prerequisites check if thete is a comp
* @type {[type]}
*/
var curComp = app.project.activeItem;
if (!curComp || !(curComp instanceof CompItem)) {
// alert("noComp");
// return;
curComp = app.project.items.addComp('automagically sort', 500, 500, 1, 10, 25);
}
var names = ['Lorem', 'ipsum', 'dolor', 'sit', 'amet', 'consectetur', 'adipisicing', 'elit', 'sed', 'do', 'eiusmod', 'tempor', 'incididunt', 'ut', 'labore', 'et', 'dolore', 'magna', 'aliqua', 'Lorem', 'ipsum', 'dolor', 'sit', 'amet', 'consectetur', 'adipisicing', 'elit', 'sed', 'do', 'eiusmod', 'tempor', 'incididunt', 'ut', 'labore', 'et', 'dolore', 'magna', 'aliqua'];// some random names
var layers = [];// holds the layers
for(var l = 0; l < names.length; l++) {
var lay = curComp.layers.addNull();// add a null
lay.name = makeid(4) + ' ' + names[l] + ' ' + makeid(5); // give it a name
layers.push(lay);// push to array
} // close l create layers loop. // this shoudl be a space seprated field or something like that
var unique_names = names.getUnique(); // use the prototype functiion
var patterns = []; // this will hold the patterns
for (var n = 0; n < unique_names.length; n++) {
patterns.push(new Pattern(unique_names[n]));
} // close ncreate patterns from names loop
var sets = []; // will hold all the layer sets
for(var r = 0; r < patterns.length; r++) {
var set = new LayerSet(patterns[r].reg);// make a new set per pattern
for (var i = 0; i < layers.length; i++) {
var layer = layers[i];// isolate
var reg = new RegExp(patterns[r].reg, 'g');// make a regex
if(reg.test(layer.name) === true) {
set.add(layer);// Yes found something push to LayerSet
} // close regex check
} // close i layers loop
if(set.collection.length > 0) {
sets.push(set);
}
}// close r pattern loop
/**
* Now the sorting
* We need to get every item from the set and move it to the top
*/
// alert(sets.toSource());
for (var s = 0; s < sets.length; s++) {
var curSet = sets[s];// isolate
if(curSet.collection.length > 0) {
// continue;
// we have something in the set loop the collection
var collabel = (s) % 16;
for (var cl = 0; cl < curSet.collection.length; cl++) {
var curLayer = curSet.collection[cl];
curLayer.moveToBeginning();
if(parseInt(app.version, 10) > 10) {
// CS6+ feature
curLayer.label = collabel;
}
} // end cl collection loop
}// if there are no layers move to the next set
} // end s sets loop
app.endUndoGroup();
}
/**
* [escapeRegExp description]
* @param {[type]} str [description]
* @return {[type]} [description]
* http://stackoverflow.com/questions/3446170/escape-string-for-use-in-javascript-regex
*/
function escapeRegExp(str) {
return str.replace(/[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g, '\\$&');
}
/**
* Make pseudo random text found here
* http://stackoverflow.com/questions/1349404/generate-a-string-of-5-random-characters-in-javascript
* @return {String} [Pseudo random string]
*/
function makeid(num) {
var text = '';
var possible = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
for(var i = 0; i < num; i++) {
text += possible.charAt(Math.floor(Math.random() * possible.length));
}
return text;
}
// _ _ ______ _______ _ _ _ _ _____ _______ _______ _____ _ _ _____
// | \ | |/ __ \ \ / / __ \| | | | \ | | __ \ /\|__ __|__ __|_ _| \ | |/ ____|
// | \| | | | \ \ /\ / /| |__) | | | | \| | | | | / \ | | | | | | | \| | | __
// | . ` | | | |\ \/ \/ / | _ /| | | | . ` | | | |/ /\ \ | | | | | | | . ` | | |_ |
// | |\ | |__| | \ /\ / | | \ \| |__| | |\ | |__| / ____ \| | | | _| |_| |\ | |__| |
// |_| \_|\____/ \/ \/ |_| \_\\____/|_| \_|_____/_/ \_\_| |_| |_____|_| \_|\_____|
automagically_sort();