Skip to main content
Participant
November 14, 2015
Answered

Place embedded

  • November 14, 2015
  • 4 replies
  • 3026 views

Is the command "Place embedded" scriptable with Javascript in order to add a file as smart object to an opened document, without copy&paste? Thank you

This topic has been closed for replies.
Correct answer uberplugins

var filePath = new File('~/path/to/image.png');

placeEmbeded(filePath);

function placeEmbeded(filePath) {

    var actDesc = new ActionDescriptor();

    actDesc.putPath(charIDToTypeID('null'), filePath);

    actDesc.putEnumerated(charIDToTypeID('FTcs'), charIDToTypeID('QCSt'), charIDToTypeID('Qcsa'));

    executeAction(charIDToTypeID('Plc '), actDesc, DialogModes.NO);

}

4 replies

Participant
April 15, 2023

How do you define the size and position of the image being placed? Your solution works well, putting the image at the layer's center.

Thanks!

 

Stephen Marsh
Community Expert
Community Expert
April 15, 2023
quote

How do you define the size and position of the image being placed? Your solution works well, putting the image at the layer's center.

Thanks!

 


By @mslino65

 

The script will behave the same as any file being placed manually, using the resolution of the placed image to the target image to determine the physical size, unless Preferences > General > Resize Image During Place is active, which will proportionally scale an oversized image to the canvas width or height.

 

You can always change DialogModes.NO to DialogModes.ALL for an interactive place to test what is going on.

Participant
November 15, 2015

Thank you. It works!

JJMack
Community Expert
Community Expert
November 15, 2015

Place may or may not scale the smart object layer.  To be able to get the size of the smart object I always scale the layer to 100% size after doing the place.

function placeImage(file) {

  // avoid bug in cs2 and maybe CS4  make sure current layer targer is not the Layer Mask

  var idslct = charIDToTypeID( "slct" );

     var desc5 = new ActionDescriptor();

     var idnull = charIDToTypeID( "null" );

         var ref3 = new ActionReference();

         var idChnl = charIDToTypeID( "Chnl" );

         var idChnl = charIDToTypeID( "Chnl" );

         var idRGB = charIDToTypeID( "RGB " );

         ref3.putEnumerated( idChnl, idChnl, idRGB );

     desc5.putReference( idnull, ref3 );

     var idMkVs = charIDToTypeID( "MkVs" );

     desc5.putBoolean( idMkVs, false );

  executeAction( idslct, desc5, DialogModes.NO );

  // Place in the file

  var idPlc = charIDToTypeID( "Plc " );

     var desc5 = new ActionDescriptor();

     var idnull = charIDToTypeID( "null" );

     desc5.putPath( idnull, new File( file ) );

     var idFTcs = charIDToTypeID( "FTcs" );

     var idQCSt = charIDToTypeID( "QCSt" );

     var idQcsa = charIDToTypeID( "Qcsa" );

     desc5.putEnumerated( idFTcs, idQCSt, idQcsa );

     var idOfst = charIDToTypeID( "Ofst" );

         var desc6 = new ActionDescriptor();

         var idHrzn = charIDToTypeID( "Hrzn" );

         var idPxl = charIDToTypeID( "#Pxl" );

         desc6.putUnitDouble( idHrzn, idPxl, 0.000000 );

         var idVrtc = charIDToTypeID( "Vrtc" );

         var idPxl = charIDToTypeID( "#Pxl" );

         desc6.putUnitDouble( idVrtc, idPxl, 0.000000 );

     var idOfst = charIDToTypeID( "Ofst" );

     desc5.putObject( idOfst, idOfst, desc6 );

  executeAction( idPlc, desc5, DialogModes.NO );

  // because I can't find a way to get the scale of a smart object layer, reset to 100%

  activeDocument.activeLayer.resize(100 ,100,AnchorPosition.MIDDLECENTER);

  return app.activeDocument.activeLayer;

}

JJMack
Participant
November 15, 2015

Thank you. It works!

uberplugins
uberpluginsCorrect answer
Inspiring
November 14, 2015

var filePath = new File('~/path/to/image.png');

placeEmbeded(filePath);

function placeEmbeded(filePath) {

    var actDesc = new ActionDescriptor();

    actDesc.putPath(charIDToTypeID('null'), filePath);

    actDesc.putEnumerated(charIDToTypeID('FTcs'), charIDToTypeID('QCSt'), charIDToTypeID('Qcsa'));

    executeAction(charIDToTypeID('Plc '), actDesc, DialogModes.NO);

}