Skip to main content
Inspiring
November 11, 2024
Answered

Script (JS) : Loading file directly to a layer

  • November 11, 2024
  • 1 reply
  • 446 views

Hi all,

 

In my script, I load files to process them one by one. The script is like :

// folder selection
sourceFolder = Folder.selectDialog("Please select your source files directory");
sourceFilesList = sourceFolder.getFiles(/\.(jpg|png|tif|jpeg)$/i);
numFiles = sourceFilesList.length;
sourceFilesList.sort();

// error checks
// (...)

// load dimensions and resulotion of the files in the folder 
// (they all have the same characteristics)
var sourceDoc = app.open(sourceFilesList[0]);
sourceDocWidth = sourceDoc.width;
sourceDocHeight = sourceDoc.height;
resolution = sourceDoc.resolution;

// create the result image
resultDoc = app.documents.add(sourceDocWidth, sourceDocHeight, resolution, et_resultFile.text);

for (var i = 0; i < numFiles; i++) {

    // load image file
    var sourceDoc = app.open(sourceFilesList[i]);

    // copy and paste the image to a layer in the result file
    sourceDoc.selection.selectAll();
    sourceDoc.selection.copy();
    app.activeDocument = resultDoc;
    resultDoc.paste();
    sourceDoc.close(SaveOptions.DONOTSAVECHANGES);

    // now do something with the layer in the result file
    layer = resultDoc.activeLayer;

    // (...code here...)

 } // end for i loop

 You see that I load a file, then select it all, copy and paste in the work (result) file, then close the source file. It takes quite some time to do this and I can't find a way to open directly the source file in a new layer in the result file without copy/paste.

 

Any idea ?

 

Fred

This topic has been closed for replies.
Correct answer Fre5D95

OK, i found the solution...

 

I now use the duplicate() method.

 

The code is now :

 

// folder selection
sourceFolder = Folder.selectDialog("Please select your source files directory");
sourceFilesList = sourceFolder.getFiles(/\.(jpg|png|tif|jpeg)$/i);
numFiles = sourceFilesList.length;
sourceFilesList.sort();

// error checks
// (...)

// load dimensions and resulotion of the files in the folder 
// (they all have the same characteristics)
var sourceDoc = app.open(sourceFilesList[0]);
sourceDocWidth = sourceDoc.width;
sourceDocHeight = sourceDoc.height;
resolution = sourceDoc.resolution;

// create the result image
resultDoc = app.documents.add(sourceDocWidth, sourceDocHeight, resolution, "result");

for (var i = 0; i < numFiles; i++) {

    // load image file
    var sourceDoc = app.open(sourceFilesList[i]);

    // copy and paste the image to a layer in the result file
    // sourceDoc.selection.selectAll();
    // sourceDoc.selection.copy();
    // app.activeDocument = resultDoc;
    // resultDoc.paste();

// ***>>> here is the change ***
    sourceDoc.activeLayer.duplicate(resultDoc);
// ***>>> end of change      ***

    sourceDoc.close(SaveOptions.DONOTSAVECHANGES);

    // now do something with the layer in the result file
    layer = resultDoc.activeLayer;

    // (...code here...)

 } // end for i loop

I hope it will be usefull for other.

 

... that's quite complexe to look for full documentation on all the functions of Adobe scripting language...

 

Thanks

 

Fred

1 reply

Fre5D95AuthorCorrect answer
Inspiring
November 11, 2024

OK, i found the solution...

 

I now use the duplicate() method.

 

The code is now :

 

// folder selection
sourceFolder = Folder.selectDialog("Please select your source files directory");
sourceFilesList = sourceFolder.getFiles(/\.(jpg|png|tif|jpeg)$/i);
numFiles = sourceFilesList.length;
sourceFilesList.sort();

// error checks
// (...)

// load dimensions and resulotion of the files in the folder 
// (they all have the same characteristics)
var sourceDoc = app.open(sourceFilesList[0]);
sourceDocWidth = sourceDoc.width;
sourceDocHeight = sourceDoc.height;
resolution = sourceDoc.resolution;

// create the result image
resultDoc = app.documents.add(sourceDocWidth, sourceDocHeight, resolution, "result");

for (var i = 0; i < numFiles; i++) {

    // load image file
    var sourceDoc = app.open(sourceFilesList[i]);

    // copy and paste the image to a layer in the result file
    // sourceDoc.selection.selectAll();
    // sourceDoc.selection.copy();
    // app.activeDocument = resultDoc;
    // resultDoc.paste();

// ***>>> here is the change ***
    sourceDoc.activeLayer.duplicate(resultDoc);
// ***>>> end of change      ***

    sourceDoc.close(SaveOptions.DONOTSAVECHANGES);

    // now do something with the layer in the result file
    layer = resultDoc.activeLayer;

    // (...code here...)

 } // end for i loop

I hope it will be usefull for other.

 

... that's quite complexe to look for full documentation on all the functions of Adobe scripting language...

 

Thanks

 

Fred

Stephen Marsh
Community Expert
Community Expert
November 11, 2024

Sorry, a bit late to the party!

 

Here is an example where I use duplicate:

 

https://community.adobe.com/t5/photoshop-ecosystem-discussions/combining-tiffs/m-p/13475955

 

Here is an example where both place embedded and place linked are used:

 

https://community.adobe.com/t5/photoshop-ecosystem-discussions/layers-creating-layers/td-p/13252109

 

If files have different PPI resolution values, then the sizing of placed images will change. There are various ways to work around this, with later versions of Photoshop offering:

 

 

executeAction(stringIDToTypeID("placedLayerResetTransforms"), undefined, DialogModes.NO);

 

Participant
November 12, 2024

Thanks Stephen for your answer and your remark on resolution.

 

In my script, I assume that all files in the source have the same dimensions (width, height), resolution (ppi) and format (jpg, tif, png), I already implemented a check for inconsistency on dimensions, but will add one on resolution.

 

However, as you, I have placed at the beginning of the script :

var savedRuler = app.preferences.rulerUnits;
app.preferences.rulerUnits = Units.PIXELS;

(my code)

// last line of code
app.preferences.rulerUnits = savedRuler;

 

But this is not very effective if the user choose to abort the script with a long press on [Esc]... this is why I will (try to) make my code independant of the ruler settings, calculating the proper conversion factors when needed.

 

Fred

 

ps. my script is to make timeslices, see my IG @7361438_photo for an example.