-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPanel Splitter.jsx
executable file
·224 lines (185 loc) · 6.18 KB
/
Panel Splitter.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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
/*
Project: Panel Splitter for Adobe Photoshop
Author: dilshan-h [https://github.com/dilshan-h]
Description: Split your image/canvas into cells using rows, columns & save them as high quality PDFs.
Copyright (c) [2024] - MIT License
*/
// flag to control script execution
var continueScript = true;
// Save the current preferences
var startDisplayDialogs = app.displayDialogs;
// Set Adobe Photoshop to display no dialogs
app.displayDialogs = DialogModes.NO;
// Store the state of the document before changes
var doc = app.activeDocument;
var initialState = doc.activeHistoryState;
var dialog = new Window("dialog", "Confirmation - Panel Splitter");
dialog.alignChildren = "left";
var fontText = ScriptUI.newFont("Segoe UI", "Regular", 14);
var fontBtns = ScriptUI.newFont("Segoe UI", "Regular", 14);
// Add a message label
var messageLine1 = dialog.add(
"statictext",
undefined,
"IMPORTANT: Make sure that;"
);
messageLine1.graphics.font = fontText;
var messageLine2 = dialog.add(
"statictext",
undefined,
"1. You have saved your file."
);
messageLine2.graphics.font = fontText;
var messageLine3 = dialog.add(
"statictext",
undefined,
"2. You have a backup of this document."
);
messageLine3.graphics.font = fontText;
var messageLine4 = dialog.add(
"statictext",
undefined,
"Do you want to proceed?"
);
messageLine4.graphics.font = fontText;
// Add a group to contain buttons
var buttonGroup = dialog.add("group");
buttonGroup.alignment = "center";
buttonGroup.alignChildren = "center";
// Add Yes and No buttons
var yesButton = buttonGroup.add("button", undefined, "Yes");
yesButton.graphics.font = fontBtns;
var noButton = buttonGroup.add("button", undefined, "No");
noButton.graphics.font = fontBtns;
var bottomText = dialog.add(
"statictext",
undefined,
"Panel Splitter • Made with ❤ by dilshan-h • github.com/dilshan-h"
);
// Functions to handle button click events
yesButton.onClick = function () {
alert("On the next dialog box, select a location to save the panels.");
dialog.close();
};
noButton.onClick = function () {
alert("Script will be stopped now. Run again to continue.");
dialog.close();
continueScript = false;
};
dialog.show();
if (continueScript) {
// ask the user for the output folders
var outputFolder = Folder.selectDialog(
"Select a folder for the output files"
);
if (outputFolder == null) {
alert(
"An output directory is required!\nScript will be stopped now. Run again to continue."
);
continueScript = false;
}
}
if (continueScript) {
cropAndSavePDFs();
// Reset the application preferences
app.displayDialogs = startDisplayDialogs;
// Undo changes to revert the document state
doc.activeHistoryState = initialState;
app.activeDocument.close(SaveOptions.DONOTSAVECHANGES);
alert("Files successfully saved!");
}
// Function to prompt user for rows and columns
function promptRowsColumns() {
var rows = parseInt(prompt("Enter the number of rows:", ""));
var columns = parseInt(prompt("Enter the number of columns:", ""));
return [rows, columns];
}
// Function to clear existing guides
function clearGuides() {
var doc = app.activeDocument;
doc.guides.removeAll();
}
// Function to add outer guides
function setOuterGuides() {
var doc = app.activeDocument;
var width = doc.width.value;
var height = doc.height.value;
doc.guides.add(Direction.HORIZONTAL, 0);
doc.guides.add(Direction.HORIZONTAL, height);
doc.guides.add(Direction.VERTICAL, 0);
doc.guides.add(Direction.VERTICAL, width);
}
// Function to add new guides based on rows and columns
function addGuides(rows, columns) {
var doc = app.activeDocument;
var width = doc.width.value;
var height = doc.height.value;
var horizontalSpacing = width / columns;
var verticalSpacing = height / rows;
for (var i = 1; i < rows; i++) {
doc.guides.add(Direction.HORIZONTAL, verticalSpacing * i);
}
for (var j = 1; j < columns; j++) {
doc.guides.add(Direction.VERTICAL, horizontalSpacing * j);
}
}
// Function to save selection as PDF
function saveAsPDF(selectionBounds, outputPath) {
var doc = app.activeDocument;
var bounds = selectionBounds;
doc.crop(bounds);
var saveOptions = new PDFSaveOptions();
saveOptions.compatibility = PDFCompatibility.PDF14;
saveOptions.embedThumbnail = true;
saveOptions.encoding = PDFEncoding.JPEG;
saveOptions.jpegQuality = 12;
saveOptions.layers = false;
saveOptions.preserveEditing = false;
saveOptions.view = false;
doc.saveAs(new File(outputFolder + outputPath), saveOptions);
}
// Main function
function cropAndSavePDFs() {
var rowsColumns = promptRowsColumns();
if (!rowsColumns || rowsColumns.length !== 2) return;
var rows = rowsColumns[0];
var columns = rowsColumns[1];
var doc = app.activeDocument;
clearGuides();
setOuterGuides();
addGuides(rows, columns);
// Merge visible layers
doc.flatten();
var guides = doc.guides;
var pdfCounter = 1;
var horizontalGuides = [];
var verticalGuides = [];
// Group guides based on orientation
for (var i = 0; i < guides.length; i++) {
if (guides[i].direction === Direction.HORIZONTAL) {
horizontalGuides.push(guides[i]);
} else if (guides[i].direction === Direction.VERTICAL) {
verticalGuides.push(guides[i]);
}
}
// Sort guides based on their coordinates
horizontalGuides.sort(function (a, b) {
return a.coordinate - b.coordinate;
});
verticalGuides.sort(function (a, b) {
return a.coordinate - b.coordinate;
});
for (var i = 0; i < rows; i++) {
for (var j = 0; j < columns; j++) {
var top = horizontalGuides[i].coordinate;
var bottom = horizontalGuides[i + 1].coordinate;
var left = verticalGuides[j].coordinate;
var right = verticalGuides[j + 1].coordinate;
var selectionBounds = [left, top, right, bottom];
var outputPath = "/Panel_" + pdfCounter + ".pdf";
saveAsPDF(selectionBounds, outputPath);
doc.activeHistoryState = doc.historyStates[doc.historyStates.length - 2];
pdfCounter++;
}
}
}