// Photoshop CS script: manyFilesIntoOne // // Given a subdirectory containing many JPG files, this script creates a new // Photoshop document with many layers, each of which is from one of the // subdirectory JPG files. The size of the new doc is set to the size of the // first image file read. // // To use this script, store it anywhere on your disk, then start Photoshop and // use File->Scripts->Browse... to select and execute this script. If you'd // like this script to appear in the list of scripts at File->Scripts, just // store this script in \Program Files\Adobe\Photoshop CS\Presets\Scripts // // References: // \Program Files\Adobe\Photoshop CS\Scripting Guide\Photoshop Scripting Guide.pdf // \Program Files\Adobe\Photoshop CS\Scripting Guide\JavaScript Reference Guide.pdf // // (based on examples from the JavaScript Reference Guide; developed on Windows XP, // not tested on other platforms) // // Jim Elder, Ottawa, Oct 2004 // Save the current preferences var startRulerUnits = app.preferences.rulerUnits; var startTypeUnits = app.preferences.typeUnits; var startDisplayDialogs = app.displayDialogs; // Set Photoshop to use pixels and display no dialogs app.preferences.rulerUnits = Units.PIXELS; app.preferences.typeUnits = TypeUnits.PIXELS; app.displayDialogs = DialogModes.NO; // ask the user to identify the input subdirectory (folder) var inputFilesFolder = Folder.selectDialog( "Select a folder that contains JPG images to be read into one " + "new Photoshop doc as layers", Folder("~")); // the setting of default dir doesn't seem to work for other than ~ // Start the script debugger // uncomment the line below to cause Photoshop to start its debugger at this point // $.level = 1; debugger; // see if the user gave us a folder if (inputFilesFolder != null) { // close all the open documents while (app.documents.length) app.activeDocument.close(); // get all the jpg files in the specified folder var fileList = inputFilesFolder.getFiles("*.png"); // open one of them to get its size for (var i = 0; i < fileList.length; i++) { if (fileList[i] instanceof File) { var firstDoc = open(fileList[i]); break; } } // if there was a first doc... if (typeof(firstDoc) != 'undefined') { // create a new document to contain the merged images var mergedDoc = app.documents.add(firstDoc.width, firstDoc.height, firstDoc.resolution, "Merged", NewDocumentMode.RGB, DocumentFill.TRANSPARENT, 1); // close that first doc firstDoc.close(SaveOptions.DONOTSAVECHANGES); // open each file and add it as a layer to the 'merged' doc for (var i = 0; i < fileList.length; i++) { // open only files if (fileList[i] instanceof File) { open (fileList[i]); // save the document name for the layer name in the merged document var docName = app.activeDocument.name; // flatten the document so we get everything and then copy app.activeDocument.flatten(); app.activeDocument.selection.selectAll(); app.activeDocument.selection.copy(); // close file; don't save anything we did app.activeDocument.close(SaveOptions.DONOTSAVECHANGES); // merge this image into the mergedDoc as a new layer mergedDoc.paste(); mergedDoc.activeLayer.name = docName; } } // sort the layers by name for (var x = 0; x < app.activeDocument.layers.length; x++) { for (var y = 0; y < app.activeDocument.layers.length - 1 - x; y++) { var doc1 = app.activeDocument.layers[y].name; var doc2 = app.activeDocument.layers[y + 1].name; // compare layer names in a non-case sensitive way if (doc1.toUpperCase() > doc2.toUpperCase()) { app.activeDocument.layers[y].move(app.activeDocument.layers[y+1], ElementPlacement.PLACEAFTER); } } } } } // Reset the application preferences app.preferences.rulerUnits = startRulerUnits; app.preferences.typeUnits = startTypeUnits; app.displayDialogs = startDisplayDialogs;