// CREATE USER INTERFACE
(function () {
var title = "Script";
// Reusable UI variables.
var g; // group
var p; // panel
var w; // window
// Permanent UI variables.
var btnCancel;
var btnOk;
var inpTitle;
var inpSeason;
var inpLanguage;
// CREATE USER INTERFACE
w = new Window("dialog", title);
w.alignChildren = "fill";
p = w.add("panel");
g = p.add("group");
g.add("statictext", undefined, "Title :");
inpTitle = g.add("edittext");
inpTitle.characters = 15;
g = p.add("group");
g.add("statictext", undefined, "Season :");
inpSeason = g.add("edittext");
inpSeason.characters = 15;
g = p.add("group");
g.add("statictext", undefined, "Language :");
inpLanguage = g.add("edittext");
inpLanguage.characters = 15;
g = w.add("group");
g.alignment = "center";
btnOk = g.add("button", undefined, "Ok");
btnCancel = g.add("button", undefined, "Cancel");
// UI EVENT HANDLERS
btnOk.onClick = function () {
w.close(1);
};
btnCancel.onClick = function () {
w.close(0);
};
// Show THE WINDOW
if (w.show() == 1) {
process();
}
function process() {
var doc = app.activeDocument;
var layers = doc.layers;
// Calculate the total number of artboards
var totalArtboards = 0;
for (var i = 0, l = layers.length; i < l; i++) {
doc.activeLayer = layers[i];
if (isArtBoard(doc.activeLayer)) {
totalArtboards++;
}
}
// Process artboards with a progress bar
var progressBar = new ProgressBar("Processing...", totalArtboards);
try {
for (var i = 0, l = layers.length; i < l; i++) {
doc.activeLayer = layers[i];
if (isArtBoard(doc.activeLayer)) {
// Update progress bar
progressBar.update(i - 1);
// Change the artboard name
doc.activeLayer.name = inpTitle.text + "_" + inpSeason.text + "_Main_" + doc.activeLayer.name;
// Export the artboard as PNG
exportArtboardToPNG(doc, doc.activeLayer, inpTitle.text + "_" + inpSeason.text + "_Main_" + doc.activeLayer.name + "_" + inpLanguage.text);
}
}
} finally {
// Close progress bar
progressBar.close();
}
}
function exportArtboardToPNG(doc, layer, filename) {
var exportOptions = new ExportOptionsSaveForWeb();
exportOptions.format = SaveDocumentType.PNG;
exportOptions.PNG8 = false;
exportOptions.transparency = true;
exportOptions.interlaced = false;
exportOptions.quality = 100;
// Save current ruler unit
var originalRulerUnits = app.preferences.rulerUnits;
app.preferences.rulerUnits = Units.PIXELS;
// Check if the layer is an artboard
if (isArtBoard(layer)) {
// Set canvas size to match layer dimensions
var bounds = layer.bounds;
var width = bounds[2] - bounds[0];
var height = bounds[1] - bounds[3];
doc.resizeCanvas(width, height, AnchorPosition.TOPLEFT);
// Export the layer as PNG
var exportFile = new File(doc.path + "/" + filename + ".png");
doc.exportDocument(exportFile, ExportType.SAVEFORWEB, exportOptions);
// Restore original ruler unit
app.preferences.rulerUnits = originalRulerUnits;
// Undo the resizeCanvas to keep the original document intact
doc.activeHistoryState = doc.historyStates[doc.historyStates.length - 2];
} else {
alert("Unable to export layer. Make sure the layer is an artboard or text layer.");
}
}
function isArtBoard(layer) {
// Check if the layer is an artboard
try {
return layer.isBackgroundLayer || layer.grouped || layer.layers.length > 0;
} catch (e) {
return false;
}
}
function ProgressBar(title, maxSteps) {
this.window = new Window("palette", title);
this.progress = this.window.add("progressbar", undefined, 0, maxSteps);
this.progress.maximumSize.width = 300;
this.window.show();
this.update = function (step) {
this.progress.value = step;
this.window.update();
};
this.close = function () {
this.window.close();
};
}
})();
//////////////////////////////////////////////////