Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Photoshop script for comparing selected layer comps. The script arranges layer comps on a new document for your viewing pleasure.
// Version 1.1.
// Compare Selected Comps.jsx
// https://gist.github.com/joonaspaakko/adbe208a867f8681afa86032f91f5099?ts=2
// #########
// Changelog
// #########
// v.1.1.
// - Tested in Photoshop CC 2019
// - Added a fallback for when less than 2 layer comps are selected, in which case all layer comps are pushed to the comparison document.
// - Fixed the border issue (cYfAR).
// - Fixed the background fusing issue (Ldfr0q).
// - Known issues: (l2Nrk7) Comparison document's border on the right and bottom doesn't fit inside the dociment. This should only be an issue of you plan to do more with the comparison image than just use it as a temp preview.
// v.1.0.
// - First Version
// - Tested in Photoshop CC 2019
// - Known issues:
// - (cYfAR) The script adds a border around the final comp layer in order to divide the comp layers a bit. If the comp doesn't have a solid background, the stroke will do weird things.
// - (Ldfr0q) The first comp gets fused with the background of the comparison document in some situations...
#target photoshop
var startCompare = false;
var maxHorizontal = 1;
if ( app.documents.length > 0 ) init();
function init() {
var rulerUnits = app.preferences.rulerUnits;
app.preferences.rulerUnits = Units.PIXELS;
var doc = app.activeDocument;
// Duplicate current document
var tempDoc = doc.duplicate( (doc.name + ' (Comparison)') );
// Check layer comp selection.length
// Get layer comps
var layerComps = tempDoc.layerComps;
var selectedComps = [];
var leftOverComps = [];
// Go through every comp
for ( var i=0; i < layerComps.length; i++ ) {
var comp = layerComps[ i ];
// If comp is selected...
if ( comp.selected ) {
selectedComps.push( comp );
}
else {
leftOverComps.push( comp );
}
}
// If more than one comp is selected...
// Remove all leftover layer comps
if ( selectedComps.length > 1 ) {
// Go through every comp
for ( var i=0; i < leftOverComps.length; i++ ) {
var comp = leftOverComps[ i ];
comp.remove();
}
}
dialog( tempDoc.layerComps.length );
if ( !startCompare ) {
// tempDoc.close( SaveOptions.DONOTSAVECHANGES );
}
else if ( startCompare ) {
var tempDocsCollection = [];
// Get layer comps again...
var layerComps = tempDoc.layerComps;
var selectedCompsLength = layerComps.length;
// Go through every comp
for ( var i=0; i < selectedCompsLength; i++ ) {
var comp = layerComps[ i ];
comp.apply();
var thisCompDoc = tempDoc.duplicate( comp.name, true);
thisCompDoc.flatten();
addBlackBorder();
tempDocsCollection.push( thisCompDoc );
app.activeDocument = tempDoc;
}
// Get ready to receive compsies
// Scrub the temp document clean...
tempDoc.flatten();
whiteFill();
// New size to fit the border
tempDoc.resizeCanvas( (tempDoc.width + 1), (tempDoc.height + 1), AnchorPosition.TOPLEFT );
var tempDocWidth = tempDoc.width;
var tempDocHeight = tempDoc.height;
// Find out how many rows are needed
var columns = maxHorizontal;
var rows = 0;
var equalizer = 1;
for ( var i = 0; i < selectedCompsLength; i++ ) {
if ( equalizer == 1 ) {
rows = rows+1;
}
equalizer = equalizer == columns ? 1 : equalizer + 1;
}
var newWidth = tempDocWidth * columns;
var newHeight = tempDocHeight * rows;
tempDoc.resizeCanvas( newWidth, newHeight, AnchorPosition.TOPLEFT );
// Go through every comp document
var cumulativeWidth = 0;
var cumulativeHeight = 0;
var equalizer = 1;
for ( var i=0; i < tempDocsCollection.length; i++ ) {
var compDoc = tempDocsCollection[ i ];
app.activeDocument = compDoc;
compLayerName = compDoc.name;
// Duplicate comp document back to the tempdoc as a single layer...
var compLayer = compDoc.activeLayer.duplicate( tempDoc, ElementPlacement.PLACEATEND );
compDoc.close( SaveOptions.DONOTSAVECHANGES );
app.activeDocument = tempDoc;
compLayer.name = compLayerName;
// Move comp layers side by side
compLayer.translate( cumulativeWidth, cumulativeHeight );
cumulativeWidth = ( equalizer == columns ) ? 0 : cumulativeWidth + tempDocWidth;
cumulativeHeight = ( equalizer == columns ) ? (cumulativeHeight + tempDocHeight) : cumulativeHeight;
equalizer = equalizer == columns ? 1 : equalizer + 1;
}
}
app.preferences.rulerUnits = rulerUnits;
}
function whiteFill() {
function cTID(s) { return app.charIDToTypeID(s); };
function sTID(s) { return app.stringIDToTypeID(s); };
var desc419 = new ActionDescriptor();
desc419.putEnumerated( cTID('Usng'), cTID('FlCn'), cTID('Wht ') );
desc419.putUnitDouble( cTID('Opct'), cTID('#Prc'), 100.000000 );
desc419.putEnumerated( cTID('Md '), cTID('BlnM'), cTID('Nrml') );
executeAction( cTID('Fl '), desc419, DialogModes.NO );
}
function selectAllLayers() {
// Makes background layer a normal layer.
// Otherwise it wouldn't be selected in the next step.
var firstLayer = app.activeDocument.layers[ app.activeDocument.layers.length - 1 ];
if ( firstLayer.isBackgroundLayer ) firstLayer.isBackgroundLayer = false;
// Select all layers...
var actionDescriptor = new ActionDescriptor();
var actionReference = new ActionReference();
actionReference.putEnumerated( charIDToTypeID( "Lyr " ), charIDToTypeID( "Ordn" ), charIDToTypeID( "Trgt" ) );
actionDescriptor.putReference( charIDToTypeID( "null" ), actionReference );
executeAction( stringIDToTypeID( "selectAllLayers" ), actionDescriptor, DialogModes.NO );
}
function addBlackBorder() {
function cTID(s) { return app.charIDToTypeID(s); };
function sTID(s) { return app.stringIDToTypeID(s); };
var desc97 = new ActionDescriptor();
desc97.putBoolean( cTID('Rltv'), true );
desc97.putUnitDouble( cTID('Wdth'), cTID('#Pxl'), 2.000000 );
desc97.putUnitDouble( cTID('Hght'), cTID('#Pxl'), 2.000000 );
desc97.putEnumerated( cTID('Hrzn'), cTID('HrzL'), cTID('Cntr') );
desc97.putEnumerated( cTID('Vrtc'), cTID('VrtL'), cTID('Cntr') );
desc97.putEnumerated( sTID('canvasExtensionColorType'), sTID('canvasExtensionColorType'), cTID('Clr ') );
var desc98 = new ActionDescriptor();
desc98.putUnitDouble( cTID('H '), cTID('#Ang'), 0.000000 );
desc98.putDouble( cTID('Strt'), 0.000000 );
desc98.putDouble( cTID('Brgh'), 0.000000 );
desc97.putObject( sTID('canvasExtensionColor'), cTID('HSBC'), desc98 );
executeAction( cTID('CnvS'), desc97, DialogModes.NO );
}
function dialog( compsLength ) {
/*
Code for Import https://scriptui.joonas.me — (Triple click to select):
{"activeId":0,"items":{"item-0":{"id":0,"type":"Dialog","parentId":false,"style":{"varName":null,"text":"Compare Selected Comps.jsx","preferredSize":[0,0],"margins":29,"orientation":"row","spacing":10,"alignChildren":["right","center"]}},"item-1":{"id":1,"type":"EditText","parentId":0,"style":{"varName":null,"text":"4","preferredSize":[52,0],"alignment":null,"helpTip":null}},"item-2":{"id":2,"type":"StaticText","parentId":0,"style":{"varName":null,"text":"Max comps side by side:","justify":"left","preferredSize":[0,0],"alignment":null,"helpTip":null}},"item-5":{"id":5,"type":"Button","parentId":0,"style":{"varName":null,"text":"Cancel","justify":"center","preferredSize":[0,0],"alignment":null,"helpTip":null}},"item-6":{"id":6,"type":"Button","parentId":0,"style":{"varName":null,"text":"Compare","justify":"center","preferredSize":[0,0],"alignment":null,"helpTip":null}}},"order":[0,2,1,6,5]}
*/
// DIALOG
// ======
var dialog = new Window("dialog");
dialog.text = "Compare Selected Comps.jsx";
dialog.orientation = "row";
dialog.alignChildren = ["right","center"];
dialog.spacing = 10;
dialog.margins = 29;
var statictext1 = dialog.add("statictext");
statictext1.text = "Max comps side by side:";
var edittext1 = dialog.add("edittext");
edittext1.text = "4";
edittext1.preferredSize.width = 52;
var button1 = dialog.add("button", undefined, undefined, {name:"ok"});
button1.text = "Compare";
button1.justify = "center";
var button2 = dialog.add("button", undefined, undefined, {name:"cancel"});
button2.text = "Cancel";
button2.justify = "center";
// Customization
edittext1.text = compsLength;
edittext1.active = true;
button1.onClick = function() {
startCompare = true;
maxHorizontal = parseInt( edittext1.text, 10);
dlg.close();
}
button2.onClick = function() {
dlg.close();
}
dialog.show();
}
@joonaspaakko
Copy link
Author

joonaspaakko commented Dec 11, 2020

Gif showing how the script works
Compare Selected Comps

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment