-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathResize all Artboards in all Files in Folder.jsx
More file actions
152 lines (126 loc) · 4.98 KB
/
Copy pathResize all Artboards in all Files in Folder.jsx
File metadata and controls
152 lines (126 loc) · 4.98 KB
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
#target Illustrator
/*
Revision-1
Author: Shivendra Agarwal
Year: 2017
Title: Script to scale-up artwork and artboard above 15 Mpixel
*/
if (app.documents.length > 0)
alert("ERROR: \n Close all documents before running this script.");
requiredABsize = prompt('Scale artboards to what square size?\nREMEMBER THIS NUMBER TO CREATE ENOUGH SPACE BETWEEN YOUR SCALED ARTBOARDS IN THE NEXT STEP.\n\nThis script uses \"Scale Strokes and Effects\" for you.\n\n', '500', 'Select artboard area');
dir = Folder.selectDialog("Select root folder containing Illustrator assets.");
// If dir variable return null, user most likely canceled the dialog or
// the input folder and it subfolders don't contain any .ai files.
if (dir != null) {
// returns an array of file paths in the selected folder.
files = GetFiles(dir);
alert('Total ' + files.length + ' files (AI/EPS) will be processed.', 'Alert');
for (var f = 0; f < files.length; f++) {
var doc = app.open(files[f]);
resizeArtboardAndArwork();
doc.close(SaveOptions.SAVECHANGES);
}
}
function resizeArtboardAndArwork() {
app.executeMenuCommand("ReArrange Artboards");
var activeDoc = app.activeDocument;
var TotalArtboards = activeDoc.artboards.length;
var originalOrigin = activeDoc.rulerOrigin;
for (var i = 0; i < TotalArtboards; i++) {
var _artboards = activeDoc.artboards;
var abActive = _artboards[i];
var abProps = getArtboardBounds(abActive);
var scale = findRequiredScale(abProps);
// abActive = activeDoc.artboards[ activeDoc.artboards.getActiveArtboardIndex() ];
activeDoc.artboards.setActiveArtboardIndex(i);
// alert(i);
app.selection = [];
// app.executeMenuCommand ('deselectall');
app.executeMenuCommand('selectallinartboard');
var selection = activeDoc.selection;
var artboardRight = abActive.artboardRect[2];
// Get the Height of the Artboard
var artboardBottom = abActive.artboardRect[3];
var artboardX = abActive.artboardRect[0];
var artboardY = abActive.artboardRect[1];
var horziontalCenterPosition = (artboardRight + (-1 * artboardX)) / 2;
var verticalCenterPosition = (artboardY - (artboardBottom)) / 2;
//alert(app.activeDocument.rulerOrigin+"\n left "+artboardX+"\ntop "+artboardY+"\nright "+artboardRight+"\nbottom "+artboardBottom+"\nTheleft: "+horziontalCenterPosition+"\nThevert: "+verticalCenterPosition);
activeDoc.rulerOrigin = [horziontalCenterPosition, verticalCenterPosition];
app.selection = [];
activeDoc.selectObjectsOnActiveArtboard();
// app.executeMenuCommand ('selectallinartboard');
// Check if anything is selected:
if (selection.length > 0) {
for (j = 0; j < selection.length; j++) {
selection[j].resize(scale * 100, scale * 100, true, true, true, true, scale * 100, Transformation.DOCUMENTORIGIN);
}
}
var scaledArtboardRect = newRect(-abProps.width / 2 * scale, -abProps.height / 2 * scale, abProps.width * scale, abProps.height * scale);
activeDoc.artboards[i].artboardRect = scaledArtboardRect;
}
activeDoc.rulerOrigin = originalOrigin;
}
function findRequiredScale(props) {
requiredABarea = 250000; //px
currentABarea = props.width * props.height;
scale = (Math.sqrt(requiredABarea / currentABarea));
if (scale > 1)
return scale;
else
return 1;
}
// Artboard bounds helper (used above):
function getArtboardBounds(artboard) {
var bounds = artboard.artboardRect,
left = bounds[0],
top = bounds[1],
right = bounds[2],
bottom = bounds[3],
width = right - left,
height = top - bottom,
props = {
left: left,
top: top,
width: width,
height: height
};
return props;
}
function newRect(x, y, width, height) {
var l = 0;
var t = 1;
var r = 2;
var b = 3;
var rect = [];
rect[l] = x;
rect[t] = -y;
rect[r] = width + x;
rect[b] = -(height - rect[t]);
return rect;
};
function GetFiles(folder) {
var i, item,
// Array to store the files in...
files = [],
// Get files...
items = folder.getFiles();
// Loop through all files in the given folder
for (i = 0; i < items.length; i++) {
item = items[i];
// Find .ai files
var aifileformat = item.name.match(/\.ai$/i);
var epsfileformat = item.name.match(/\.eps$/i);
// If item is a folder, check the folder for files.
if (item instanceof Folder) {
// Combine existing array with files found in the folder
files = files.concat(GetFiles(item));
}
// If the item is a file, push it to the array.
else if (item instanceof File && (epsfileformat || aifileformat)) {
// Push files to the array
files.push(item);
}
}
return files;
}