Copy link to clipboard
Copied
Hi~
I want to make a script that could read layer data,convert to smart obj, save layer as png,but now i have no idea how to convert a single layer to smart obj, and save image failed.
Could someone tell me how to do this, or give me some document, Thanks So Much.
Best regards.
function exportDataAndPng() {
var curGroups = []
var allnames = []
function _exportUIData()
{
var startRulerUnits = app.preferences.rulerUnits;
var startTypeUnits = app.preferences.typeUnits;
app.preferences.rulerUnits = Units.PIXELS;
app.preferences.typeUnits = TypeUnits.PIXELS;
var layers = app.activeDocument.layers;
var message = "";
var psdName = app.activeDocument.name.replace (".psd", "");
var file = new File (destPath + "/" + psdName + "_UIData.txt");
file.open ("w");
file.encoding = "UTF-8"
_loadpngNames()
curGroups.push( 'Root' )
_checkLayers(layers, file);
app.preferences.rulerUnits = startRulerUnits;
app.preferences.typeUnits = startTypeUnits;
}
function _loadpngNames()
{
//allnames.push(...)//load png names from target folder
}
function _getUniquePngName( nameIn )
{
var idx = 1
var origName = nameIn.replace(/^\s+|\s+$/gm, '');
origName = origName.replace(/[_%d+]$/,'')
var name = origName
while ( name in allnames ) {
name = origName + "_" + idx.toString()
idx += 1
}
return name
}
function _checkLayers(layers, expFile)
{
for(var i = 0 ; i < layers.length ; i++)
{
var layer = layers[i];
if(layer.visible == false)
{
continue;
}
if(layer.typename == 'ArtLayer')
{
app.activeDocument.activeLayer = layer
if ( layer.kind != LayerKind.TEXT ) {
//how to convert to smart object
//***********************
//save png
var filename = _getUniquePngName(decodeURI(layer.name),allnames)//create unique png name
if (_saveImage( filename ))
{
allnames.push( filename )
}
}
_writeData(layer, filename, expFile, curGroups);//export layer data
}
else
{
curGroups.push( decodeURI(layer.name) )
_checkLayers(layer.layers, expFile);
curGroups.pop()
}
}
}
function _saveImage( filename )
{
var desc = new ActionDescriptor(),
desc2 = new ActionDescriptor();
desc2.putEnumerated(app.charIDToTypeID("Op "), app.charIDToTypeID("SWOp"), app.charIDToTypeID("OpSa"));
desc2.putEnumerated(app.charIDToTypeID("Fmt "), app.charIDToTypeID("IRFm"), app.charIDToTypeID("PN24"));
desc2.putBoolean(app.charIDToTypeID("Intr"), false);
desc2.putBoolean(app.charIDToTypeID("Trns"), true);
desc2.putBoolean(app.charIDToTypeID("Mtt "), true);
desc2.putInteger(app.charIDToTypeID("MttR"), 255);
desc2.putInteger(app.charIDToTypeID("MttG"), 255);
desc2.putInteger(app.charIDToTypeID("MttB"), 255);
desc2.putBoolean(app.charIDToTypeID("SHTM"), false);
desc2.putBoolean(app.charIDToTypeID("SImg"), true);
desc2.putBoolean(app.charIDToTypeID("SSSO"), false);
desc2.putList(app.charIDToTypeID("SSLt"), new ActionList());
desc2.putBoolean(app.charIDToTypeID("DIDr"), false);
desc2.putPath(app.charIDToTypeID("In "), new File(filename));
desc.putObject(app.charIDToTypeID("Usng"), app.stringIDToTypeID("SaveForWeb"), desc2);
app.executeAction(app.charIDToTypeID("Expr"), desc, DialogModes.NO);
}
function _writeData( layer, filename, expFile, curGroups ) {
//export layer data, code is to large to post
}
_exportUIData()
}
There are at least two ways to convert an active/targeted layer to a SO:
var idnewPlacedLayer = stringIDToTypeID( "newPlacedLayer" );
executeAction( idnewPlacedLayer, undefined, DialogModes.NO );
// or
app.runMenuItem(stringIDToTypeID('newPlacedLayer'));
Copy link to clipboard
Copied
var layers = app.activeDocument.layers;
for(var i = 0 ; i < layers.length ; i++)
{
app.activeDocument.activeLayer = layer
//convert to smart obj
//save as png
1. How to convert current activeLayer to smart object.
2. How to save current activeLayer as png
Thanks a lot
Copy link to clipboard
Copied
There are at least two ways to convert an active/targeted layer to a SO:
var idnewPlacedLayer = stringIDToTypeID( "newPlacedLayer" );
executeAction( idnewPlacedLayer, undefined, DialogModes.NO );
// or
app.runMenuItem(stringIDToTypeID('newPlacedLayer'));
Copy link to clipboard
Copied
save activelayer as png?
Copy link to clipboard
Copied
save activelayer as png?
By @hui5C21
I haven't looked at your code. Have you searched these forums: Export layer to a folder: or web: LayerPNG24.jsx? You should get multiple results with working code examples etc.
A couple of common examples are turning off layer visibility for all layers except the active layer and save/export... Or you can dupe the active layer to a new temp doc and save/export from there and close the temp file without saving.
Copy link to clipboard
Copied
Yes,I have done this successfully on the way 'dupe the active layer to a new temp doc and save/export from there and close the temp file without saving', and i just want to known if there is a easier way to do this. I'm going to search and try make it it easier.
Thank you very much, best wish!
Copy link to clipboard
Copied
I haven't tested, however I'm guessing that turning off visibility of all layers except the active layer would be more efficient/faster... But then you may need to capture and restore the visibility of all layers.
Copy link to clipboard
Copied
I will try it this way, Thanks
Copy link to clipboard
Copied
I believe that it is possible, however, I can't find an example to capture the current layer panel visibility, then restore after changing the current visibility... Or perhaps what I was thinking of is storing/restoring the selected layers and not visibility, as...
Anyway, what you can do is call the following function once before saving/exporting, then call the function a second time, after exporting to return to the previous visibility (which appears to be "silently" stored without explicitly setting it so):
toggleActiveLayerVisibility(true);
function toggleActiveLayerVisibility(toggleOptionsPalette) {
var s2t = function (s) {
return app.stringIDToTypeID(s);
};
var descriptor = new ActionDescriptor();
var list = new ActionList();
var reference = new ActionReference();
reference.putEnumerated( s2t( "layer" ), s2t( "ordinal" ), s2t( "targetEnum" ));
list.putReference( reference );
descriptor.putList( s2t( "null" ), list );
descriptor.putBoolean( s2t( "toggleOptionsPalette" ), toggleOptionsPalette );
executeAction( s2t( "show" ), descriptor, DialogModes.NO );
}
Copy link to clipboard
Copied
To avoid wiped spaces in 4-char ID's ("Op " & "In ") next time insert code by using </> icon.