How to Export to artboards using the photoshop variables
I know how to export to each psd file using the photoshop variables.
But I want to know how to replicate artboards with the data in my csv file.
I know how to export to each psd file using the photoshop variables.
But I want to know how to replicate artboards with the data in my csv file.
Assuming that you have a folder containing layered PSD files created from the data sets, you can try the following script to stack each PSD as an artboard, retaining the original layers from each separate PSD. This script is only intended for later CC versions and has been successfully tested in v2021, v2024 and v2025 on Mac and Win.
EDIT: Original code upated to v1.7 (20th August 2025)
/*
Stack Layered Files to Artboards.jsx
Stephen Marsh
v1.0 - 8th May 2024: Initial release.
v1.1 - 9th May 2024: Added code to set the artboard background colour.
v1.2 - 13th August 2025: Reworked the selectAllLayers function. Reworked the smart object conversion code.
v1.3 - 14th August 2025: Added a step to unlock all selected floating layers before converting to a smart object. Various UX enhancements.
v1.4 - 15th August 2025: Artboard creation reworked to correctly handle content extending out of the artboard bounds. Smart objects are no longer used to dupe artboards.
v1.5 - 16th August 2025: Added checks to abort the script if artboards are detected in the input files.
v1.6 - 19th August 2025: Artboard arrangement modified: 0 = one single row, 1 = one single column, 2 = two columns etc. Digit validation check added to the column number prompt.
v1.7 - 20th August 2025: Code refactored, hopefully it's not broken! :]
https://community.adobe.com/t5/photoshop-ecosystem-discussions/how-to-export-to-artboards-using-the-photoshop-variables/td-p/14603249
Originally based on:
https://community.adobe.com/t5/photoshop-ecosystem/combining-mulitple-photoshop-files-into-1-photoshop-file-with-artboards/td-p/12218200
*/
// Global helpers
var c2t = function (s) { return app.charIDToTypeID(s); };
var s2t = function (s) { return app.stringIDToTypeID(s); };
#target photoshop
try {
if (app.documents.length === 0) {
(function () {
var origUnits = app.preferences.rulerUnits;
app.preferences.rulerUnits = Units.PIXELS;
var inputFolder = Folder.selectDialog('Please select the input folder:');
if (inputFolder === null) {
app.beep();
return;
}
var inputFiles = inputFolder.getFiles(/\.(webp|avif|jpg|jpeg|tif|tiff|png|psd|psb)$/i);
inputFiles.sort(function (a, b) {
return decodeURI(a.name).toLowerCase().localeCompare(decodeURI(b.name).toLowerCase());
}).reverse();
// Open first file
var firstFile = app.open(File(inputFiles[0]));
if (checkForArtboardsAndAbort(firstFile)) return;
normalizeBackgroundLayer();
selectAllLayers();
applyLocking(true);
newArtboard();
app.activeDocument.duplicate("Artboard Stack from Files", false);
firstFile.close(SaveOptions.DONOTSAVECHANGES);
var artboardStack = app.activeDocument;
artboardStack.activeLayer.name = app.activeDocument.name.replace(/\.[^\.]+$/, '');
// Process remaining files
for (var i = 1; i < inputFiles.length; i++) {
var remainingFile = app.open(File(inputFiles[i]));
if (checkForArtboardsAndAbort(remainingFile, artboardStack)) return;
normalizeBackgroundLayer();
selectAllLayers();
applyLocking(true);
newArtboard();
dupeToBaseDoc(remainingFile, artboardStack);
}
// Arrange & cleanup
app.beep();
arrangeArtboards();
app.activeDocument.trim(TrimType.TRANSPARENT);
app.preferences.rulerUnits = origUnits;
app.togglePalettes();
app.beep();
selectTool('artboardTool');
selectView("fitOnScreen");
selectView("zoomOut");
alert("Script completed!" + "\r\r" + "Manually arrange the artboards as required (Press Tab to restore the hidden panels).");
///// Functions /////
function normalizeBackgroundLayer() {
var layers = app.activeDocument.layers;
var layer = layers[layers.length - 1];
if (layer.isBackgroundLayer) layer.name = 'Background';
}
function checkForArtboardsAndAbort(docToClose, stackDoc) {
if (findArtboards().length > 0) {
alert("Artboards are not supported as input!");
docToClose.close(SaveOptions.DONOTSAVECHANGES);
if (stackDoc) stackDoc.close(SaveOptions.DONOTSAVECHANGES);
return true;
}
return false;
}
function selectAllLayers() {
try {
var doc = app.activeDocument;
var descriptor = new ActionDescriptor();
var reference2 = new ActionReference();
reference2.putEnumerated(s2t("layer"), s2t("ordinal"), s2t("targetEnum"));
descriptor.putReference(c2t("null"), reference2);
executeAction(s2t("selectAllLayers"), descriptor, DialogModes.NO);
if (doc.layers[doc.layers.length - 1].isBackgroundLayer) {
var descriptor2 = new ActionDescriptor();
var reference = new ActionReference();
reference.putProperty(s2t("layer"), s2t("background"));
descriptor2.putReference(c2t("null"), reference);
descriptor2.putEnumerated(s2t("selectionModifier"), s2t("selectionModifierType"), s2t("addToSelection"));
descriptor2.putBoolean(s2t("makeVisible"), false);
executeAction(s2t("select"), descriptor2, DialogModes.NO);
}
} catch (e) {
alert("selectAllLayers() error!\r" + e + " " + e.line);
}
}
function applyLocking(protectNone) {
try {
var descriptor = new ActionDescriptor();
var descriptor2 = new ActionDescriptor();
var reference = new ActionReference();
reference.putEnumerated(s2t("layer"), s2t("ordinal"), s2t("targetEnum"));
descriptor.putReference(c2t("null"), reference);
descriptor2.putBoolean(s2t("protectNone"), protectNone);
descriptor.putObject(s2t("layerLocking"), s2t("layerLocking"), descriptor2);
executeAction(s2t("applyLocking"), descriptor, DialogModes.NO);
} catch (e) {
alert("applyLocking() error!\r" + e + " " + e.line);
}
}
function arrangeArtboards() {
try {
var abLength = app.activeDocument.layerSets.length;
var theColumns = 1;
var space_x = 250;
var space_y = 250;
while (isNaN(theColumns = prompt("Arrange " + abLength + " artboards into a grid. \rEnter the number of columns (0 = one single row, 1 = one single column, 2 = two columns etc.):", theColumns))) {
if (theColumns === null || theColumns === "") {
alert('Script cancelled!');
return;
}
alert('Please enter a valid whole number!');
}
theColumns = parseInt(theColumns);
if (theColumns === 0) return;
if (theColumns < 1) theColumns = 1;
var theArtboards = [], w = 0, h = 0;
for (var i = 0; i < activeDocument.layerSets.length; i++) {
var r = new ActionReference();
r.putProperty(s2t("property"), s2t("artboardEnabled"));
r.putIdentifier(s2t("layer"), activeDocument.layerSets[i].id);
if (executeActionGet(r).getBoolean(s2t("artboardEnabled"))) {
var r2 = new ActionReference();
r2.putProperty(s2t("property"), s2t("artboard"));
r2.putIdentifier(s2t("layer"), activeDocument.layerSets[i].id);
var rect = executeActionGet(r2).getObjectValue(s2t("artboard")).getObjectValue(s2t("artboardRect"));
var l = rect.getDouble(s2t("left"));
var t = rect.getDouble(s2t("top"));
var rgt = rect.getDouble(s2t("right"));
var b = rect.getDouble(s2t("bottom"));
theArtboards.push([activeDocument.layerSets[i], [l, t, rgt, b]]);
var w1 = rgt - l, h1 = b - t;
if (w1 > w) w = w1;
if (h1 > h) h = h1;
}
}
var rows = Math.floor(theArtboards.length / theColumns);
if (rows * theColumns < theArtboards.length) rows++;
var i = 0;
for (var y = 0; y < rows; y++) {
var pos_y = y * (h + space_y);
for (var x = 0; x < theColumns; x++) {
if (i >= theArtboards.length) break;
activeDocument.activeLayer = theArtboards[i][0];
var pos_x = x * (w + space_x);
move(pos_x - theArtboards[i][1][0], pos_y - theArtboards[i][1][1]);
i++;
}
}
function move(x, y) {
try {
var d = new ActionDescriptor();
var r = new ActionReference();
r.putEnumerated(s2t("layer"), s2t("ordinal"), s2t("targetEnum"));
d.putReference(s2t("null"), r);
var d1 = new ActionDescriptor();
d1.putUnitDouble(s2t("horizontal"), s2t("pixelsUnit"), x);
d1.putUnitDouble(s2t("vertical"), s2t("pixelsUnit"), y);
d.putObject(s2t("to"), s2t("offset"), d1);
executeAction(s2t("move"), d, DialogModes.NO);
} catch (e) {
alert("arrangeArtboards.move() error!\r" + e + " " + e.line);
}
}
} catch (e) {
alert("arrangeArtboards() error!\r" + e + " " + e.line);
}
}
function artboardBackground(artboardColor) {
try {
var ideditArtboardEvent = s2t("editArtboardEvent");
var desc520 = new ActionDescriptor();
var ref129 = new ActionReference();
ref129.putEnumerated(s2t("layer"), s2t("ordinal"), s2t("targetEnum"));
desc520.putReference(c2t("null"), ref129);
var desc521 = new ActionDescriptor();
var idcolor = s2t("color");
var desc523 = new ActionDescriptor();
desc523.putDouble(s2t("red"), 128);
desc523.putDouble(s2t("grain"), 128);
desc523.putDouble(s2t("blue"), 128);
desc521.putObject(idcolor, s2t("RGBColor"), desc523);
desc521.putInteger(s2t("artboardBackgroundType"), artboardColor);
desc520.putObject(s2t("artboard"), s2t("artboard"), desc521);
desc520.putInteger(s2t("changeBackground"), 1);
executeAction(ideditArtboardEvent, desc520, DialogModes.NO);
} catch (e) {
alert("artboardBackground() error!\r" + e + " " + e.line);
}
}
function selectTool(tool) {
try {
var desc9 = new ActionDescriptor();
var ref7 = new ActionReference();
ref7.putClass(s2t(tool));
desc9.putReference(c2t('null'), ref7);
executeAction(c2t('slct'), desc9, DialogModes.NO);
} catch (e) {
alert("selectTool() error!\r" + e + " " + e.line);
}
}
function selectView(menuItem) {
try {
var descriptor = new ActionDescriptor();
var reference = new ActionReference();
reference.putEnumerated(s2t("menuItemClass"), s2t("menuItemType"), s2t(menuItem));
descriptor.putReference(c2t("null"), reference);
executeAction(s2t("select"), descriptor, DialogModes.NO);
} catch (e) {
alert("selectView() error!\r" + e + " " + e.line);
}
}
function newArtboard() {
try {
var descriptor = new ActionDescriptor();
var descriptor2 = new ActionDescriptor();
var reference = new ActionReference();
reference.putClass(s2t("artboardSection"));
descriptor.putReference(c2t("null"), reference);
descriptor.putString(s2t("name"), app.activeDocument.name.replace(/\.[^\.]+$/, ''));
descriptor2.putDouble(s2t("top"), 0.0);
descriptor2.putDouble(s2t("left"), 0.0);
descriptor2.putDouble(s2t("bottom"), app.activeDocument.height.value);
descriptor2.putDouble(s2t("right"), app.activeDocument.width.value);
descriptor.putObject(s2t("artboardRect"), s2t("classFloatRect"), descriptor2);
executeAction(s2t("make"), descriptor, DialogModes.NO);
app.activeDocument.activeLayer.name = app.activeDocument.name.replace(/\.[^\.]+$/, '');
artboardBackground(3);
} catch (e) {
alert("newArtboard() error!\r" + e + " " + e.line);
}
}
function dupeToBaseDoc(openFile, stackDoc) {
try {
var descriptor = new ActionDescriptor();
var reference = new ActionReference();
reference.putEnumerated(s2t("layer"), s2t("ordinal"), s2t("targetEnum"));
descriptor.putReference(c2t("null"), reference);
var reference2 = new ActionReference();
reference2.putName(s2t("document"), stackDoc.name);
descriptor.putReference(s2t("to"), reference2);
descriptor.putInteger(s2t("destinationDocumentID"), stackDoc.id); // dynamic doc ID
descriptor.putString(s2t("name"), app.activeDocument.name.replace(/\.[^\.]+$/, ''));
descriptor.putInteger(s2t("version"), 5);
executeAction(s2t("duplicate"), descriptor, DialogModes.NO);
openFile.close(SaveOptions.DONOTSAVECHANGES);
artboardBackground(3);
app.activeDocument.activeLayer = app.activeDocument.activeLayer.layers[0];
} catch (e) {
alert("dupeToBaseDoc() error!\r" + e + " " + e.line);
}
}
function findArtboards() {
try {
var layers = app.activeDocument.layers;
var artboards = [];
for (var i = 0; i < layers.length; i++) {
if (isArtboard(layers[i].id)) {
artboards.push(layers[i]);
}
}
return artboards;
function isArtboard(layerID) {
try {
var r = new ActionReference();
r.putIdentifier(s2t('layer'), layerID);
var options = executeActionGet(r);
return options.hasKey(s2t('artboard'));
} catch (e) {
return false;
}
}
} catch (e) {
alert("findArtboards() error!\r" + e + " " + e.line);
}
}
})();
} else {
alert('Please close all open files before running this script...');
}
} catch (e) {
alert("Error!\r" + e + " " + e.line);
}
https://prepression.blogspot.com/2017/11/downloading-and-installing-adobe-scripts.html
Already have an account? Login
Enter your E-mail address. We'll send you an e-mail with instructions to reset your password.