• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
2

How to Export to artboards using the photoshop variables

New Here ,
May 06, 2024 May 06, 2024

Copy link to clipboard

Copied

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.

TOPICS
Actions and scripting

Views

80

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Adobe
Community Expert ,
May 07, 2024 May 07, 2024

Copy link to clipboard

Copied

Variables/Data-driven graphics were around long before artboards. As you know, they only export to PSD as a native feature.

 

I am aware of an old custom script from the late Mike Hale to export directly to JPEG, so it is possible to extend variables via scripting. So perhaps somebody could directly script data sets to artboards.

 

You can make a feature request by making  a new post and labelling it as an “idea" rather than a discussion:

 

https://community.adobe.com/t5/photoshop-ecosystem-ideas/how-do-i-write-a-feature-request/idi-p/1238...

 

I can envisage an indirect script that would take the output from the variable PSD exports and create a new layered file with an artboard for each separate PSD created from the variables.

 

Artboards have sizes, names, spacing, child layers/groups etc. They are sometimes complex for automation and I have a tough time working with them for scripting their position and spacing. The problem with my approach is that all artboards would be stacked one on top the other, they would not be arranged in a grid or vertically or horizontally. EDIT: Solved!

 

Thinking about this, it would probably just be easier to ignore the built-in variable feature and script the creation of artboard content from a CSV file.

 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
May 07, 2024 May 07, 2024

Copy link to clipboard

Copied

LATEST

@Lucas347275917lt9 

 

Assuming that you have a folder containing 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 and v2024.

 

/*
Stack Layered Files to Artboards.jsx
v1.0 - 8th May 2024, Stephen Marsh
v1.1 - 9th May 2024, Added code to set the artboard background colour
https://community.adobe.com/t5/photoshop-ecosystem-discussions/how-to-export-to-artboards-using-the-photoshop-variables/td-p/14603249
Based on:
https://community.adobe.com/t5/photoshop-ecosystem/combining-mulitple-photoshop-files-into-1-photoshop-file-with-artboards/td-p/12218200
NOTE: Works with layered files
*/

#target photoshop

