Skip to main content
June 6, 2012
Question

How do you "Paste In Place" in javascript?

  • June 6, 2012
  • 2 replies
  • 4184 views

I'm seeing a problem when running copy merged and then paste from script.  If there is no alpha component in the mask, it looks like photoshop internally crops the layer to useful content.  Then when you paste, it pastes only the useful content meaning meanin it's translated so pixels at the top and left have content.  This is really stupid default behavior.  However, the UI has a way to work around this, you run Edit | Paste In Place and it keeps the translation component it figured out when running copy merged.  Great.

Unfortunately since I'm working from script I don't see a way of running Paste In Place as it is not in the commands exposed on Document.  I tried looking at the javascript recording and there appears to be nothing output for that specific command.  Argh!  Does anyone know if photoshop has a way to script this?

Otherwise, I'm gonna have to look at a completely different way of recording out this stuff.

Thanks,

Adrian

This topic has been closed for replies.

2 replies

JJMack
Community Expert
Community Expert
June 7, 2012

var selectedRegion = Array(Array(x,y), Array(x+width,y), Array(x+width,y+height), Array(x,y+height));

app.activeDocumen.selection.select(selectedRegion);   //select area to paste into

app.activeDocument.paste(true);                                         // [paste into] true

JJMack
June 7, 2012

Yep, I had found that before posting. Unfortunately since photoshop is cropping internally, I have no way of getting the correct x,y.

Thanks though,

Adrian

JJMack
Community Expert
Community Expert
June 7, 2012

Before copying the merged layer to the clipboard try getting the layers bounds to get size and location.

var startRulerUnits = app.preferences.rulerUnits;

app.preferences.rulerUnits = Units.PIXELS; // tell ps to work with pixels

var LB = app.activeDocument.activeLayer.bounds;

app.activeDocument.selection.selectAll()

app.activeDocument.selection.copy()

var selectedRegion = Array(Array(LB[0].value,LB[1].value),Array(LB[2].value,LB[1].value),Array(LB[2].value,LB[3].value),Array(LB[0].value,LB[3].value));

app.activeDocument.selection.select(selectedRegion);   //select area to paste into

app.activeDocument.paste(true);                                          // [paste into] true

app.preferences.rulerUnits = startRulerUnits;

JJMack
June 6, 2012

Nevermind.  Apparently running it from the menu doesn't output script information, but using the hotkey does.  WTF?  In case anyone is curious here's the script it output

// =======================================================

var idpast = charIDToTypeID( "past" );

    var desc557 = new ActionDescriptor();

    var idinPlace = stringIDToTypeID( "inPlace" );

    desc557.putBoolean( idinPlace, true );

    var idAntA = charIDToTypeID( "AntA" );

    var idAnnt = charIDToTypeID( "Annt" );

    var idAnno = charIDToTypeID( "Anno" );

    desc557.putEnumerated( idAntA, idAnnt, idAnno );

executeAction( idpast, desc557, DialogModes.NO );

Thanks,

Adrian