Last active
June 7, 2023 11:18
-
-
Save bradley-newman/1c0488adb5b524b00a9511fb6bee0ce1 to your computer and use it in GitHub Desktop.
Photoshop JSX code for getting all the smart objects in a scene, opening them for editing, and saving them out as PNG sprites to folders that match the Photoshop folders they are stored within.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var activeDocName; | |
var activeDocExtension; | |
var filePath; | |
var fileName; | |
function GetAndSaveSmartObjs() | |
{ | |
$.writeln("GetSmartObjs called..."); | |
var rootLayers = new Array(); | |
rootLayers = app.activeDocument.artLayers; //get layers at the root level. | |
//ROOT LAYERS////////////////////////////////// | |
if(rootLayers.length > 0) | |
{ | |
var smartObjs = GetSmartObjsInLayers(rootLayers); //get the smart obs in these layers | |
SaveSmartObjs(smartObjs); | |
} | |
else | |
{ | |
$.writeln("rootLayers array is empty."); | |
} | |
//LAYER SETS/////////////////////////////////////////////// | |
var rootLayerSets = app.activeDocument.layerSets; //get root layers sets | |
//get the smart obs in these layer sets. | |
for(var i = 0; i < rootLayerSets.length; i++) | |
{ | |
var layersInSet = rootLayerSets[i].layers; | |
var smartObjs = GetSmartObjsInLayers(layersInSet); | |
SaveSmartObjs(smartObjs, rootLayerSets[i].name); | |
} | |
} | |
function GetSmartObjsInLayers(layers) | |
{ | |
var smartObjs = new Array(); | |
try | |
{ | |
if(layers.length > 0) | |
{ | |
for(var i = 0; i < layers.length; i++) | |
{ | |
if(layers[i].kind == LayerKind.SMARTOBJECT) | |
{ | |
$.writeln("Adding smart obj: " + layers[i].name); | |
smartObjs.push(layers[i]); | |
} | |
} | |
} | |
else | |
{ | |
$.writeln(layers.name + " array is empty."); | |
} | |
} | |
catch(e) | |
{ | |
alert(e); | |
$.writeln(e); | |
} | |
return smartObjs; | |
} | |
function SaveSmartObjs(smartObjsArray) | |
{ | |
SaveSmartObjs(smartObjsArray, null); | |
} | |
function SaveSmartObjs(smartObjsArray, folderName) | |
{ | |
try | |
{ | |
if(smartObjsArray.length > 0) | |
{ | |
for(var i = 0; i < smartObjsArray.length; i++) | |
{ | |
if(smartObjsArray[i].kind == LayerKind.SMARTOBJECT) | |
{ | |
var smartObj = smartObjsArray[i]; | |
activeDocName = app.activeDocument.name.replace(/\.[^\.]+$/, ''); | |
activeDocExtension = decodeURI(app.activeDocument.name).replace(/^.*\./,''); | |
filePath = app.activeDocument.path; | |
fileName = File(filePath + "/" + activeDocName); | |
var fullFilePath; | |
if(folderName == null) | |
{ | |
fullFilePath = File(filePath + "/" + smartObj.name); | |
} | |
else | |
{ | |
fullFilePath = File(filePath + "/" + folderName + "/" + smartObj.name); | |
CreateFolder(fullFilePath); | |
} | |
$.writeln("fullFilePath: " + fullFilePath); | |
$.writeln("Edit contents of smart obj: " + smartObj.name); | |
//Undocumented code derived from the Photoshop script listener | |
//it opens the smart object for editing of the contents | |
//which when opened crops the layers to the size of the sprite | |
//this allows us to then saved a PNG that is the correct size for the smart object. | |
app.activeDocument.activeLayer = smartObj; | |
var idplacedLayerEditContents = stringIDToTypeID( "placedLayerEditContents" ); | |
var desc7 = new ActionDescriptor(); | |
executeAction( idplacedLayerEditContents, desc7, DialogModes.NO ); | |
SavePNG(fullFilePath); | |
CloseDocument(); | |
} | |
} | |
} | |
else | |
{ | |
$.writeln(smartObjsArray.name + " array is empty."); | |
} | |
} | |
catch(e) | |
{ | |
alert(e); | |
$.writeln(e); | |
} | |
} | |
function CreateFolder(fullPath) | |
{ | |
var folder = new Folder(fullPath); | |
if ( ! folder.exists ) | |
{ | |
folder.create() | |
} | |
} | |
function SavePNG(fullFilePath) //e.g. fullFilePath = "C:/Folder/Texture.psd" | |
{ | |
$.writeln("SavePNG(" + fullFilePath + ")"); | |
try | |
{ | |
if(fullFilePath.exists) | |
{ | |
fullFilePath.remove(); | |
} | |
pngSaveOptions = new PNGSaveOptions(); | |
activeDocument.saveAs(fullFilePath, pngSaveOptions, true, Extension.LOWERCASE); | |
} | |
catch(e) | |
{ | |
alert(e); | |
$.writeln(e); | |
} | |
} | |
function CloseDocument() | |
{ | |
app.activeDocument.close(SaveOptions.DONOTSAVECHANGES); | |
} | |
GetAndSaveSmartObjs(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment