Script to Import Photos and Place Them on Artboards in One File
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;
