Tuesday, April 16, 2013

Photoshop Scripting with Javascript

Photoshop has a couple of kinds of automation, and today I learned about scripting it with Javascript. You can also record actions into macros in the Actions toolbox/window; though these are somewhat limited, they can be assigned a hotkey from a short range of the function keys (F2, F3, etc.)

Here's the script I threw together from examples and experimenting. Its purpose is to visibly watermark a folder of images with one image. It prompts you to choose a folder of images that you want to modify, then an output folder, then the watermark image you want pasted onto the images from the folder. At each image, the script prompts you to specify which corner of the image you want the watermark on.

 /*   
 Description:  
 This script pastes one image to the chosen corner of each image in a folder.  
 */  
 // enable double clicking from the   
 // Macintosh Finder or the Windows Explorer  
 #target photoshop  
 ////////////////////////////////////  
 /// Options and Variables  
 ///  
 ////////////////////////////////////  
   
 //margin is the number of pixels you want to space the edge of the watermark  
 //from the edge of the image  
 var marginX = 5;   
 var marginY = 5;  
   
 //save a copy of the modified images to a jpeg file   
 var fileSaveOptions = new JPEGSaveOptions();  
 fileSaveOptions.embedColorProfile = true;  
 fileSaveOptions.formatOptions = FormatOptions.STANDARDBASELINE;  
 fileSaveOptions.matte = MatteType.NONE;  
 fileSaveOptions.quality = 10;  
   
 // Close the file without saving  
 var documentCloseOptions = SaveOptions.DONOTSAVECHANGES;  
                  
 // Make Photoshop the frontmost application  
 // in case we double clicked the file  
 app.bringToFront();  
   
 /////////////////////////  
 // SETUP  
 /////////////////////////  
   
 // A list of file extensions to skip, keep them lower case  
 gFilesToSkip = Array( "db", "xmp", "xcf", "thm", "txt", "doc", "md0", "tb0", "adobebridgedb", "adobebridgedbt", "bc", "bct" );  
   
 /////////////////////////  
 // MAIN  
 /////////////////////////  
   
 // Pops open a dialog for the user to  
 // choose the folder of documents to process  
 var inputFolder = Folder.selectDialog("Select a folder of documents to process");  
   
 // Pops open a dialog for the user to  
 // set the output folder  
 var outputFolder = Folder.selectDialog("Select a folder for the output files");  
   
 //choose the file to watermark with  
 var logoFile = File.openDialog ("Select the logo file", "*.png", false);  
 var logo = open(logoFile);  
 //select all of the watermark  
 logo.selection.selectAll();  
 //copy the watermark to the clipboard  
 logo.selection.copy();  
   
 // Open Folder of Images  
 OpenFolder();  
 /////////////////////////  
 // FUNCTIONS  
 /////////////////////////  
   
 // Given the a Folder of files, open them  
 function OpenFolder() {  
      var filesOpened = 0;  
      var fileList = inputFolder.getFiles();  
      for ( var i = 0; i < fileList.length; i++ ) {  
           // Make sure all the files in the folder are compatible with PS  
           if ( fileList[i] instanceof File && ! fileList[i].hidden && ! IsFileOneOfThese( fileList[i], gFilesToSkip )) {  
                var photo = open( fileList[i] );  
                filesOpened++;  
                  
                //add a new layer for the watermark  
                var pasteLayer = photo.artLayers.add();  
                //paste the watermark into the new layer  
                photo.paste();  
                //ask the user which corner the watermark should be in  
                var corner = prompt("Top Left = 1, Top Right = 2, \r\nBottom Left = 3, Bottom Right = 4, \r\n Quit = 5","1","Which corner?");  
                if(corner == 1)  
                     topLeft(pasteLayer);  
                else if(corner == 2)  
                     topRight(pasteLayer);  
                else if(corner == 3)  
                     bottomLeft(pasteLayer);  
                else if(corner == 4)  
                     bottomRight(pasteLayer);  
                else if(corner == 5)  
                     break; //stop the program  
   
                //Save a copy of the file  
                app.activeDocument.saveAs(outputFolder, fileSaveOptions, true, Extension.LOWERCASE);  
                //Close the modified file  
                app.activeDocument.close(documentCloseOptions);  
           }  
      }  
 }  
   
 // given a file name and a list of extensions  
 // determine if this file is in the list of extensions  
 function IsFileOneOfThese( inFileName, inArrayOfFileExtensions ) {  
      var lastDot = inFileName.toString().lastIndexOf( "." );  
      if ( lastDot == -1 ) {  
           return false;  
      }  
      var strLength = inFileName.toString().length;  
      var extension = inFileName.toString().substr( lastDot + 1, strLength - lastDot );  
      extension = extension.toLowerCase();  
      for (var i = 0; i < inArrayOfFileExtensions.length; i++ ) {  
           if ( extension == inArrayOfFileExtensions[i] ) {  
                return true;  
           }  
      }  
      return false;  
 }  
 function topLeft( layer ){// layerObject, Number, Number  
      // if can not move layer return  
   if(layer.iisBackgroundLayer||layer.positionLocked) return  
   // get the layer bounds  
   var layerBounds = layer.bounds;  
   // get top left position  
   var layerX = layerBounds[0].value;  
   var layerY = layerBounds[1].value;  
   // the difference between where layer needs to be and is now  
   var deltaX = marginX-layerX;  
   var deltaY = marginY-layerY;  
   // move the layer into position  
   layer.translate (deltaX, deltaY);  
 }  
 function topRight( layer ){  
      // if can not move layer return  
   if(layer.iisBackgroundLayer||layer.positionLocked) return  
   // get the layer bounds  
   var layerBounds = layer.bounds;  
   // get top left position  
   var layerX = layerBounds[0].value;  
   var layerY = layerBounds[1].value;  
   // the difference between where layer needs to be and is now  
   var deltaX = layerX-marginX;  
   var deltaY = marginY-layerY;  
   // move the layer into position  
   layer.translate (deltaX, deltaY);  
 }  
 function bottomRight( layer ){  
      // if can not move layer return  
   if(layer.iisBackgroundLayer||layer.positionLocked) return  
   // get the layer bounds  
   var layerBounds = layer.bounds;  
   // get top left position  
   var layerX = layerBounds[0].value;  
   var layerY = layerBounds[1].value;  
   // the difference between where layer needs to be and is now  
   var deltaX = layerX-marginX;  
   var deltaY = layerY-marginY;  
   // move the layer into position  
   layer.translate (deltaX, deltaY);  
 }  
 function bottomLeft( layer ){  
      // if can not move layer return  
   if(layer.iisBackgroundLayer||layer.positionLocked) return  
   // get the layer bounds  
   var layerBounds = layer.bounds;  
   // get top left position  
   var layerX = layerBounds[0].value;  
   var layerY = layerBounds[1].value;  
   // the difference between where layer needs to be and is now  
   var deltaX = marginX-layerX;  
   var deltaY = layerY-marginY;  
   // move the layer into position  
   layer.translate (deltaX, deltaY);  
 }  

My references and borrowed code:
http://www.tranberry.com/photoshop/photoshop_scripting/PS4GeeksOrlando/IntroScripts/openFolder.jsx
http://forums.adobe.com/message/4287129?tstart=0, reply number 3

No comments: