lohriialo/photoshop-scripting-python

Python photoshop

abdalwdod opened this issue · 3 comments

How can i create file Psd and insert in it a new image

Hey, I had the same doubt writing a code to do so and I managed a way to do; Since Photoshop doesn't equally have a way to insert a image as "Smart Object", I had to use this snippet:

def Apply(file):
jsx = r"""
var idPlc = charIDToTypeID( "Plc " );
var desc1883 = new ActionDescriptor();
var idIdnt = charIDToTypeID( "Idnt" );
desc1883.putInteger( idIdnt, 24 );
var idnull = charIDToTypeID( "null" );
desc1883.putPath( idnull, new File( '""" + file + """') );
var idFTcs = charIDToTypeID( "FTcs" );
var idQCSt = charIDToTypeID( "QCSt" );
var idQcsa = charIDToTypeID( "Qcsa" );
desc1883.putEnumerated( idFTcs, idQCSt, idQcsa );
var idOfst = charIDToTypeID( "Ofst" );
var desc1884 = new ActionDescriptor();
var idHrzn = charIDToTypeID( "Hrzn" );
var idRlt = charIDToTypeID( "#Rlt" );
desc1884.putUnitDouble( idHrzn, idRlt, 0.000000 );
var idVrtc = charIDToTypeID( "Vrtc" );
var idRlt = charIDToTypeID( "#Rlt" );
desc1884.putUnitDouble( idVrtc, idRlt, -0.000000 );
var idOfst = charIDToTypeID( "Ofst" );
desc1883.putObject( idOfst, idOfst, desc1884 );
executeAction( idPlc, desc1883, DialogModes.NO );
"""
app.doJavascript(jsx)

Since it uses javascript, make sure the file instance applied at the function follows this format: "path\to\item", ok?

Hope it helps!

I wan to replace an image in a specific layer. Is there a way i can do it ?!

I wan to replace an image in a specific layer. Is there a way i can do it ?!

So far, either in Photoshop documentation have a specific method for replacing images in a layer, the best you can do is turn the layer in a Smart Object kind and use this code in javascript:

function Replace(img) {
var idplacedLayerReplaceContents = stringIDToTypeID( "placedLayerReplaceContents" );
var desc1852 = new ActionDescriptor();
var idnull = charIDToTypeID( "null" );
desc1852.putPath( idnull, new File(img) );
executeAction( idplacedLayerReplaceContents, desc1852, DialogModes.NO );
}

Make sure the "img" is a string and in the same format used above;