Skip to main content
masoomehm33651827
Participant
August 19, 2017
Question

place file in photoshop document script

  • August 19, 2017
  • 2 replies
  • 6025 views

hello

please help me. i want to write javaScript in photoshop for place files (images) with Specified size and dimentions in a document with a distinct place. (for example in center of document). it is important for me that my file place in photoshop document but not open my file in a new document.

thanks a lot

This topic has been closed for replies.

2 replies

pixxxelschubser
Community Expert
Community Expert
August 19, 2017

The answer of your question is depends on your preferences. Eg if: scale image while placing (sorry I do not work with an english version and do not know the correct name) is enabled you can place an image at the middle center of your base image with the following code

var desc1 = new ActionDescriptor();

    var list1 = new ActionList();

        var ref1 = new ActionReference();

        ref1.putName( charIDToTypeID('Lyr '), "D:/path_to_your_image/name_of_your_image.jpg" );

    list1.putReference( ref1 );

desc1.putList( charIDToTypeID('null'), list1 );

executeAction( charIDToTypeID('Hd  '), desc1, DialogModes.NO );

Furthermore you can scale the image in relation of you base image (eg base image with 1000 x 1000px and image to place with 700 x 700 px) but only in percent eg with the following code

var desc2 = new ActionDescriptor();

desc2.putInteger( charIDToTypeID('Idnt'), 2 );

desc2.putPath( charIDToTypeID('null'), new File( "D:/path_to_your_image/name_of_your_image.jpg" ) );

//desc2.putBoolean( cTID('Lnkd'), true ); // only needed if you wish linked images

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

    var desc3 = new ActionDescriptor();

    desc3.putUnitDouble( charIDToTypeID('Hrzn'), charIDToTypeID('#Pxl'), 0.000000 );

    desc3.putUnitDouble( charIDToTypeID('Vrtc'), charIDToTypeID('#Pxl'), 0.000000 );

desc2.putObject( charIDToTypeID('Ofst'), charIDToTypeID('Ofst'), desc3 );

desc2.putUnitDouble( charIDToTypeID('Wdth'), charIDToTypeID('#Prc'), 70 ); // in percent of the longest side of your base image

desc2.putUnitDouble( charIDToTypeID('Hght'), charIDToTypeID('#Prc'), 70 );

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

Always depends of your preferences and what you really want.

Have fun

JJMack
Community Expert
Community Expert
August 19, 2017

Does not matter what the preference is set to if you use this code the layer will not be scaled.

function placeImage(file) {

// =======avoid bug in cs2 and maybe CS4 ==================================

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 can't get the scale of a smart object, reset to 100%

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

return app.activeDocument.activeLayer;

}

JJMack
JJMack
Community Expert
Community Expert
August 19, 2017

Place by default will scale placed image to fit within canvas with the place transform if the image is larger than canvas size.  You can get the true size of the object by first insuring the placed object transform is set to 100% then get the layers bound.  You can then get the document bounds and set a new transform for the placed smart object layer to size and position the layer where you want it. I do this all the time in my Photo Collage Toolkit's populating scripts.  I use action manager code to place in the image files. Then transform placed layer width and height to 100% to get the layers actual size using layer's bounds.

Any transform you add must  change the width and height the same percentage or you will distort the image.  If you want the image to be a particular aspect ratio or shape  mask the transformed layer to the aspect ratio or shape.

Photo Collage Toolkit

Photoshop scripting is powerful and I believe this package demonstrates this video shows 5 images template populated automatically

The package includes four simple rules to follow when making Photo Collage Template PSD files so they will be compatible with my Photoshop scripts.

  1. Size the photo collage templates for the print size you want - width, height and print DPI resolution.
  2. Photo collage templates must have a Photoshop background layer. The contents of this layer can be anything.
  3. Photo collage templates must have alpha channels named "Image 1", "Image 2", ... "Image n".
  4. Photo collage templates layers above the background layers must provide transparent areas to let the images that will be placed below them show through.

There are fifteen scripts in this package they provide the following functions:

  1. HelpPhotoCollageToolkit.jsx - Online Help
  2. TestCollageTemplate.jsx - Used to test a Photo Collage Template while you are making it with Photoshop.
  3. CollageTemplateBuilder.jsx - Can build Templates compatible with this toolkit's scripts.
  4. LayerToAlphaChan.jsx - Used to convert a Prototype Image Layer stack into a template document.
  5. InteractivePopulateCollage.jsx - Used to interactively populate Any Photo Collage template. Offers most user control inserting pictures and text.
  6. ReplaceCollageImage.jsx - use to replace a populated collage image Smart Object layer with an other image correctly resized and positioned.
  7. ChangeTextSize.jsx - This script can be used to change Image stamps text size when the size used by the populating did not work well.
  8. PopulateCollageTemplate.jsx - Used to Automatically populate a Photo Collage template and leave the populated copy open in Photoshop.
  9. BatchOneImageCollage.jsx - Used to Automatically Batch Populate Collage templates that only have one image inserted. The Collage or Image may be stamped with text.
  10. BatchMultiImageCollage.jsx - Used to Automatically Batch Populate Any Photo Collage template with images in a source image folder. Easier to use than the interactive script. Saved collages can be tweaked.
  11. PasteImageRoll.jsx - Paste Images into a document to be print on roll paper.
  12. BatchPicturePackage.jsx - Used to Automatically Batch Populate Any Photo Collage template with an image in a source image folder.  Images Rotates for best fit.
  13. BatchPicturePackageNoRotate.jsx - Used to Automatically Batch Populate Any Photo Collage template with an image in a source image folder.
  14. PopulatePicturePackage.jsx - Used to Automatically populate a Photo Collage template Fill with the same single image and leave the populated copy open in Photoshop.
  15. PCTpreferences.jsx - Edit This File to Customize Collage Populating scripts default setting and add your own Layer styles.

Documentation and Examples

JJMack