Click here to Skip to main content
15,881,172 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I'm currently researching on Photoshop scripting.
I'm trying to place an image/psd/ai file into an active document.

The closest I got to so far is the code below, which opens the file in a separate document, copies and pastes it into the originally active document.

However, as you all know copy and pasting rasterizes the layer.

Is there a way to do place it?
(Javascript only)

Thanks

JavaScript
var docRef = app.activeDocument;
file = app.openDialog();//opens dialog,choose one image

if(file[0]){ //if you have chosen an image
   app.load(file[0]); //load it into documents
   backFile= app.activeDocument; //prepare your image layer as active document
   backFile.resizeImage(200,200); //resize image into given size
   backFile.selection.selectAll();
   backFile.selection.copy(); //copy image into clipboard
   backFile.close(SaveOptions.DONOTSAVECHANGES); //close image without saving changes
   docRef.paste(); //paste selection into your document
   docRef.layers[0].name = "BackgroundImage"; //set your layer's name
}
Posted
Updated 11-May-22 7:26am
Comments
Sergey Alexandrovich Kryukov 3-Mar-15 8:50am    
Why Photoshop and not, say, GIMP of something else? You probably need to understand: Photoshop is very feature-rich (as I understand, Adobe actively bought components), but is proprietary and awkward to work with. All Adobe produce is enormously heavy. They do understand image processing well, but all their programming products are pretty bad, even the PDF viewer. Do you really plan to use some unique Photoshop features? Then your work may make sense. But those features become less and less unique with time... Just some factors for you to consider...
—SA
LighthouseCall 3-Mar-15 10:18am    
Yes I'll be using very particular features for my projects. Thanks for your reply, however help with JavaScript is what I need at the moment.
RedDk 3-Mar-15 13:24pm    
Don't stop using the "Photoshop" tag just because someone here in Q&A says don't use it ...

Also, search for "Photoshop (your CS version here: X) JavaScript Ref" on the internet. Download this .pdf. It's at least a few MB in size and it's chock-full of stuff a javascript/photoshop user would need to know. And of course, while you're at the Adobe site illustrating the use of it's more than generous exposure to a programming platform such as javascript, check out "ActionScript". Which might or might not be something new at this point (2010 >>>).

Solved this and put it up here for other people. Used a scriptListener to do it.

It currently uses openDialog for file selection, however one can always place a path in the variable selectedFile.

var idPlc = charIDToTypeID( "Plc " ); 
var desc11 = new ActionDescriptor();  
var idnull = charIDToTypeID( "null" );
 
var selectedFile = app.openDialog();
//opens dialog to select file

desc11.putPath( idnull, new File(selectedFile) );
var idFTcs = charIDToTypeID( "FTcs" ); 
var idQCSt = charIDToTypeID( "QCSt" );   
var idQcsa = charIDToTypeID( "Qcsa" ); 
desc11.putEnumerated( idFTcs, idQCSt, idQcsa );
var idOfst = charIDToTypeID( "Ofst" );     
var desc12 = new ActionDescriptor();     
var idHrzn = charIDToTypeID( "Hrzn" );    
var idPxl = charIDToTypeID( "#Pxl" );      
desc12.putUnitDouble( idHrzn, idPxl, 0.000000 );     
var idVrtc = charIDToTypeID( "Vrtc" );    
var idPxl = charIDToTypeID( "#Pxl" );    
desc12.putUnitDouble( idVrtc, idPxl, 0.000000 );
var idOfst = charIDToTypeID( "Ofst" );
desc11.putObject( idOfst, idOfst, desc12 );
executeAction( idPlc, desc11, DialogModes.NO );
 
Share this answer
 
This works great. I added a repeat loop so I can hold down the shift key and select multiple files to be placed.

var idPlc = charIDToTypeID( "Plc " ); 
var desc11 = new ActionDescriptor();  
var idnull = charIDToTypeID( "null" );

//Opens dialog to select file
var selectedFiles = app.openDialog();

//Looping through the selected files
for(var i=0;i<selectedFiles.length;i++){

    //Storing each file path in a variable
    var selectedFile = selectedFiles[i];

    //Placing each embedded file
    desc11.putPath( idnull, new File(selectedFile) );
    var idFTcs = charIDToTypeID( "FTcs" ); 
    var idQCSt = charIDToTypeID( "QCSt" );   
    var idQcsa = charIDToTypeID( "Qcsa" ); 
    desc11.putEnumerated( idFTcs, idQCSt, idQcsa );
    var idOfst = charIDToTypeID( "Ofst" );     
    var desc12 = new ActionDescriptor();     
    var idHrzn = charIDToTypeID( "Hrzn" );    
    var idPxl = charIDToTypeID( "#Pxl" );      
    desc12.putUnitDouble( idHrzn, idPxl, 0.000000 );     
    var idVrtc = charIDToTypeID( "Vrtc" );    
    var idPxl = charIDToTypeID( "#Pxl" );    
    desc12.putUnitDouble( idVrtc, idPxl, 0.000000 );
    var idOfst = charIDToTypeID( "Ofst" );
    desc11.putObject( idOfst, idOfst, desc12 );
    executeAction( idPlc, desc11, DialogModes.NO );

    }
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900