forked from johnwun/js4ai
-
Notifications
You must be signed in to change notification settings - Fork 0
/
distributeStackedObjects.jsx
90 lines (76 loc) · 3.14 KB
/
distributeStackedObjects.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
/////////////////////////////////////////////////////////////////
// Distribute Stacked Objects v1.1(and Center them too.) -- CS,CS5
//>=--------------------------------------
// So you just imported 20 eps files and illustrator stacks them on top of each other.
// You want to see them spread out a bit? Run this script.
//
// This script distributes and centers all selected objects.
// Each object's position is determined by it's stacking order in the layer palette.
// To adjust an objects position in the layout,
// manually move the object's stacking order in the layer palette.
// The top object (in the layer) is always placed first.
//
// When the script is run, a prompt appears,
// and the user can define a custom padding value for spacing the grid.
// The script then auto-distributes objects, and centers them on the page.
//
// This script was designed for creating artboards and for easy viewing
// of a stack of imported artwork, but it should work with everything.
//
// v1.1:Fixed a bug that crashed the script.
//>=--------------------------------------
// JS code (c) copyright: John Wundes ( [email protected] ) www.wundes.com
//copyright full text here: http://www.wundes.com/js4ai/copyright.txt
//////////////////////////////////////////////////////////////////
var doc = activeDocument;
var selx = doc.selection;
if(selx.length ==0){alert("You must select objects to distribute.");}
else{makeGrid(selx);}
function makeGrid(sel)
{
var objectsCentered = true;
if(objectsCentered){
var newGroup = app.activeDocument.groupItems.add();
}
var maxW = maxH = currentX = currentY = maxRowH = 0;
var padding = "not valid";
while (isNaN(padding)){
padding = Number(prompt("How much padding between objects?","10"));
}
var layout ='g';
layout = prompt('(H)orizontal, (V)ertical, or (G)rid?\nIf you don\'t care and want to skip this dialog, comment out line 44)','G');
var gridCols = (layout.toLowerCase() == "h") ? sel.length : 1;
gridCols = (layout.toLowerCase() == "g" ) ? Math.round(Math.sqrt(sel.length)) : gridCols;
for(var e=0, slen=sel.length;e<slen;e++)
{
if(objectsCentered){
// ::Add to group
sel[e].moveToBeginning( newGroup );
}
// :::SET POSITIONS:::
sel[e].top = currentY;
sel[e].left = currentX;
// :::DEFINE X POSITION:::
currentX += (sel[e].width + padding);
var itembottom = (sel[e].top-sel[e].height);
maxRowH = itembottom < maxRowH ? itembottom : maxRowH;
if((e % gridCols) == (gridCols - 1))
{
currentX = 0;
maxH = (maxRowH);
// :::DEFINE Y POSITION:::
currentY = maxH-padding;
maxRowH=0;
}
}
if(objectsCentered){
newGroup.top = -( doc.height/2) + newGroup.height/2;
newGroup.left = (doc.width/2)-newGroup.width/2;
// :::UNGROUP:::
var sLen=sel.length;
while(sLen--)
{
sel[sLen].moveToBeginning( doc.activeLayer );
}
}
}