// Create a subimages file for AppGameKit - Adobe Photoshop Script // Description: Export a colon seperated subimages.txt file converting the layer names to ascii values suitable for a BMFont // Requirements: Adobe Photoshop CS2020 (not tried on earlier versions) // Version: 1.0 // Author: Scraggle // =============================================================================== // Installation: // 1. Place script in // Mac: '~/Applications/Adobe Photoshop CC#/Presets/Scripts/' // Win: 'C:\Program Files\Adobe\Adobe Photoshop CC#\Presets\Scripts\' // 2. Restart Photoshop // 3. Choose File > Scripts > Subimages File Creator // Alterntively: // Choose: File > Scripts > Browse then select and run the script from whereever you saved it // =============================================================================== // Enables double-click launching from the Mac Finder or Windows Explorer #target photoshop // Bring application forward app.bringToFront(); // Set active Document variable and decode name for output var docRef = app.activeDocument; var docName = decodeURI(activeDocument.name); // Define pixels as unit of measurement var defaultRulerUnits = preferences.rulerUnits; preferences.rulerUnits = Units.PIXELS; // Define variable for the number of layers in the active document var layerNum = app.activeDocument.artLayers.length; // Define variable for the active layer in the active document var layerRef = app.activeDocument.activeLayer; // Define varibles for x and y of layers var x = layerRef.bounds[0].value; var y = layerRef.bounds[1].value; var w = layerRef.bounds[2].value; var h = layerRef.bounds[3].value; var coords = ""; // Loop to iterate through all layers function recurseLayers(currLayers) { for ( var i = 0; i < currLayers.layers.length; i++ ) { layerRef = currLayers.layers[i]; if ( layerRef.visibility == false ) { continue; } x = layerRef.bounds[0].value; y = Math.floor(layerRef.bounds[1].value / 100)*102 w = layerRef.bounds[2].value - x; h = 102 var chr = layerRef.name.charCodeAt(0); if (chr == 8220) {chr = 34;} // Special case for quote character coords += chr + ":" + x + ":" + y + ":" + w + ":" + h + "\n"; //test if it's a layer set if ( isLayerSet(currLayers.layers[i]) ) { recurseLayers(currLayers.layers[i]); } } } //a test for a layer set function isLayerSet(layer) { try { if ( layer.layers.length > 0 ) { return true; } } catch(err) { return false; } } // Ask the user for the folder to export to var FPath = Folder.selectDialog("Save exported coordinates to"); // Detect line feed type if ( $.os.search(/windows/i) !== -1 ) { fileLineFeed = "Windows"; } else { fileLineFeed = "Macintosh"; } // Export to txt file var filename = docName.substring(0, docName.length - 4) + " subimages.txt"; function writeFile(info) { try { var f = new File(FPath + "/" + filename); f.remove(); f.open('a'); f.lineFeed = fileLineFeed; f.write(info); f.close(); } catch(e){} } // Run the functions recurseLayers(docRef); preferences.rulerUnits = defaultRulerUnits; // Set preferences back to user 's defaults writeFile(coords); // Show results if ( FPath == null ) { alert("Export aborted", "Canceled"); } else { alert("Exported " + layerNum + " layer's coordinates to " + FPath + "/" + filename + " using " + fileLineFeed + " line feeds.", "Success!"); }