Last active
January 6, 2025 01:15
-
-
Save chrisjreber/fd8e7aadaad78fc471d7eca9d6dc0687 to your computer and use it in GitHub Desktop.
Photoshop Script - create merged before and after image
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Created by Chris Reber - [email protected] | |
// enable double clicking from the Macintosh Finder or the Windows Explorer | |
#target photoshop | |
// Settings | |
var maxHeight = 600; | |
// Script below: | |
// Initialize variables | |
var firstId = ''; | |
var secondId = ''; | |
var mergedId = ''; | |
var firstName = ""; | |
var secondName = ""; | |
var firstWidth = ""; | |
var firstHeight = ""; | |
var secondWidth = ""; | |
var secondHeight = ""; | |
var largestHeight = ""; | |
var totalWidth = ""; | |
var newdocwidth = ""; | |
var mergedName = ""; | |
var theFolder = Folder.selectDialog ("select folder"); | |
var theContent = theFolder.getFiles(/.+\.(?:gif|JPG|[ew]mf|eps|tiff?|psd|pdf|bmp|png)$/i); | |
var secondContent = theFolder.getFiles(/.+\.(?:gif|JPG|[ew]mf|eps|tiff?|psd|pdf|bmp|png)$/i); | |
var doc = ''; | |
var docPath = ''; | |
// Open all images in photoshop | |
for (var i = 0; i < theContent.length; i++) { | |
var theObject = theContent[i]; | |
// open(theObject); | |
if (theContent[i] instanceof File) { | |
open(theObject); | |
// get a reference to the current (active) document and store it in a variable named "doc" | |
var doc = app.activeDocument; | |
var docPath = doc.path+'/merged'; | |
// change the color mode to RGB. Important for resizing GIFs with indexed colors, to get better results | |
doc.changeMode(ChangeMode.RGB); | |
// Create web folder | |
var f = new Folder(doc.path+'/merged'); | |
if (!f.exists) { | |
f.create(); | |
} | |
var active = app.activeDocument; | |
var activeName = active.name; | |
} | |
} | |
// get a reference to the current (active) document and store it in a variable named "doc" | |
var doc = app.activeDocument; | |
loopThroughActiveDocuments(); | |
function loopThroughActiveDocuments(firstName) { | |
var firstName = app.activeDocument.name; | |
var mergedName = firstName; | |
if (firstName.indexOf("after") >= 0) { | |
var secondName = firstName.replace("after", "before"); | |
var mergedName = firstName.replace("after", "merged"); | |
} else if (firstName.indexOf("before") >= 0) { | |
var secondName = firstName.replace("before", "after"); | |
var mergedName = firstName.replace("before", "merged"); | |
} else if (firstName.indexOf("After") >= 0) { | |
var secondName = firstName.replace("After", "Before"); | |
var mergedName = firstName.replace("After", "merged"); | |
} else if (firstName.indexOf("Before") >= 0) { | |
var secondName = firstName.replace("Before", "After"); | |
var mergedName = firstName.replace("Before", "merged"); | |
} else if (firstName.indexOf("AFTER") >= 0) { | |
var secondName = firstName.replace("AFTER", "BEFORE"); | |
var mergedName = firstName.replace("AFTER", "merged"); | |
} else if (firstName.indexOf("BEFORE") >= 0) { | |
var secondName = firstName.replace("BEFORE", "AFTER"); | |
var mergedName = firstName.replace("BEFORE", "merged"); | |
} else { | |
alert( | |
'Matching Image not found, check names so there is a /"before/" and /"after/" name ' + | |
'\n' + | |
"First name: " + | |
firstName + | |
'\n'+ | |
"Matching name: " + | |
secondName | |
); | |
} | |
// get Current ID | |
for (var i = 0; i < app.documents.length; i++) { | |
if(app.documents[i].name == firstName) { | |
firstId = app.documents[i]; | |
app.activeDocument = firstId; | |
app.documents[i].resizeImage(null,UnitValue(maxHeight,"px"),72,ResampleMethod.BICUBIC); | |
firstWidth = app.documents[i].width; | |
firstHeight = app.documents[i].height; | |
} else if(app.documents[i].name == secondName) { | |
secondId = app.documents[i]; | |
app.activeDocument = secondId; | |
app.documents[i].resizeImage(null,UnitValue(maxHeight,"px"),72,ResampleMethod.BICUBIC); | |
secondWidth = app.documents[i].width; | |
secondHeight = app.documents[i].height; | |
app.activeDocument = app.documents[i]; | |
} | |
} | |
createNewMergedImage(); | |
function createNewMergedImage() { | |
totalWidth = firstWidth + secondWidth; | |
app.documents.add(totalWidth, maxHeight, 72, mergedName, NewDocumentMode.RGB, DocumentFill.TRANSPARENT, 1); | |
newdocwidth = app.activeDocument.width; | |
mergedId = app.activeDocument; | |
} | |
var selRegion = Array(); | |
var selRegionSecond = Array(); | |
if (firstName.indexOf("After") >= 0) { | |
var selRegion = Array( | |
Array(firstWidth, 0), | |
Array(totalWidth, 0), | |
Array(totalWidth, maxHeight), | |
Array(firstWidth, maxHeight), | |
Array(firstWidth, 0)) | |
var selRegionSecond = Array( | |
Array(0, 0), | |
Array(secondWidth, 0), | |
Array(secondWidth, maxHeight), | |
Array(0, maxHeight), | |
Array(0, 0)) | |
copyImage(firstId, selRegion); | |
copyImage(secondId, selRegionSecond); | |
} else if (firstName.indexOf("after") >= 0) { | |
var selRegion = Array( | |
Array(firstWidth, 0), | |
Array(totalWidth, 0), | |
Array(totalWidth, maxHeight), | |
Array(firstWidth, maxHeight), | |
Array(firstWidth, 0)) | |
var selRegionSecond = Array( | |
Array(0, 0), | |
Array(secondWidth, 0), | |
Array(secondWidth, maxHeight), | |
Array(0, maxHeight), | |
Array(0, 0)) | |
copyImage(firstId, selRegion); | |
copyImage(secondId, selRegionSecond); | |
} else if (firstName.indexOf("AFTER") >= 0) { | |
var selRegion = Array( | |
Array(firstWidth, 0), | |
Array(totalWidth, 0), | |
Array(totalWidth, maxHeight), | |
Array(firstWidth, maxHeight), | |
Array(firstWidth, 0)) | |
var selRegionSecond = Array( | |
Array(0, 0), | |
Array(secondWidth, 0), | |
Array(secondWidth, maxHeight), | |
Array(0, maxHeight), | |
Array(0, 0)) | |
copyImage(firstId, selRegion); | |
copyImage(secondId, selRegionSecond); | |
} else { | |
var selRegion = Array( | |
Array(0, 0), | |
Array(firstWidth, 0), | |
Array(firstWidth, maxHeight), | |
Array(0, maxHeight), | |
Array(0, 0)) | |
var selRegionSecond = Array( | |
Array(firstWidth, 0), | |
Array(totalWidth, 0), | |
Array(totalWidth, maxHeight), | |
Array(firstWidth, maxHeight), | |
Array(firstWidth, 0)) | |
copyImage(firstId, selRegion); | |
copyImage(secondId, selRegionSecond); | |
} | |
// Copy Images into merged image | |
function copyImage(imageID, imageRegion) { | |
app.activeDocument = imageID; | |
app.activeDocument.selection.selectAll(); | |
app.activeDocument.selection.copy(); | |
app.activeDocument.close(SaveOptions.DONOTSAVECHANGES); | |
app.activeDocument = mergedId; | |
app.activeDocument.selection.select(imageRegion); | |
app.activeDocument.paste(); | |
} | |
setupBoxes(); | |
addText(); | |
saveDoc(); | |
} | |
function saveDoc() { | |
app.activeDocument.flatten(); | |
// our web export options | |
var options = new ExportOptionsSaveForWeb(); | |
options.quality = 100; | |
options.format = SaveDocumentType.JPEG; | |
options.optimized = true; | |
var name = app.activeDocument.name; | |
if (name.toLowerCase().slice(-4) == ".png" || name.toLowerCase().slice(-4) == ".jpg" || name.toLowerCase().slice(-4 == ".psd")) { | |
name = name.slice(0, -4); | |
} | |
var newName = name+'.jpg'; | |
app.activeDocument.exportDocument(File(docPath+'/'+newName),ExportType.SAVEFORWEB,options); | |
app.activeDocument.close(SaveOptions.DONOTSAVECHANGES); | |
if(app.documents.length) { | |
loopThroughActiveDocuments(); | |
} | |
} | |
function addText() { | |
// Add Text | |
var docWidth = app.activeDocument.width; | |
var docWidthCalc = docWidth - 120; | |
// Before text | |
textSettings(); | |
app.activeDocument.activeLayer.textItem.contents="Before"; | |
app.activeDocument.activeLayer.textItem.position = Array(20, 44 ); | |
// After text | |
textSettings(); | |
app.activeDocument.activeLayer.textItem.contents="After"; | |
app.activeDocument.activeLayer.textItem.position = Array(docWidthCalc, 44); | |
} | |
function textSettings() { | |
var myTextLayer = app.activeDocument.artLayers.add(); | |
myTextLayer.kind = LayerKind.TEXT; | |
var textColour = new SolidColor(); | |
//set colour | |
textColour.rgb.hexValue="ffffff"; | |
//set font | |
app.activeDocument.activeLayer.textItem.font = "Georgia"; | |
// set font size | |
app.activeDocument.activeLayer.textItem.size = "40pt"; | |
//set colour | |
app.activeDocument.activeLayer.textItem.color = textColour; | |
} | |
function setupBoxes() { | |
// X,Y | |
// Put the coordinates in clockwise order | |
var boxWidth = 160; | |
var boxHeight = 60; | |
var boxOpacity = 60; | |
var docWidth = app.activeDocument.width; | |
var cleanDocWidth = parseInt(docWidth, 10); | |
// Draw before rectangle | |
DrawShape( | |
[0, 0], | |
[boxWidth, 0], | |
[boxWidth, boxHeight], | |
[0, boxHeight]); | |
app.activeDocument.activeLayer.opacity = boxOpacity; | |
// Draw after rectangle | |
DrawShape( | |
[cleanDocWidth - boxWidth, 0], | |
[cleanDocWidth, 0], | |
[cleanDocWidth, boxHeight], | |
[cleanDocWidth - boxWidth, boxHeight]); | |
app.activeDocument.layers.getByName("Color Fill 2").opacity = boxOpacity; | |
} | |
/* Code by Mike Hale http://www.ps-scripts.com/bb/viewtopic.php?f=14&t=1802&start=15 | |
with small modification by Vladimir Carrer | |
*/ | |
function DrawShape() { | |
var doc = app.activeDocument; | |
var y = arguments.length; | |
var i = 0; | |
var lineArray = []; | |
for (i = 0; i < y; i++) { | |
lineArray[i] = new PathPointInfo; | |
lineArray[i].kind = PointKind.CORNERPOINT; | |
lineArray[i].anchor = arguments[i]; | |
lineArray[i].leftDirection = lineArray[i].anchor; | |
lineArray[i].rightDirection = lineArray[i].anchor; | |
} | |
var lineSubPathArray = new SubPathInfo(); | |
lineSubPathArray.closed = true; | |
lineSubPathArray.operation = ShapeOperation.SHAPEADD; | |
lineSubPathArray.entireSubPath = lineArray; | |
var myPathItem = doc.pathItems.add("myPath", [lineSubPathArray]); | |
var desc88 = new ActionDescriptor(); | |
var ref60 = new ActionReference(); | |
ref60.putClass(stringIDToTypeID("contentLayer")); | |
desc88.putReference(charIDToTypeID("null"), ref60); | |
var desc89 = new ActionDescriptor(); | |
var desc90 = new ActionDescriptor(); | |
var desc91 = new ActionDescriptor(); | |
desc91.putDouble(charIDToTypeID("Rd "), 0.000000); // R | |
desc91.putDouble(charIDToTypeID("Grn "), 0.000000); // G | |
desc91.putDouble(charIDToTypeID("Bl "), 0.000000); // B | |
var id481 = charIDToTypeID("RGBC"); | |
desc90.putObject(charIDToTypeID("Clr "), id481, desc91); | |
desc89.putObject(charIDToTypeID("Type"), stringIDToTypeID("solidColorLayer"), desc90); | |
desc88.putObject(charIDToTypeID("Usng"), stringIDToTypeID("contentLayer"), desc89); | |
executeAction(charIDToTypeID("Mk "), desc88, DialogModes.NO); | |
myPathItem.remove(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment