Search code examples
javascriptscalephotoshopphotoshop-script

Photoshop JavaScript images do not place consistently


Currently I'm writing a script that's purpose is to import an image of any size and DPI on a new layer, apply a layer mask, and scale it to to fill the image (for example, if the imported image is in landscape orientation, the height would be scaled until it was equal to the document height). Unfortunately, Photoshop places images at a seemingly random scaling. Using scriptlistener, if I place an image, and change the percentage scaling (the percentages up at the top underneath the file, edit, image, etc.... ribbon) to 100% for both width and height, I get this:

var idPlc = charIDToTypeID( "Plc " );
var desc144 = new ActionDescriptor();
var idIdnt = charIDToTypeID( "Idnt" );
desc144.putInteger( idIdnt, 44 );
var idnull = charIDToTypeID( "null" );
desc144.putPath( idnull, new File( "C:\\Users\\Cameron Anderson\\Desktop\\Junk\\2hu chibis\\Wallpapers\\Exports\\1920x1080\\New folder\\Alice.png" ) );
var idFTcs = charIDToTypeID( "FTcs" );
var idQCSt = charIDToTypeID( "QCSt" );
var idQcsa = charIDToTypeID( "Qcsa" );
desc144.putEnumerated( idFTcs, idQCSt, idQcsa );
var idOfst = charIDToTypeID( "Ofst" );
    var desc145 = new ActionDescriptor();
    var idHrzn = charIDToTypeID( "Hrzn" );
    var idPxl = charIDToTypeID( "#Pxl" );
    desc145.putUnitDouble( idHrzn, idPxl, -0.000000 );
    var idVrtc = charIDToTypeID( "Vrtc" );
    var idPxl = charIDToTypeID( "#Pxl" );
    desc145.putUnitDouble( idVrtc, idPxl, -0.000000 );
var idOfst = charIDToTypeID( "Ofst" );
desc144.putObject( idOfst, idOfst, desc145 );
var idWdth = charIDToTypeID( "Wdth" );
var idPrc = charIDToTypeID( "#Prc" );
desc144.putUnitDouble( idWdth, idPrc, 108.843537 );
var idHght = charIDToTypeID( "Hght" );
var idPrc = charIDToTypeID( "#Prc" );
desc144.putUnitDouble( idHght, idPrc, 108.853411 );
var idLnkd = charIDToTypeID( "Lnkd" );
desc144.putBoolean( idLnkd, true );
executeAction( idPlc, desc144, DialogModes.NO );

From what I've learned so far of script listener, I believe that the two lines similar to desc144.putUnitDouble( idWdth, idPrc, 108.843537 ); describe the percentage scaling, given that the lines above them give the TypeID "#Prc", but I don't understand why it's 108.843537 instead of 100, like it should be.

Is there any way to set photoshop up to place in a consistent manner? Or at the very least, is it possible to get all the placed images to be placed at 100% instead of whatever is happening behind-the-scenes causing the code numbers to be weird?

The final script needs to be able to handle an image of any reasonable dimensions and DPI, as I can find a ratio between the document DPI and the placed image to properly scale them.

Thanks in advance!!


Solution

  • I have found the solution! As it turns out, you can actually disable Photoshop's ability to scale on place in the preferences menu, thus removing the arbitrary nature of the place command. Everything works as expected. Big ups to this thread on the adobe forums, as it solved the problem I was having completely.