//================================================================= // // // // This script will save out each individual layer into it's own png file appending layer // attributes, and region attributes to the file name and depositing them in folders based // on region. This script is to assist with automation export of grid images for any game. // // Note: You must save your psd document before running this script. // Note: The script will only affect visible layers. // Script was written using ExtendScript Toolkit 2 and tested on Adobe Photoshop CS4. //================================================================= //http://cssdk.host.adobe.com/sdk/1.0/docs/WebHelp/app_notes/ps_doc_model.htm displayDialogs = DialogModes.NO; app.preferences.rulerUnits = Units.PIXELS; //================================================================= // Save Options //================================================================= saveOptions = new PNGSaveOptions(); sfwOptions = new ExportOptionsSaveForWeb(); sfwOptions.format = SaveDocumentType.JPEG; sfwOptions.includeProfile = false; sfwOptions.interlaced = 0; sfwOptions.optimized = true; sfwOptions.quality = 100; //0-100 //================================================================= // Globals: Feel free to modify these variables to suit your needs. //================================================================= //TODO:remove var imgFolder = "images"; var imgDir = "gbl"; //================================================================= // Don't touch anything below here unless you know what you are doing! //================================================================= //================================================================= // Determine Canvas Size //================================================================= var docH = app.activeDocument.height.value; var docW = app.activeDocument.width.value; //================================================================= // Check to see that psd has been saved //================================================================= if ((documents.length != 0) && (activeDocument.saved)){ var AD = activeDocument; var CurrentFolder = AD.path; var docRef = app.activeDocument; var parentFolder = CurrentFolder + ''; parentFolder = parentFolder.replace('/src',''); //alert(parentFolder); //================================================================= // Get the Filename without the file extension //================================================================= var NumChars = (AD.name).indexOf("."); //var FileCat = (AD.name).substring (0, NumChars); var indexF = AD.name.indexOf("_"); //mode var FileCat = AD.name.substring(0,indexF); //contentType var fileType = AD.name.substring(indexF + 1); fileType = fileType.replace(".psd",""); //alert(FileCat); //================================================================= // Remove Groups - TODO //================================================================= //while ( app.activeDocument.layerSets.length > 0 ) { // var id633 = charIDToTypeID( "Ungr" ); // app.activeDocument.layerSets[0].remove(); //} //================================================================= // Check to see how many layers are visible //================================================================= var tempLayer = AD.artLayers.add(); var checkArray = new Array(AD.layers.length); for(a=1;a<=AD.layers.length;a++){ var CL = AD.layers[a-1]; if(!((CL.kind == LayerKind.TEXT)||(CL.kind == LayerKind.NORMAL)||(CL.kind == LayerKind.SMARTOBJECT)||(CL.kind == LayerKind.LayerSet))){ checkArray[a-1] = 1; } if(CL.visible == 0){ checkArray[a-1] = 2; } } for(a=1;a<=AD.layers.length;a++){ AD.layers[a-1].visible = 0; } for(a=2;a<=AD.layers.length;a++){ AD.layers[a-2].visible = 0; AD.layers[a-1].visible = 1; if((checkArray[a-1]!= 1)&&(checkArray[a-1]!= 2)){ //================================================================= // Split up layer name so we can use it in the file name //================================================================= var lyrname = AD.layers[a-1].name; var index = AD.layers[a-1].name.indexOf("_") ; var prefix = AD.layers[a-1].name.substring(0,index); var suffix = AD.layers[a-1].name.substring(index + 1); var stemIndex = AD.layers[a-1].name.indexOf("-"); var stemSuffix = AD.layers[a-1].name.substring(stemIndex + 1); var imageStem = AD.layers[a-1].name.substring(0,stemIndex); var imageState = AD.layers[a-1].name.substring(index + 1,stemIndex); var indexCrop = AD.layers[a-1].name.indexOf("-"); var prefixCrop = AD.layers[a-1].name.substring(0,indexCrop); var suffixCrop = AD.layers[a-1].name.substring(indexCrop + 1); var resizeIndex = AD.layers[a-1].name.indexOf("x"); var resizeSuffix = AD.layers[a-1].name.substring(resizeIndex + 1); var resizeStem = AD.layers[a-1].name.substring(0,resizeIndex); var resizeState = AD.layers[a-1].name.substring(indexCrop + 1,resizeIndex); //alert(suffixCrop); var savedState = docRef.activeHistoryState; //================================================================= // Global Functions //================================================================= //apply layerStyle //ArtLayer.applyStyle("Some Style Name") //Global vars driven thru interface //TODO validate style exist var autoStyle = 0; var style = "Sepia Tone (Image)"; function applyLayerStyle() { if (autoStyle == 1){ alert(autoStyle); AD.layers[a-1].applyStyle(style); } else { //SKIP } } //Rasterize SMART OBJECTS function rasterizeSO() { if (AD.layers[a-1].kind == LayerKind.SMARTOBJECT) { var id633 = charIDToTypeID( "Mn " ); var id634 = charIDToTypeID( "MnIt" ); } else { //SKIP } } function autocrop() { if ( suffixCrop == "doc") { //skip } else { AD.resizeCanvas(Number(resizeState),Number(resizeSuffix),AnchorPosition.MIDDLECENTER); } } //CENTER CONTENT function centerAsset() { if(suffixCrop == "doc") { //skip }else{ AD.trim(TrimType.TRANSPARENT,true,true,true,true); } } //debug text function debug(){ if(AD.layers[a-1].transparentPixelsLocked) { var myLayerRef = docRef.artLayers.add(); myLayerRef.kind = LayerKind.TEXT; //myLayerRef.name = "Filename"; var myTextRef = myLayerRef.textItem; if (docRef.width > 128) { myTextRef.size = 60; } else { myTextRef.size = 20; } // Set font size in Points //myTextRef.size = 20; //Set font - use GetFontName.js to get exact name myTextRef.font = "Arial"; //Set text colour in RGB values var newColor = new SolidColor(); newColor.rgb.red = 255; newColor.rgb.green = 255; newColor.rgb.blue = 255; myTextRef.color = newColor; // Set the position of the text. myTextRef.justification = Justification.CENTER; myTextRef.kind = TextType.PARAGRAPHTEXT; myTextRef.width = docRef.width; myTextRef.height = docRef.height; myTextRef.position = [0, docRef.height / 2.5 ]; myLayerRef.opacity = 80; myTextRef.contents =imageState; } } //validate layer names function layerValid(lyrname) { var validate =/^([a-zA-Z0-9_.-])+\_([a-zA-Z0-9_.-])+\-([a-zA-Z0-9])+([a-zA-Z0-9])+/; return validate.test(lyrname); } function exportGBLPNG() { var tempFolderGUID = new Folder (parentFolder+"/"+FileCat+"/"+fileType ); tempFolderGUID.create(); newFile = new File(tempFolderGUID+"/"+FileCat+"_"+fileType+"_"+imageStem+".png"); if (newFile.exists) { newFile.remove() } AD.saveAs (newFile,saveOptions, true, Extension.LOWERCASE); docRef.activeHistoryState = savedState; } function exportPNG() { var tempFolderGUID = new Folder (parentFolder+"/"+FileCat); tempFolderGUID.create(); newFile = new File(tempFolderGUID+"/"+FileCat+"_"+fileType+"_"+imageStem+".png"); if (newFile.exists) { newFile.remove() } AD.saveAs (newFile,saveOptions, true, Extension.LOWERCASE); docRef.activeHistoryState = savedState; } //================================================================= // Create new folders to deposit each set of files based on type //================================================================= switch(FileCat){ case "gbl": if (layerValid(lyrname)) { applyLayerStyle(); rasterizeSO(); centerAsset(); autocrop(); debug(); exportGBLPNG(); } break; default: if (layerValid(lyrname)) { applyLayerStyle(); rasterizeSO(); centerAsset(); autocrop(); debug(); exportPNG(); } break; } } } for(a=1;a<=AD.layers.length;a++){ if(checkArray[a-1] == 2){ AD.layers[a-1].visible = 0; }else{ AD.layers[a-1].visible = 1; } } AD.layers[0].remove(); }else{ alert("You either did not save the document or have no document opened !"); }