Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
1

Renaming Pasted Layer from the Filename of a Separate File

Community Beginner ,
Feb 23, 2024 Feb 23, 2024

I have a script that iterates through PSD files in a folder (main folder), opening them one at a time and then referencing a another folder (reference folder) to find files that contain the same filename.  If it finds one or more, it opens those and adds them as layers in the original document.  However when it pastes them into the original file the layer names are "Background Copy", "Background Copy 1", etc.  I've tried a few different ways to rename the layer using replace, but nothing seems to work.  Can anyone tell me where I need to put that code in order to have the reference layers paste in with their original filename?

 

// Function to duplicate layers from one document to another
function duplicateLayers(sourceDocument, targetDocument) {
  for (var i = 0; i < sourceDocument.artLayers.length; i++) {
    var layer = sourceDocument.artLayers[i];
    layer.duplicate(targetDocument, ElementPlacement.PLACEATEND);
  }
}

// Main function to process each PSD file
function processFile(mainFilePath, layersFolderPath) {
  var mainDocument = app.open(new File(mainFilePath));

  // Extract the filename without extension
  var mainFileName = mainDocument.name.replace(/\.[^\.]+$/, '');

  // Iterate through the layers folder and open matching PSD files
  var layersFolder = new Folder(layersFolderPath);
  if (layersFolder.exists) {
    var fileList = layersFolder.getFiles();
    for (var i = 0; i < fileList.length; i++) {
      if (fileList[i] instanceof File && fileList[i].hidden == false) {
        var layerFile = fileList[i];
        var layerFileName = layerFile.name.replace(/\.[^\.]+$/, '');
        

        // Check if the layer file matches the main file
        if (layerFileName.indexOf(mainFileName) > -1) {
          var layerDocument = app.open(layerFile);

          // Duplicate layers from the layer document to the main document
          duplicateLayers(layerDocument, mainDocument);

          // Close the layer document without saving changes
          layerDocument.close(SaveOptions.DONOTSAVECHANGES);
        }
      }
    }
  } else {
    $.writeln("Layers folder does not exist: " + layersFolderPath);
  }

  // Save and close the main document
  mainDocument.save();
  mainDocument.close(SaveOptions.SAVECHANGES);
}

// Set Folder Locations
var mainFolderPath = Folder.selectDialog("Select the main folder:");
var layersFolderPath = Folder.selectDialog("Select the references folder:");

var mainFolder = new Folder(mainFolderPath);
if (mainFolder.exists) {
  var mainFileList = mainFolder.getFiles();
  for (var j = 0; j < mainFileList.length; j++) {
    if (mainFileList[j] instanceof File && mainFileList[j].hidden == false) {
      var mainFilePath = mainFileList[j].fsName;
      processFile(mainFilePath, layersFolderPath);
    }
  }
} else {
  $.writeln("Main folder does not exist: " + mainFolderPath);
}

 

TOPICS
Actions and scripting
223
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Community Expert , Feb 23, 2024 Feb 23, 2024

@mattmyles 

 

I have added the following:

 

// Rename the duped layer
app.activeDocument.activeLayer.name = layerFileName;

 

It appears to work as expected... Full code:

 

// Function to duplicate layers from one document to another
function duplicateLayers(sourceDocument, targetDocument) {
    for (var i = 0; i < sourceDocument.artLayers.length; i++) {
        var layer = sourceDocument.artLayers[i];
        layer.duplicate(targetDocument, ElementPlacement.PLACEATEND);
    }
}

// Main function
...
Translate
Adobe
Community Expert ,
Feb 23, 2024 Feb 23, 2024

@mattmyles 

 

I have added the following:

 

// Rename the duped layer
app.activeDocument.activeLayer.name = layerFileName;

 

It appears to work as expected... Full code:

 

// Function to duplicate layers from one document to another
function duplicateLayers(sourceDocument, targetDocument) {
    for (var i = 0; i < sourceDocument.artLayers.length; i++) {
        var layer = sourceDocument.artLayers[i];
        layer.duplicate(targetDocument, ElementPlacement.PLACEATEND);
    }
}

// Main function to process each PSD file
function processFile(mainFilePath, layersFolderPath) {
    var mainDocument = app.open(new File(mainFilePath));

    // Extract the filename without extension
    var mainFileName = mainDocument.name.replace(/\.[^\.]+$/, '');

    // Iterate through the layers folder and open matching PSD files
    var layersFolder = new Folder(layersFolderPath);
    if (layersFolder.exists) {
        var fileList = layersFolder.getFiles();
        for (var i = 0; i < fileList.length; i++) {
            if (fileList[i] instanceof File && fileList[i].hidden == false) {
                var layerFile = fileList[i];
                var layerFileName = layerFile.name.replace(/\.[^\.]+$/, '');


                // Check if the layer file matches the main file
                if (layerFileName.indexOf(mainFileName) > -1) {
                    var layerDocument = app.open(layerFile);

                    // Duplicate layers from the layer document to the main document
                    duplicateLayers(layerDocument, mainDocument);

                    // Close the layer document without saving changes
                    layerDocument.close(SaveOptions.DONOTSAVECHANGES);

                    // Rename the duped layer
                    app.activeDocument.activeLayer.name = layerFileName;
                }
            }
        }
    } else {
        $.writeln("Layers folder does not exist: " + layersFolderPath);
    }

    // Save and close the main document
    mainDocument.save();
    mainDocument.close(SaveOptions.SAVECHANGES);
}

// Set Folder Locations
var mainFolderPath = Folder.selectDialog("Select the main folder:");
var layersFolderPath = Folder.selectDialog("Select the references folder:");

var mainFolder = new Folder(mainFolderPath);
if (mainFolder.exists) {
    var mainFileList = mainFolder.getFiles();
    for (var j = 0; j < mainFileList.length; j++) {
        if (mainFileList[j] instanceof File && mainFileList[j].hidden == false) {
            var mainFilePath = mainFileList[j].fsName;
            processFile(mainFilePath, layersFolderPath);
        }
    }
} else {
    $.writeln("Main folder does not exist: " + mainFolderPath);
}

 

 

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Feb 24, 2024 Feb 24, 2024

Thank you.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Feb 24, 2024 Feb 24, 2024
LATEST

You're welcome!

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines