Automating “Save for Web” File Naming

It’s easy enough to make a macro to output multiple sizes of jpgs into different folders, but if you want multiple sizes of the image in the same folder, what can you do? This.

//vars
var doc = app.activeDocument;
// get a reference to the active document and store it in a variable named "doc"

var fname = new String (doc.name);
//get filename of doc

fname = fname.substr (0, fname.lastIndexOf("."));
//remove extension

var folder = doc.path;
//get folder of doc

var folderStr = folder.toString() + "/jpg";
//name the subfolder to be created in current folder

var outfolder = new Folder (folderStr);
//make Folder object

if (!outfolder.exists) { //check if subfolder already exists
  outfolder.create();
  //create the subfolder if it doesn't exist
}

//functions
function resize(height) {
  doc.resizeImage(null,UnitValue(height,"px"),null,ResampleMethod.BICUBICSHARPER);
  //resize image to specified height, keeping aspect ratio
}
function saveForWeb(outputFolderStr, filename) {
  var opts, file;
  opts = new ExportOptionsSaveForWeb();
  //create object for save for web options
  opts.format = SaveDocumentType.JPEG;
  //specify format option jpg
  if (filename.length > 27) { //check filename length so it won't get trunctuated
    file = new File(outputFolderStr + "/temp.jpg");
    //temp file name because file name would be too long
    activeDocument.exportDocument(file, ExportType.SAVEFORWEB, opts);
    //export
    file.rename(filename + ".jpg");
    //rename temp file to the desired name
  } else { //desired name is short enough
    file = new File(outputFolderStr + "/" + filename + ".jpg");
    activeDocument.exportDocument(file, ExportType.SAVEFORWEB, opts);
    //export
  }
}

//run
resize(1200);
saveForWeb(folderStr, fname + "-zoom");
resize(600);
saveForWeb(folderStr, fname + "-large");
resize(250);
saveForWeb(folderStr, fname);

If you wanted to use a predetermined folder instead of creating a subfolder, you could just change this:

var currentFolder = doc.path;
//get folder of doc

var folderStr = currentFolder.toString() + "/jpg";
//name the subfolder to be created in current folder

to this:

var folderStr = "/your/path/here"
//use your desired folder for output

Square Canvas in Photoshop with Javascript

This script is really simple, but it can be really useful if you need to make a lot of images square instead of rectangle. You can run it from File > Scripts > Browse…, but since we are talking about using it for a lot of images, you would probably want to record a macro. Then you can use the macro as any regular Photoshop macro.

var w = app.activeDocument.width;
var h = app.activeDocument.height;

if (w > h) {
	app.activeDocument.resizeCanvas(w,w);
}
if (h > w) {
	app.activeDocument.resizeCanvas(h,h);
}