Skip to content

Instantly share code, notes, and snippets.

@kardolus
Last active March 11, 2016 17:47
Show Gist options
  • Save kardolus/30e749419221447cdd6e to your computer and use it in GitHub Desktop.
Save kardolus/30e749419221447cdd6e to your computer and use it in GitHub Desktop.
Benchmark PhotoShop Actions in Extended Javascript
var ORIGINAL = 'E:\\FILES\\30425\\ORIGINAL'; // input folder
var PRE_PROCESSED = 'E:\\FILES\\30425\\PRE_PROCESSED'; // output folder
var benchmarkFile = 'E:\\LOG\\benchmark.csv'; // time messurements
/**
* The functions you combine in this method will be benchmark tested
* @param fileName - Target file
*/
function psAction(fileName){
psOpenFile(fileName);
psInsertMeta("30425");
psGetBackgroundLayer();
psRenameLayer("Background", "Original");
psCopyLayer("Original", "Retouched");
psLockLayer("Original");
psMakeLayerInvisible("Original");
psAddLayer("Background");
psMakeBackgroundLayer("Background");
psAddClippingPath("Path 1");
psSetAsActiveLayer("Retouched");
psCloseFile();
}
function psOpenFile(fileName){
// Only want to open non-hidden files (and no folders)
if ((fileName instanceof File) && (fileName.hidden == false)) {
// Open in Photoshop
open(fileName);
}
}
function psCloseFile(){
var doc = activeDocument;
app.activeDocument.saveAs(new File(PRE_PROCESSED));
doc.close(SaveOptions.SAVECHANGES);
}
function psAddNLayers(nLayers){ // run on tif files
var doc = activeDocument;
for(var i = 0; i < nLayers; i++){
doc.backgroundLayer.duplicate();
}
}
function psSaveTiff(){
var doc = activeDocument;
var tiffSaveOptions = new TiffSaveOptions();
tiffSaveOptions.byteOrder = ByteOrder.MACOS;
tiffSaveOptions.embedColorProfile = true;
tiffSaveOptions.layers = true;
tiffSaveOptions.imageCompression = TIFFEncoding.TIFFLZW;
tiffSaveOptions.layerCompression = LayerCompression.RLE;
doc.saveAs(doc.path, tiffSaveOptions, false, Extension.LOWERCASE);
}
function psResize(percentage){
var doc = activeDocument;
doc.resizeImage(UnitValue(percentage,"%"), UnitValue(percentage,"%"), // width, height
null, ResampleMethod.AUTOMATIC); // dpi
}
function psFlatten(){
var doc = activeDocument;
doc.flatten();
}
/**
* @param profile - The new color profile, such as: sRGB IEC61966-2.1
*/
function psRecolor(profile){
var doc = activeDocument;
doc.convertProfile(profile, Intent.RELATIVECOLORIMETRIC, true, true );
}
function psSetDefaults(){
app.preferences.rulerUnits = Units.PIXELS;
app.preferences.typeUnits = TypeUnits.PIXELS;
app.preferences.askBeforeSavingLayeredTIFF = false;
app.foregroundColor.rgb.hexValue = "000000";
app.backgroundColor.rgb.hexValue = "ffffff";
app.displayDialogs = DialogModes.NO;
}
// Fit image to desired canvas dimensions in pixels.
function psFit(width, height, resolution){
var doc = activeDocument;
// the original dimensions
var startWidth = doc.width;
var startHeight = doc.height;
var startRes = doc.resolution;
doc.backgroundLayer.duplicate();
if(startRes != resolution){
doc.resizeImage(startWidth, startHeight, resolution,
ResampleMethod.AUTOMATIC);
}
if (startWidth != width){
if (startWidth < width){
doc.resizeCanvas(width, null, AnchorPosition.MIDDLECENTER);
} else {
doc.resizeImage(width, null, null, ResampleMethod.AUTOMATIC);
}
}
if (startHeight != height){
if (startHeight < height){
doc.resizeCanvas(null, height, AnchorPosition.MIDDLECENTER);
} else {
doc.resizeImage(null, height, null, ResampleMethod.AUTOMATIC);
doc.resizeCanvas(width, null, AnchorPosition.MIDDLECENTER);
}
}
}
function psInsertMeta(id) {
// instructions is a standard writable photoshop tag.
// Exif tags are not writable.
if (app.activeDocument.info.instructions != undefined ||
app.activeDocument.info.instructions != "") {
app.activeDocument.info.instructions += ", " + id;
}else {
app.activeDocument.info.instructions = id;
}
return "Updated meta tag: instructions = " + id;
}
function psAddClippingPath(name){
var pathRef = app.activeDocument.pathItems.add(name, new Array());
pathRef.makeClippingPath();
pathRef.deselect();
return "Added Clipping Path: " + name;
}
function psGetBackgroundLayer(){
var name;
for(var i = 0; i < app.activeDocument.artLayers.length; i++){
if(app.activeDocument.artLayers[i].isBackgroundLayer) {
name = app.activeDocument.artLayers[i].name;
break;
}
}
return name;
}
function psRenameLayer(oldName, newName){
var layerRef = app.activeDocument.artLayers.getByName(oldName);
layerRef.name = newName;
return "Layer " + oldName + " renamed to " + newName;
}
function psLockLayer(name){
var layerRef = app.activeDocument.artLayers.getByName(name);
layerRef.allLocked = true;
return "Locked Layer: " + name;
}
function psMakeLayerInvisible(name){
var layerRef = app.activeDocument.artLayers.getByName(name);
layerRef.visible = false;
return "Made Layer Invisible: " + name;
}
function psCopyLayer(oldName, newName){
var layerRef = app.activeDocument.artLayers.getByName(oldName);
var newLayer = layerRef.duplicate();
newLayer.name = newName;
return "Copied Layer: " + oldName + " to " + newName;
}
function psAddLayer(name){
var layerRef = app.activeDocument.artLayers.add();
layerRef.name = name;
return "Added Layer: " + name;
}
function psMakeBackgroundLayer(name){
var layerRef = app.activeDocument.artLayers.getByName(name);
layerRef.isBackgroundLayer = true;
return "Set to Background: " + name;
}
function psSetAsActiveLayer(name){
var layerRef = app.activeDocument.artLayers.getByName(name);
app.activeDocument.activeLayer = layerRef;
return "Set " + name + " as active layer";
}
function psExit(){
app.purge(PurgeTarget.ALLCACHES);
executeAction(app.charIDToTypeID('quit'), undefined, DialogModes.NO);
}
function psWriteToFile(fileName, text){
var txtFile = new File(fileName);
txtFile.open("a");
txtFile.writeln(text);
txtFile.close();
}
function psInitFile(fileName){
var txtFile = new File(fileName);
txtFile.open("w");
txtFile.writeln("File,Size(Bytes),Time(ms)");
txtFile.close();
}
var inputFolder = new Folder(ORIGINAL);
var fileList = inputFolder.getFiles(/\.(jpg|tif|psd|bmp|gif|png|jpeg|tiff)$/i);
var startTime, endTime, actionTime;
psInitFile(benchmarkFile);
psSetDefaults();
for (var i = 0; i < fileList.length; i++) {
startTime = new Date().getTime();
// Perform PhotoShop action on File
psAction(fileList[i]);
endTime = new Date().getTime();
actionTime = endTime - startTime; // in milliseconds
psWriteToFile(benchmarkFile, fileList[i].name + "," +
fileList[i].length + "," +
actionTime);
}
psExit();
var SCRIPTS_FOLDER = 'E:\\SCRIPTS\\';
var pathName =String( app.activeDocument.fullName);
if(pathName.indexOf('ORIGINAL') > -1){
var theId = pathName.replace('/e/FILES/', '').replace('/ORIGINAL', '').slice (0, 5);
var script = new File(SCRIPTS_FOLDER + theId + '.jsx'); // the child script
$.evalFile (script);
}
@kardolus
Copy link
Author

Extendscript (jsx) files that run as part of the startup process of Photoshop are timed out by Adobe. To work around this timeout limit you can start a script as a child process with a simple script such as "runChild.jsx".

On Windows, put the file in C:\Program Files\Common Files\Adobe\Startup Scripts CC\Adobe Photoshop and it will start up automatically.
On Mac OS, the startup folder for user-defined scripts is: ~/Library/Application Support/Adobe/Startup Scripts CC/Adobe Photoshop

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