Script to replace Smart Objects in Photoshop files
May 15th, 2013Wanting to share graphical assets between multiple Photoshop files and update them at once just like when linking images to Illustrator files, I wrote a script that can replace Smart Object layers with external .psb files. This actually is my first Photoshop script. Any suggestions for improvements would be much appreciated.
How to use it:
- Install the script, place the .jsx file in the Scripts folder (Photoshop CS/Presets/Scripts) and restart the application. The script appears in the Scripts menu (File > Scripts)
- Create the graphical assets as .psb files and save them in a folder named “assets”
The file names must start with the prefix “so_” and end with “.psb”. (the folder name and the prefix can be altered in the variables in the beginning of the code.) - In the psd(b) files in which you want to use these assets, create Smart Object layers that have the same names as .psb files without the extension, e.g. a layer named “so_mail_icon” links to assets/so_mail_icon.psb.
- Run the script. The smart layers will be replaced with the .psb files.
var prefix = "so_"; var assetPath = "assets/" var count = 0; listLayersets(app.activeDocument); listLayers(app.activeDocument); var path; function listLayersets(pointer) { for (var i = 0; i < pointer.layerSets.length; i++) { var layerset = pointer.layerSets[i]; listLayers (layerset); } } function listLayers(pointer) { for (var i = 0; i < pointer.artLayers.length; i++) { var layer = pointer.artLayers[i]; if (layer.name.indexOf(prefix) == 0) { if (layer.kind == "LayerKind.SMARTOBJECT") { app.activeDocument.activeLayer = layer; replaceSO (app.activeDocument.path + "/" + assetPath + layer.name + ".psb"); } } } } function replaceSO (newFile) { var id = stringIDToTypeID( "placedLayerReplaceContents" ); var desc = new ActionDescriptor(); var idn = charIDToTypeID("null"); desc3.putPath( idn, new File(newFile)); var idp = charIDToTypeID("PgNm"); desc3.putInteger(idp, 1); executeAction(id, desc, DialogModes.NO); return app.activeDocument.activeLayer };
