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

Script to Import Photos and Place Them on Artboards in One File

New Here ,
Jul 14, 2021 Jul 14, 2021

Copy link to clipboard

Copied

Looking for some help with a custom script, hopefully someone has some insight for me. My end goal is to have a single PSD file with a set of images, each placed on their own artboard, with all artboards being a consistent size (in pixels).

 

I can't use a batch process or anything traditional because I need the user to be able to adjust the placement of each photo within the artboard before exporting the resized images.

 

Breaking it down a bit, I'm looking for the script to do the following actions:

  • Have the user input a desired width and height for the artboards to be set to.
    • These values will be the same across all the artboards (only need to prompt once), but it's important that the user is prompted for the dimensions since they will change.
  • Have the user select a set of images from their computer to import (these will be a mix of sizes and aspect ratios, but always flat images.)
  • Import all the images into their own individual artboards within a single PSD, with all artboards sized to the user input values.
  • Scale all the imported images (maintaining their individual aspect ratios) so that the width of the photo is equal to the width of the artboard. Height value can just be whatever it needs to be to keep the images proportional.

 

I don't have a lot of code knowledge but pieced this together from other forum posts. I can get everything done with this except for the last part where the images scale to match artboard width.

 

 

 

// ============================================================================
// Installation:
// 1. Place script in:
// Mac: <hard drive>/Applications/Adobe Photoshop CC#/Presets/Scripts/
// 2. Restart Photoshop
// 3. Choose File > Scripts > filesToArtboards
// ============================================================================

#target photoshop
app.bringToFront();

// Save the current ruler units and set to pixels
var savedRuler = app.preferences.rulerUnits;
app.preferences.rulerUnits = Units.PIXELS;

function cTID(s) {
    return app.charIDToTypeID(s);
}

function sTID(s) {
    return app.stringIDToTypeID(s);
}

function newArtboard(_name, _w, _h) {
    var desc6 = new ActionDescriptor();
    var ref1 = new ActionReference();
    ref1.putClass(sTID('artboardSection'));
    desc6.putReference(cTID('null'), ref1);
    var desc7 = new ActionDescriptor();
    desc7.putString(cTID('Nm  '), _name);
    desc6.putObject(cTID('Usng'), sTID('artboardSection'), desc7);
    var desc8 = new ActionDescriptor();
    desc8.putDouble(cTID('Top '), 0);
    desc8.putDouble(cTID('Left'), 0);
    desc8.putDouble(cTID('Btom'), _h);
    desc8.putDouble(cTID('Rght'), _w);
    desc6.putObject(sTID('artboardRect'), sTID('classFloatRect'), desc8);
    executeAction(cTID('Mk  '), desc6, DialogModes.NO);
}

function main() {

    // User inputs desired canvas specs and files
    var userWidth = prompt("Enter a new width in pixels.", "Enter width.");
    var userHeight = prompt("Enter a new height in pixels.", "Enter height.");
    var fileList = app.openDialog("Select your files"), delta = 0, currentDocWidth = 0;

    if (fileList != null && fileList != "") {
        var doc = app.documents.add(400, 400, 72, "File1");
        for (var i = 0; i < fileList.length; i++) {

            app.open(fileList[i]);

            var idset = stringIDToTypeID("set");
            var desc914 = new ActionDescriptor();
            var idnull = stringIDToTypeID("null");
            var ref474 = new ActionReference();
            var idlayer = stringIDToTypeID("layer");
            var idbackground = stringIDToTypeID("background");
            ref474.putProperty(idlayer, idbackground);
            desc914.putReference(idnull, ref474);
            var idto = stringIDToTypeID("to");
            var desc915 = new ActionDescriptor();
            var idlayer = stringIDToTypeID("layer");
            desc914.putObject(idto, idlayer, desc915);
            executeAction(idset, desc914, DialogModes.NO);

            currentDocWidth = app.activeDocument.width.value;

            newArtboard(app.activeDocument.name, userWidth, userHeight);
            app.activeDocument.activeLayer.duplicate(doc, ElementPlacement.INSIDE);
            app.activeDocument.close(SaveOptions.DONOTSAVECHANGES);
            if (i > 0) {
                app.activeDocument.activeLayer.translate(delta, 0);
            }

            delta2 = delta + currentDocWidth;
        }

        app.runMenuItem(charIDToTypeID("FtOn"));

        var iddelete = stringIDToTypeID("delete");
        var desc733 = new ActionDescriptor();
        var idnull = stringIDToTypeID("null");
        var ref395 = new ActionReference();
        var idlayer = stringIDToTypeID("layer");
        ref395.putName(idlayer, "Layer 0");
        desc733.putReference(idnull, ref395);
        var idlayerID = stringIDToTypeID("layerID");
        var list44 = new ActionList();
        list44.putInteger(5);
        desc733.putList(idlayerID, list44);
        executeAction(iddelete, desc733, DialogModes.NO);

        alert('Done!');
    }
}

main();

// Restore the ruler units
app.preferences.rulerUnits = savedRuler;

 

 

 

TOPICS
Actions and scripting

Views

1.4K

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

correct answers 1 Correct answer

Guide , Aug 01, 2021 Aug 01, 2021

I didn't really dig into your code, just made minimal changes to make it work the way you want:

 

#target photoshop
app.bringToFront();

// Save the current ruler units and set to pixels
var savedRuler = app.preferences.rulerUnits;
app.preferences.rulerUnits = Units.PIXELS;

function cTID(s) {
    return app.charIDToTypeID(s);
}

function sTID(s) {
    return app.stringIDToTypeID(s);
}

function newArtboard(_name, top, left, bottom, right) {
    var desc6 = new ActionDescriptor();
    var ref1 = n
...

Votes

Translate

Translate
Adobe
Community Expert ,
Aug 01, 2021 Aug 01, 2021

Copy link to clipboard

Copied

So have you been able to make headway with this? 

 

I fail to see any placing of Smart Objects in your code. 

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
Guide ,
Aug 01, 2021 Aug 01, 2021

Copy link to clipboard

Copied

I didn't really dig into your code, just made minimal changes to make it work the way you want:

 

#target photoshop
app.bringToFront();

// Save the current ruler units and set to pixels
var savedRuler = app.preferences.rulerUnits;
app.preferences.rulerUnits = Units.PIXELS;

function cTID(s) {
    return app.charIDToTypeID(s);
}

function sTID(s) {
    return app.stringIDToTypeID(s);
}

function newArtboard(_name, top, left, bottom, right) {
    var desc6 = new ActionDescriptor();
    var ref1 = new ActionReference();
    ref1.putClass(sTID('artboardSection'));
    desc6.putReference(cTID('null'), ref1);
    var desc7 = new ActionDescriptor();
    desc7.putString(cTID('Nm  '), _name);
    desc6.putObject(cTID('Usng'), sTID('artboardSection'), desc7);
    var desc8 = new ActionDescriptor();
    desc8.putDouble(cTID('Top '), top);
    desc8.putDouble(cTID('Left'), left);
    desc8.putDouble(cTID('Btom'), bottom);
    desc8.putDouble(cTID('Rght'), right);
    desc6.putObject(sTID('artboardRect'), sTID('classFloatRect'), desc8);
    executeAction(cTID('Mk  '), desc6, DialogModes.NO);
}

function main() {

    // User inputs desired canvas specs and files
    var userWidth = Number (prompt("Enter a new width in pixels.", "Enter width."));
    var userHeight = Number( prompt("Enter a new height in pixels.", "Enter height."));
    var fileList = app.openDialog("Select your files"), delta = 0, currentDocWidth = 0;

    if (fileList != null && fileList != "") {
        var doc = app.documents.add(400, 400, 72, "File1");
        for (var i = 0; i < fileList.length; i++) {

            app.open(fileList[i]);

            var idset = stringIDToTypeID("set");
            var desc914 = new ActionDescriptor();
            var idnull = stringIDToTypeID("null");
            var ref474 = new ActionReference();
            var idlayer = stringIDToTypeID("layer");
            var idbackground = stringIDToTypeID("background");
            ref474.putProperty(idlayer, idbackground);
            desc914.putReference(idnull, ref474);
            var idto = stringIDToTypeID("to");
            var desc915 = new ActionDescriptor();
            var idlayer = stringIDToTypeID("layer");
            desc914.putObject(idto, idlayer, desc915);
            executeAction(idset, desc914, DialogModes.NO);

            currentDocWidth = app.activeDocument.width.value;
            executeAction(sTID('newPlacedLayer'), undefined, DialogModes.NO )
             
            with (activeDocument.activeLayer){
                resize(userWidth/(bounds[2].value)*100,userWidth/(bounds[2].value)*100)
                var center = bounds[1].value+( bounds[3].value- bounds[1].value)/2
                newArtboard(app.activeDocument.name, center-userHeight/2, bounds[0].value, center+userHeight/2, bounds[0].value+userWidth);
            }
        
        activeDocument.activeLayer.duplicate(doc, ElementPlacement.INSIDE);
        app.activeDocument.close(SaveOptions.DONOTSAVECHANGES);
            if (i > 0) {
                app.activeDocument.activeLayer.translate(delta, 0);
            }

            delta2 = delta + currentDocWidth;
        }

        app.runMenuItem(charIDToTypeID("FtOn"));

        var iddelete = stringIDToTypeID("delete");
        var desc733 = new ActionDescriptor();
        var idnull = stringIDToTypeID("null");
        var ref395 = new ActionReference();
        var idlayer = stringIDToTypeID("layer");
        ref395.putName(idlayer, "Layer 0");
        desc733.putReference(idnull, ref395);
        var idlayerID = stringIDToTypeID("layerID");
        var list44 = new ActionList();
        list44.putInteger(5);
        desc733.putList(idlayerID, list44);
        executeAction(iddelete, desc733, DialogModes.NO);

        alert('Done!');
    }
}

main();

// Restore the ruler units
app.preferences.rulerUnits = savedRuler;

 

* I agree with @c.pfaffenbichler and would also prefer to use smart objects. You can remove this line from the code to avoid using them 

 

executeAction(sTID('newPlacedLayer'), undefined, DialogModes.NO )

 

 

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
New Here ,
Aug 25, 2021 Aug 25, 2021

Copy link to clipboard

Copied

This worked, thanks so much!

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
LEGEND ,
Aug 27, 2021 Aug 27, 2021

Copy link to clipboard

Copied

You're nearly whole month too late, so few days ago I marked adjusted code as correct answer.

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
Explorer ,
Jun 24, 2022 Jun 24, 2022

Copy link to clipboard

Copied

LATEST

What would the code be for this exact script but to create a linked image?

 

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