try {

    if (app.documents.length === 0) {

        (function () {

            // Capture the original ruler units and set the ruler units to pixels
            var origUnits = app.preferences.rulerUnits;
            app.preferences.rulerUnits = Units.PIXELS;

            // Select the input folder
            var inputFolder = Folder.selectDialog('Please select the input folder:');
            // Test if Cancel button returns null, then do nothing
            if (inputFolder === null) {
                app.beep();
                return;
            }

            // Supported file formats
            var inputFiles = inputFolder.getFiles(/\.(jpg|jpeg|tif|tiff|png|psd|psb)$/i);
            // Alpha numeric sort
            inputFiles.sort().reverse();

            // Process the first "base" file
            var firstFile = app.open(File(inputFiles[0]));
            var firstFileName = app.activeDocument.name.replace(/\.[^\.]+$/, '');
            selectAllLayers();
            convertToSmartObject();
            app.activeDocument.duplicate("Artboard Stack from Files", false);
            firstFile.close(SaveOptions.DONOTSAVECHANGES);
            var artboardStack = app.activeDocument;
            app.activeDocument = artboardStack;
            artboardStack.activeLayer.name = firstFileName;
            // Call the 1st artboard function
            artBoardFromLayers();
            artBoardBackground(3);
            artboardStack.activeLayer.name = firstFileName;
            app.activeDocument.activeLayer = app.activeDocument.activeLayer.layers[0];
            convertSmartObjectToLayers();
            ungroupLayers();

            // Process the remaining file layers to the "base" file
            for (var i = 1; i < inputFiles.length; i++) {
                var remainingFiles = app.open(File(inputFiles[i]));
                var fileName = remainingFiles.name;
                remainingFiles.activeLayer.name = fileName;
                selectAllLayers();
                convertToSmartObject();
                remainingFiles.layers[0].duplicate(artboardStack, ElementPlacement.PLACEATBEGINNING);
                remainingFiles.close(SaveOptions.DONOTSAVECHANGES);
                // Call the 2nd artboard function
                artBoardFromLayers2();
                artBoardBackground(3);
                app.activeDocument.activeLayer = app.activeDocument.activeLayer.layers[0];
                convertSmartObjectToLayers();
                ungroupLayers();
            }

            // Arrange the artboards to a grid
            arrangeArtboards();

            // Return the original ruler units
            app.preferences.rulerUnits = origUnits;

            // End of script
            app.beep();


            ///// Functions /////

            function artBoardFromLayers() {
                app.runMenuItem(stringIDToTypeID("artboardFromLayersEvent"));
            }

            function artBoardFromLayers2() {
                var artboardFromLayers = stringIDToTypeID("make");
                var desc1156 = new ActionDescriptor();
                var idnull = stringIDToTypeID("null");
                var ref558 = new ActionReference();
                var idartboardSection = stringIDToTypeID("artboardSection");
                ref558.putClass(idartboardSection);
                desc1156.putReference(idnull, ref558);
                var idfrom = stringIDToTypeID("from");
                var ref559 = new ActionReference();
                var idlayer = stringIDToTypeID("layer");
                var idordinal = stringIDToTypeID("ordinal");
                var idtargetEnum = stringIDToTypeID("targetEnum");
                ref559.putEnumerated(idlayer, idordinal, idtargetEnum);
                desc1156.putReference(idfrom, ref559);
                var idusing = stringIDToTypeID("using");
                var desc1157 = new ActionDescriptor();
                var idname = stringIDToTypeID("name");
                var abName = fileName.replace(/\.[^\.]+$/, '');
                desc1157.putString(idname, abName);
                var idartboardSection = stringIDToTypeID("artboardSection");
                desc1156.putObject(idusing, idartboardSection, desc1157);
                var idlayerSectionStart = stringIDToTypeID("layerSectionStart");
                desc1156.putInteger(idlayerSectionStart, 3);
                var idlayerSectionEnd = stringIDToTypeID("layerSectionEnd");
                desc1156.putInteger(idlayerSectionEnd, 4);
                var idname = stringIDToTypeID("name");
                desc1156.putString(idname, abName);
                executeAction(artboardFromLayers, desc1156, DialogModes.NO);
            }

            function ungroupLayers() {
                var iddelete = stringIDToTypeID("delete");
                var desc546 = new ActionDescriptor();
                var idnull = stringIDToTypeID("null");
                var ref177 = new ActionReference();
                var idlayer = stringIDToTypeID("layer");
                var idordinal = stringIDToTypeID("ordinal");
                var idtargetEnum = stringIDToTypeID("targetEnum");
                ref177.putEnumerated(idlayer, idordinal, idtargetEnum);
                desc546.putReference(idnull, ref177);
                var iddeleteContained = stringIDToTypeID("deleteContained");
                desc546.putBoolean(iddeleteContained, false);
                executeAction(iddelete, desc546, DialogModes.NO);
            }

            function convertToSmartObject() {
                var idnewPlacedLayer = stringIDToTypeID("newPlacedLayer");
                executeAction(idnewPlacedLayer, undefined, DialogModes.NO);
            }

            function convertSmartObjectToLayers() {
                var idplacedLayerConvertToLayers = stringIDToTypeID("placedLayerConvertToLayers");
                executeAction(idplacedLayerConvertToLayers, undefined, DialogModes.NO);
            }

            function selectAllLayers() {
                var c2t = function (s) {
                    return app.charIDToTypeID(s);
                };
                var s2t = function (s) {
                    return app.stringIDToTypeID(s);
                };
                var descriptor = new ActionDescriptor();
                var descriptor2 = new ActionDescriptor();
                var reference = new ActionReference();
                var reference2 = new ActionReference();
                reference2.putEnumerated(s2t("layer"), s2t("ordinal"), s2t("targetEnum"));
                descriptor.putReference(c2t("null"), reference2);
                executeAction(s2t("selectAllLayers"), descriptor, DialogModes.NO);
                // Add the Background layer if it exists
                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);
            }

            function arrangeArtboards() {
                /*
                https://community.adobe.com/t5/photoshop-ecosystem-discussions/script-to-arrange-artboard-in-grid/td-p/9417657
                by r-bin
                */

                var abLength = app.activeDocument.layerSets.length;

                var clms = 2;
                var space_x = 50;
                var space_y = 50;

                clms = prompt("Arrange " + abLength + " artboards into a grid. \r Enter the number of columns:", clms);
                if (clms) {
                    clms = Number(clms);
                    var arts = new Array();
                    var w = 0;
                    var h = 0;
                    for (var i = 0; i < activeDocument.layerSets.length; i++) {
                        var r = new ActionReference();
                        r.putProperty(stringIDToTypeID("property"), stringIDToTypeID("artboardEnabled"));
                        r.putIdentifier(stringIDToTypeID("layer"), activeDocument.layerSets[i].id);

                        if (executeActionGet(r).getBoolean(stringIDToTypeID("artboardEnabled"))) {
                            var r = new ActionReference();
                            r.putProperty(stringIDToTypeID("property"), stringIDToTypeID("artboard"));
                            r.putIdentifier(stringIDToTypeID("layer"), activeDocument.layerSets[i].id);

                            var rect = executeActionGet(r).getObjectValue(stringIDToTypeID("artboard")).getObjectValue(stringIDToTypeID("artboardRect"));

                            var l = rect.getDouble(stringIDToTypeID("left"));
                            var t = rect.getDouble(stringIDToTypeID("top"));
                            var r = rect.getDouble(stringIDToTypeID("right"));
                            var b = rect.getDouble(stringIDToTypeID("bottom"));

                            arts.push([activeDocument.layerSets, [l, t, r, b]]);

                            var w1 = r - l;
                            var h1 = b - t;

                            if (w1 > w) w = w1;
                            if (h1 > h) h = h1;
                        }
                    }
                    var rows = Math.floor(arts.length / clms);
                    if (rows * clms < arts.length) ++rows;
                    var i = 0;
                    for (var y = 0; y < rows; y++) {
                        var pos_y = y * (h + space_y);
                        for (var x = 0; x < clms; x++) {
                            if (i >= arts.length) break;
                            activeDocument.activeLayer = arts[x][0][i];
                            var pos_x = x * (w + space_x);
                            move(pos_x - arts[x][1][0], pos_y - arts[x][1][1])
                            i += 1;
                        }
                    }
                }

                function move(x, y) {
                    try {
                        var d = new ActionDescriptor();
                        var r = new ActionReference();
                        r.putEnumerated(stringIDToTypeID("layer"), stringIDToTypeID("ordinal"), stringIDToTypeID("targetEnum"));
                        d.putReference(stringIDToTypeID("null"), r);
                        var d1 = new ActionDescriptor();
                        d1.putUnitDouble(stringIDToTypeID("horizontal"), stringIDToTypeID("pixelsUnit"), x);
                        d1.putUnitDouble(stringIDToTypeID("vertical"), stringIDToTypeID("pixelsUnit"), y);
                        d.putObject(stringIDToTypeID("to"), stringIDToTypeID("offset"), d1);
                        executeAction(stringIDToTypeID("move"), d, DialogModes.NO);
                    } catch (e) {
                        throw (e);
                    }
                }
            }

            function artBoardBackground(artBoardColor) {
            /* https://community.adobe.com/t5/photoshop-ecosystem-discussions/setting-artboard-background-to-transparent/m-p/12438157 */
            var ideditArtboardEvent = stringIDToTypeID("editArtboardEvent");
            var desc520 = new ActionDescriptor();
            var idnull = stringIDToTypeID("null");
            var ref129 = new ActionReference();
            var idlayer = stringIDToTypeID("layer");
            var idordinal = stringIDToTypeID("ordinal");
            var idtargetEnum = stringIDToTypeID("targetEnum");
            var desc521 = new ActionDescriptor();
            var idartboard = stringIDToTypeID("artboard");
            var idchangeBackground = stringIDToTypeID("changeBackground");
            ref129.putEnumerated(idlayer, idordinal, idtargetEnum);
            desc520.putReference(idnull, ref129);
            // Static options for "Other" idartboardBackgroundType = 4
            var idcolor = stringIDToTypeID("color");
            var desc523 = new ActionDescriptor();
            var idred = stringIDToTypeID("red");
            desc523.putDouble(idred, 128.000000);
            var idgrain = stringIDToTypeID("grain");
            desc523.putDouble(idgrain, 128.000000);
            var idblue = stringIDToTypeID("blue");
            desc523.putDouble(idblue, 128.000000);
            var idRGBColor = stringIDToTypeID("RGBColor");
            desc521.putObject(idcolor, idRGBColor, desc523);
            var idartboardBackgroundType = stringIDToTypeID("artboardBackgroundType");
            // putInteger 1 = White, 2 = Black, 3 = Transparent, 4 = Other
            desc521.putInteger(idartboardBackgroundType, artBoardColor);
            desc520.putObject(idartboard, idartboard, desc521);
            desc520.putInteger(idchangeBackground, 1);
            executeAction(ideditArtboardEvent, desc520, DialogModes.NO);
        }

        })();

    } 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

 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines