Script to import multiple images (from one folder) on single artboard with fixed height and spacing.

New Here ,
Jun 10, 2022 Jun 10, 2022

Copy link to clipboard

Copied

Hi,

I need a script which can perform following task

1. Import images from folder heirarchy for eg. Images folder may have multiple folders and each folder may have multiple images 

Images folder>

  • Folder A > multiple images
  • Folder B > multiple images

 

2.  Create multiple artboards from imported images and fit the images proportionally (height 1020 px x width proptionally). I need to fit images with spacing of 40 px from each side of artboard.

Images from folder A will be placed on 1st artboard, images from folder B will be placed on 2nd artboard

 

3. Create slice on each artboard of 1120x640 px

3. Save the .psd document.

 

Thnaks in advance!

TOPICS
Actions and scripting , macOS , Windows

Views

104

Likes

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 Professional ,
Jun 10, 2022 Jun 10, 2022

Copy link to clipboard

Copied

Do you know how to script (JS, VB, AS)?

 

Your request is not trivial (at least for me).

 

Perhaps providing screenshots of your folders/files and screenshots of the final files in Photoshop would help those that can help you.

 

Good luck!

 

Likes

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
New Here ,
Jun 11, 2022 Jun 11, 2022

Copy link to clipboard

Copied

Hi @Stephen_A_Marsh, thanks for your help.

Find attached screenshot for reference for folder structure and desired output in photoshop.

In the 1st qustion, i wrongly put this- "(height 1020 px x width proptionally)", instead it should be "imported images height should be 600px and width proportionally."

 

I am not much into the scripting but i have knwoledge of OOPs and Javascript a bit.
I think there should be two loops one which goes through every sub folder and one loop to go through all the images inside the sub folder. 

Let me know anything else's required to understand the problem.

Thanks in advance! 🙂

Likes

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 Professional ,
Jun 11, 2022 Jun 11, 2022

Copy link to clipboard

Copied

@rupeshl77305213 – Thanks, as I hinted, I may not be the person to help you with all of the code, but I can help to extract the required information for those that can help. As I mentioned, your request is not trivial and I personally hate working with artboards as they add a lot of complexity.

 

So rather than reinventing the wheel, I'd start by searching the forum or the web for other scripts that can combine images from an input folder to an artboard.

 

Here is one example that I created for stacking images horizontally:

https://community.adobe.com/t5/photoshop-ecosystem-discussions/how-to-combine-images-horizontally-vi...

 

Post links to the code to help others to help you. You may be lucky and have somebody write a bespoke script for you, but if not, it is easier to combine existing code into a new whole.

 

Once you have this working for a single input folder, the script could be expanded to recurse into subdirectories under a root directory.

Likes

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 Professional ,
Jun 11, 2022 Jun 11, 2022

Copy link to clipboard

Copied

@rupeshl77305213 – I get that you want the layer content to be 600 px high + 20 px top + 20 px bottom for 640 px high total, but what about the width?

 

What if the number of proportionally resized layers creates an artboard width that is larger than your example of 1120 px wide, is that OK? Or was that just an example of what the 3 images shown would produce with padding?

 

So the width can be anything, it is not maxed to 1120 px?

 

Likes

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 Professional ,
Jun 11, 2022 Jun 11, 2022

Copy link to clipboard

Copied

@rupeshl77305213  – I modified an existing script that I created for stepping and repeating a layer.

 

It only works with a single root-level input folder with no recursive processing into sub-directories, which adds extra complexity.

 

Not exactly what you are looking for, but perhaps better than nothing... You can run this script over each folder and save to PSD and then combine each separate artboard into a single file. 

 

 

/*
Horizontally stacked files to artboard from folder.jsx
https://community.adobe.com/t5/photoshop-ecosystem-discussions/script-to-import-multiple-images-from-one-folder-on-single-artboard-with-fixed-height-and-spacing/m-p/12998325
v1.0 - Stephen Marsh, 11th June 2022
*/

