-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRandomLayerOrder.jsx
174 lines (156 loc) · 5.75 KB
/
RandomLayerOrder.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
/**
Script © 2022 Boris Boguslavsky
https://github.com/borisboguslavsky
Based on the Random Layer Order Functionality from the Randomill Illustrator Plugin
https://randomill.com/
Feel free to modify this script as you see fit. I hope you find it useful.
*/
// ######## ### ## ## ######## ## ## ## ####
// ## ## ## ## ### ## ## ## ## ## ##
// ## ## ## ## #### ## ## ## ## ## ##
// ######## ## ## ## ## ## ###### ## ## ## ##
// ## ######### ## #### ## ## ## ## ##
// ## ## ## ## ### ## ## ## ## ##
// ## ## ## ## ## ######## ######## ####### ####
var mainPanelWindow = new Window('dialog', 'Random Order \u00A9 randomill.com', undefined);
mainPanelWindow.orientation = 'column';
mainPanelWindow.alignChildren = ['fill', 'fill'];
// Radios for selection method
var optionsGroup = mainPanelWindow.add('panel', undefined, 'Options:');
optionsGroup.orientation = 'column';
optionsGroup.alignChildren = ['fill', 'fill'];
var respectParentLayers = optionsGroup.add('checkbox', undefined, 'Respect Parent Layers');
respectParentLayers.value = true;
var cancelAndOkButtons = mainPanelWindow.add('group');
cancelAndOkButtons.alignChildren = ['fill', 'fill'];
cancelAndOkButtons.margins = [0, 0, 0, 0];
var cancel = cancelAndOkButtons.add('button', undefined, 'Cancel');
cancel.helpTip = 'Press Esc to Close';
cancel.onClick = function () {
mainPanelWindow.close();
}
var ok = cancelAndOkButtons.add('button', undefined, 'OK');
ok.helpTip = 'Press Enter to Run';
ok.onClick = function () {
try {
randomizeLayerOrder();
} catch(err) {
alert(err)
}
mainPanelWindow.close();
}
ok.active = true;
// ## ## ### #### ## ##
// ### ### ## ## ## ### ##
// #### #### ## ## ## #### ##
// ## ### ## ## ## ## ## ## ##
// ## ## ######### ## ## ####
// ## ## ## ## ## ## ###
// ## ## ## ## #### ## ##
function randomizeLayerOrder(objects) {
// Error Checks
if (app.documents.length === 0) {
alert("Error: No currently active document.");
return;
}
var objects = app.activeDocument.selection;
if (objects.length < 2) {
alert("Error: Please select at least two items. If more than two items are selected, make sure they're ungrouped.");
return;
}
if (respectParentLayers.value) {
shuffleLayersRespectParents(objects)
} else {
shuffleLayers(objects)
}
return;
}
function shuffleLayers(objects) {
var shuffled = shuffleArray(objects)
var loopLength = objects.length%2===0 ? objects.length / 2 : Math.floor(objects.length / 2) + 1
for (var i=0; i < loopLength; i++) {
var a = shuffled[i*2]
var b = shuffled[(i*2)+1]
if (!b) {
b = shuffled[0]
}
swapTwoLayers(a, b)
}
}
function shuffleLayersRespectParents(objects) {
// There must be at least two objects
if (objects.length < 2) return;
// Create a dictionary of arrays of all the objects where each array represents a parent layer
var separatedObjects = collectObjectsByParentLayers(objects);
// Get the keys for that dictionary (will be the absoluteZOrderPosition) of each parent layer
var separatedParentLayers = getKeys(separatedObjects)
// For every parent layer in the dictionary
for (var i=0; i < separatedParentLayers.length; i++) {
// Arrange all the objects in order for that parent layer
shuffleLayers(separatedObjects[separatedParentLayers[i]])
};
}
// ## ## ######## ## ######## ######## ######## ######
// ## ## ## ## ## ## ## ## ## ## ##
// ## ## ## ## ## ## ## ## ## ##
// ######### ###### ## ######## ###### ######## ######
// ## ## ## ## ## ## ## ## ##
// ## ## ## ## ## ## ## ## ## ##
// ## ## ######## ######## ## ######## ## ## ######
function swapTwoLayers(a, b) {
/*
Swaps the layer stack position of two objects
Currently relies on a temporary object to retain the position of the first object that has to be moved
*/
var temp = b.duplicate(b)
b.move(a, ElementPlacement.PLACEBEFORE)
a.move(temp, ElementPlacement.PLACEAFTER)
temp.remove()
}
function collectObjectsByParentLayers(objects) {
/*
Takes a collection of objects, and subdivides them into arrays based on their parent layers.
The function returns a dictionary of arrays containing objects that are within the same parent layer.
The dictionary keys are the parent layers' `absoluteZOrderPosition` values.
*/
var objects = app.activeDocument.selection;
var topArr = new Object();
// For each object
for (var i=0; i < objects.length; i++) {
// Make sure that the topArr has an entry for the parent layer of the object
if (!topArr[objects[i].layer.absoluteZOrderPosition]) topArr[objects[i].layer.absoluteZOrderPosition] = new Array();
// Push that object to the parent layer array
topArr[objects[i].layer.absoluteZOrderPosition].push(objects[i])
}
return topArr;
}
function getKeys(obj) {
if (Object(obj) !== obj) {
throw new TypeError('Object.keys can only be called on Objects.');
}
var hasOwnProperty = Object.prototype.hasOwnProperty;
var result = [];
for (var prop in obj) {
if (hasOwnProperty.call(obj, prop)) {
result.push(prop);
}
}
return result;
};
function shuffleArray(array) {
var curId = array.length;
// There remain elements to shuffle
while (0 !== curId) {
// Pick a remaining element
var randId = Math.floor(Math.random() * curId);
curId -= 1;
// Swap it with the current element.
var tmp = array[curId];
array[curId] = array[randId];
array[randId] = tmp;
}
return array;
}
// Show script UI when script is run
mainPanelWindow.center();
mainPanelWindow.show();