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:
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;
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
...
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.
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 )
Copy link to clipboard
Copied
This worked, thanks so much!
Copy link to clipboard
Copied
You're nearly whole month too late, so few days ago I marked adjusted code as correct answer.
Copy link to clipboard
Copied
What would the code be for this exact script but to create a linked image?