#target photoshop

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

    (function () {

        // Save the current dialog display settings
        var savedDisplayDialogs = app.displayDialogs;
        app.displayDialogs = DialogModes.NO;

        // 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();

        // Process the first "base" file
        var firstFile = app.open(File(inputFiles[0]));
        var idnewPlacedLayer = stringIDToTypeID( "newPlacedLayer" );
        executeAction( idnewPlacedLayer, undefined, DialogModes.NO );
        var firstFileName = app.activeDocument.name;
        app.activeDocument.duplicate("Horizontal-Stacker", false);
        activeDocument.resizeImage(null, 600, null, ResampleMethod.BICUBIC);
        firstFile.close(SaveOptions.DONOTSAVECHANGES);
        var docStack = app.documents[0];
        app.activeDocument = docStack;
        docStack.activeLayer.name = firstFileName;

        // 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 idnewPlacedLayer = stringIDToTypeID( "newPlacedLayer" );
            executeAction( idnewPlacedLayer, undefined, DialogModes.NO );
            var fileName = remainingFiles.name;
            remainingFiles.activeLayer.name = fileName;
            remainingFiles.resizeImage(null, 600, null, ResampleMethod.BICUBIC);
            remainingFiles.layers[0].duplicate(docStack, ElementPlacement.PLACEATBEGINNING);
            remainingFiles.close(SaveOptions.DONOTSAVECHANGES);
        }

        // Variables for step & repeat
        var doc = app.activeDocument;
        var repeatX = doc.layers.length - 1;
        var gapX = 20 * repeatX;
        var outerMargin = 40;

        // Convert to % for relative canvas resize
        var newCanvasX = repeatX * 100;

        // Relative % canvas resize
        relativeCanvasSizePercent(true, newCanvasX, 0);
        // Absolute px canvas resize
        doc.resizeCanvas(doc.width + gapX, doc.height, AnchorPosition.MIDDLELEFT);

        // Layer loop
        for (var i = 0; i < repeatX; i++) {
            // Select front layer
            doc.activeLayer = app.activeDocument.layers[0];
        }

        // Align X to right
        align2SelectAll('AdRg');
        app.runMenuItem(stringIDToTypeID('selectAllLayers'));
        if (repeatX > 1) {
            // Distribute X: for 3 or more layers
            var iddistort = stringIDToTypeID("distort");
            var desc788 = new ActionDescriptor();
            var idnull = stringIDToTypeID("null");
            var ref298 = new ActionReference();
            var idlayer = stringIDToTypeID("layer");
            var idordinal = stringIDToTypeID("ordinal");
            var idtargetEnum = stringIDToTypeID("targetEnum");
            ref298.putEnumerated(idlayer, idordinal, idtargetEnum);
            desc788.putReference(idnull, ref298);
            var idusing = stringIDToTypeID("using");
            var idalignDistributeSelector = stringIDToTypeID("alignDistributeSelector");
            var idADSDistH = stringIDToTypeID("ADSDistH");
            desc788.putEnumerated(idusing, idalignDistributeSelector, idADSDistH);
            executeAction(iddistort, desc788, DialogModes.NO);
        }

        // Outer margin absolute px canvas resize
        doc.resizeCanvas(doc.width + outerMargin, doc.height + outerMargin, AnchorPosition.MIDDLECENTER);

        // Make solid fill layer to canvas
        makeSolidFillLayer("_artboardSizeHack", 255, 255, 255);

        // Select all layers
        app.runMenuItem(stringIDToTypeID("selectAllLayers"));

        // Artboard from layers
        artboardFromSelectedLayers(20, 21, "Artboard 1");

        // Remove solid fill layer
        deleteSolidFillLayer();

        // Set the artboard colour
        abTrans();

        // End of Script Notification
        app.beep();

        // Restore the dialogs
        app.displayDialogs = savedDisplayDialogs;

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


        // Functions

        function deleteSolidFillLayer() {
            var s2t = function (s) {
                return app.stringIDToTypeID(s);
            };
            var descriptor = new ActionDescriptor();
            var list = new ActionList();
            var reference = new ActionReference();
            reference.putName(s2t("layer"), "_artboardSizeHack");
            descriptor.putReference(s2t("null"), reference);
            list.putInteger(5);
            descriptor.putList(s2t("layerID"), list);
            executeAction(s2t("delete"), descriptor, DialogModes.NO);
        }

        function artboardFromSelectedLayers(layerSectionStart, layerSectionEnd, name2) {
            var s2t = function (s) {
                return app.stringIDToTypeID(s);
            };
            var descriptor = new ActionDescriptor();
            var reference = new ActionReference();
            var reference2 = new ActionReference();
            reference.putClass(s2t("artboardSection"));
            descriptor.putReference(s2t("null"), reference);
            reference2.putEnumerated(s2t("layer"), s2t("ordinal"), s2t("targetEnum"));
            descriptor.putReference(s2t("from"), reference2);
            descriptor.putInteger(s2t("layerSectionStart"), layerSectionStart);
            descriptor.putInteger(s2t("layerSectionEnd"), layerSectionEnd);
            descriptor.putString(s2t("name"), name2);
            executeAction(s2t("make"), descriptor, DialogModes.NO);
        }

        function makeSolidFillLayer(name2, red, grain, blue) {
            var s2t = function (s) {
                return app.stringIDToTypeID(s);
            };
            var descriptor = new ActionDescriptor();
            var descriptor2 = new ActionDescriptor();
            var descriptor3 = new ActionDescriptor();
            var descriptor4 = new ActionDescriptor();
            var reference = new ActionReference();
            reference.putClass(s2t("contentLayer"));
            descriptor.putReference(s2t("null"), reference);
            descriptor2.putString(s2t("name"), name2);
            descriptor4.putDouble(s2t("red"), red);
            descriptor4.putDouble(s2t("grain"), grain);
            descriptor4.putDouble(s2t("blue"), blue);
            descriptor3.putObject(s2t("color"), s2t("RGBColor"), descriptor4);
            descriptor2.putObject(s2t("type"), s2t("solidColorLayer"), descriptor3);
            descriptor.putObject(s2t("using"), s2t("contentLayer"), descriptor2);
            executeAction(s2t("make"), descriptor, DialogModes.NO);
        }

        function align2SelectAll(method) {
            /* 
            AdLf = Align Left
            AdRg = Align Right
            AdCH = Align Centre Horizontal
            AdTp = Align Top
            AdBt = Align Bottom
            AdCV = Align Centre Vertical
            */
            app.activeDocument.selection.selectAll();
            var desc = new ActionDescriptor();
            var ref = new ActionReference();
            ref.putEnumerated(charIDToTypeID("Lyr "), charIDToTypeID("Ordn"), charIDToTypeID("Trgt"));
            desc.putReference(charIDToTypeID("null"), ref);
            desc.putEnumerated(charIDToTypeID("Usng"), charIDToTypeID("ADSt"), charIDToTypeID(method));
            try {
                executeAction(charIDToTypeID("Algn"), desc, DialogModes.NO);
            } catch (e) {}
            app.activeDocument.selection.deselect();
        }

        function relativeCanvasSizePercent(relative, width, height) {
            var s2t = function (s) {
                return app.stringIDToTypeID(s);
            };
            var descriptor = new ActionDescriptor();
            descriptor.putBoolean(s2t("relative"), relative);
            descriptor.putUnitDouble(s2t("width"), s2t("percentUnit"), width);
            descriptor.putUnitDouble(s2t("height"), s2t("percentUnit"), height);
            descriptor.putEnumerated(s2t("horizontal"), s2t("horizontalLocation"), s2t("left"));
            descriptor.putEnumerated(s2t("vertical"), s2t("verticalLocation"), s2t("top"));
            executeAction(s2t("canvasSize"), descriptor, DialogModes.NO);
        }

        function abTrans() {
            /* 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);

            // 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, 1);

            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...');
}

 

 

Likes

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
New Here ,
Jun 13, 2022 Jun 13, 2022

Copy link to clipboard

Copied

Hi @Stephen_A_Marsh 

Thank you so much for your efforts.

Yes, this soution is half way through to the final output.

Following things we should consider here:

1. The arboard size has to be 1120 x 640 px fixed, currently the width is variable according to the sku's width.

2. I work on approx 80-100 images in every project, the current solution is taking same time as i do the task mannually by duplicating old psd and draggnig and droping the SKUs images and resizing them altogether.

3. We have to find out the solution to iterate through root folder. If not using javascript then any using other language if possible or combining this script with droplet or action or set of actions to iterate through all the folders. Can you please suggest something? 

 

Appreaciate your efforts 🙂

 

Likes

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 Professional ,
Jun 13, 2022 Jun 13, 2022

Copy link to clipboard

Copied

LATEST

@rupeshl77305213 wrote:

 

1. The arboard size has to be 1120 x 640 px fixed, currently the width is variable according to the sku's width.

 

So how do you know that the input folder will contain an unknown quantity of images that add together to fall under this canvas size when resized to 600px high?

 

 


3. We have to find out the solution to iterate through root folder. ... Can you please suggest something? 

 

I don't believe that recursing through the child folders under the parent folder is the real issue in itself. It is creating new artboards each time based on the content of each new input folder, however, that may just be my limitation in seeing a way to work with this.

 

Apart from learning how to script yourself, sadly, no – I can't suggest something.

 

This is not easy for me, apart from minor tweaks, I have taken this pretty much as far as I can.

 

Perhaps somebody else would like to step up?

 

Good luck with your project.

 

Likes

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