Skip to main content
Participant
March 10, 2025
Question

Script to copy the name of multiple open documents, to a single text file

  • March 10, 2025
  • 3 replies
  • 338 views

Hi all! This is something I've been trying to brute force on my own by frankenstiening older posted scripts, but my lack of scripting know-how is really getting in the way.

 

For context, my work requires me to manually open a large amount of  photos, then use 'Open in explore' to then copy its file name, THEN to find it in another folder by using the windows search bar to copy it to a final folder location. And this is PER image.

As you might be able to tell, this take a considerate amount of time for a task that could be automated.

 

I was wondering if it would at all be possible to have a script that copies the file name of the current opened document, and then pastes it to a text file.

 

The text file would look like this;

"1.jpg" OR "2.jpg" OR "3.jpg" *etc etc*

as the windows file search tool allows multiple file searches in this format, and all I would have to do is copy the contents of the text document and paste it into search, instead of having to manually 'open in explore' all 170 images per work-order.

 

Of course, this means it would have to do this for each opened document (sequential order is not nessasary) as previous scripts i've looked into use '/n' to copy each name under a new line.

 

Thank you for any feedback, looking forward to broadening my knowlege in this topic.

🙂

3 replies

Stephen Marsh
Community Expert
Community Expert
March 10, 2025
quote

For context, my work requires me to manually open a large amount of  photos


By @noah_7511

 

Opening 170 files at once isn't a great workflow. Depending on workflow and edits, if all of your images are in a single folder, a script might be better at opening them in smaller batches while the script keeps track of the next file(s) to open.

https://community.adobe.com/t5/photoshop-ecosystem-discussions/how-to-open-a-number-of-images-but-one-at-a-time/m-p/13804564#U15090907

 

 

If you must open so many images, you might find the following script to be useful to remember all of the open files to streamline opening them again:

 

 

P.S. Do you even need to open the files into Photoshop? Are they all in the same folder? Could Adobe Bridge be used to select and copy the files rather than opening them in Photoshop?

Stephen Marsh
Community Expert
Community Expert
March 10, 2025

@noah_7511 wrote:

Hi all! This is something I've been trying to brute force on my own by frankenstiening older posted scripts, but my lack of scripting know-how is really getting in the way.

 

For context, my work requires me to manually open a large amount of  photos, then use 'Open in explore' to then copy its file name, THEN to find it in another folder by using the windows search bar to copy it to a final folder location. And this is PER image.

As you might be able to tell, this take a considerate amount of time for a task that could be automated.


 

This could be scripted, i.e. the selection of the output folder, then the script copying the open files to the folder.

 

EDIT: At a basic level, something like this native JS .copy command. It's assumed that the files are saved and ready to copy.

 

/*
Copy open files in Photoshop to a destination folder.jsx
Stephen Marsh
v1.0 - 10th March 2025
https://community.adobe.com/t5/photoshop-ecosystem-discussions/script-to-copy-the-name-of-multiple-open-documents-to-a-single-text-file/td-p/15201067
*/

#target photoshop

var destinationFolder = selectDestinationFolder();
var openDocs = app.documents.length;
var counter = 0;

// Loop over all open documents
for (var i = 0; i < app.documents.length; i++) {
    var doc = app.documents[i];
    var docPath = doc.fullName.fsName; // Get the full path of the document in the OS-specific format
    //alert(docPath);
    copyFile(docPath, destinationFolder.fsName); // Use the OS-specific path of the destination folder
    counter++;
}

// End of script notification
app.beep();
alert("Script completed!" + "\r" + "Open document count: " + openDocs + "\n" + "Files copied: " + counter);


function selectDestinationFolder() {
    var destinationFolder = Folder.selectDialog("Select the destination folder");
    if (destinationFolder == null) {
        //alert("No destination folder selected. Script cancelled.");
    }
    return destinationFolder;
}

function copyFile(sourceFilePath, destinationFolderPath) {
    var sourceFile = new File(sourceFilePath);
    var destinationFile = new File(destinationFolderPath + "/" + sourceFile.name);
    sourceFile.copy(destinationFile);
}

 

Stephen Marsh
Community Expert
Community Expert
March 10, 2025

This updated 1.1 version uses native Mac and Windows copy commands. In the case of the Mac, coloured Finder labels and Finder Comments will be preserved.

 

/*
Copy open files in Photoshop to a destination folder 1-1.jsx
Stephen Marsh
v1.1 - 10th March 2025
Original discussion:
https://community.adobe.com/t5/photoshop-ecosystem-discussions/script-to-copy-the-name-of-multiple-open-documents-to-a-single-text-file/td-p/15201067
*/

#target photoshop

var destinationFolder = selectDestinationFolder();
if (destinationFolder) {
    var openDocs = app.documents.length;
    var initialFileCount = getFileCount(destinationFolder);

    // Loop over all open documents
    for (var i = 0; i < app.documents.length; i++) {
        var doc = app.documents[i];
        var docPath = doc.fullName.fsName; // Get the full path of the document in the OS-specific format
        //alert(docPath);
        if (docPath) {
            // If the file copy fails, alert to the user with the path of the file that failed to copy
            if (!copyFile(docPath, destinationFolder.fsName)) {
                alert("Failed to copy: " + docPath);
            }
        }
    }

    var finalFileCount = getFileCount(destinationFolder);
    var filesCopied = finalFileCount - initialFileCount;

    // End of script notification
    app.beep();
    alert("Script completed!" + "\r" +
        "Open document count: " + openDocs + "\n" + "Files copied: " + filesCopied);
    // Open the destination folder
    destinationFolder.execute();
}

function selectDestinationFolder() {
    var destinationFolder = Folder.selectDialog("Select the destination folder");
    if (destinationFolder == null) {
        //alert("No destination folder selected. Script cancelled.");
    }
    return destinationFolder;
}

// New function to count files in a folder
function getFileCount(folder) {
    var files = folder.getFiles();
    return files.length;
}

/*
Based on:
https://community.adobe.com/t5/photoshop-ecosystem-discussions/to-move-files-and-folders/td-p/8787869/page/2
by willcampbell7
*/
function copyFile(sourceFilePath, destinationFolderPath) {
    var command;
    var fileFrom = new File(new File(sourceFilePath).fsName);
    var fileTo = new File(destinationFolderPath + "/" + fileFrom.name);
    var folderTo = new Folder(destinationFolderPath);

    if (!folderTo.exists) {
        folderTo.create();
    }

    if (File.fs == "Macintosh") {
        // First copy the file
        command = "cp \"" + fileFrom.fsName + "\" \"" + fileTo.fsName + "\"";
        app.system(command);

        // Use a standalone AppleScript file for Finder comment copying
        var scriptContent = 'tell application "Finder"\n' +
            '  set sourceFile to POSIX file "' + fileFrom.fsName.replace(/\\/g, "\\\\").replace(/"/g, "\\\"") + '" as alias\n' +
            '  set destFile to POSIX file "' + fileTo.fsName.replace(/\\/g, "\\\\").replace(/"/g, "\\\"") + '" as alias\n' +
            '  set commentText to comment of sourceFile\n' +
            '  if commentText is not "" then\n' +
            '    set comment of destFile to commentText\n' +
            '  end if\n' +
            'end tell';

        // Create a temporary AppleScript file
        var scriptFile = new File("/tmp/copy_comment.applescript");
        scriptFile.encoding = "UTF-8";
        scriptFile.open("w");
        scriptFile.write(scriptContent);
        scriptFile.close();

        // Execute the script with proper permissions
        app.system("osascript /tmp/copy_comment.applescript");

        // Clean up
        app.system("rm /tmp/copy_comment.applescript");

    } else if (File.fs == "Windows") {
        command = "copy \"" + fileFrom.fsName + "\" \"" + fileTo.fsName + "\"";
        app.system(command);
    }

    if (fileTo.exists) {
        return true;
    } else {
        return false;
    }
}
c.pfaffenbichler
Community Expert
Community Expert
March 10, 2025
// copy filenames to clipboard;
// 2025, use it at your own risk;
if (app.documents.length > 1) {
var theFiles = app.documents;
var theString = "\""+theFiles[0].name+"\" OR ";
for (var m = 1; m < theFiles.length-1; m++) {
theString = theString+"\""+theFiles[m].name+"\" OR "
};
theString = theString+"\""+theFiles[theFiles.length-1].name+"\"";
copyTextToClipboard(theString);
};
function copyTextToClipboard( txt ) { 
const keyTextData = app.charIDToTypeID('TxtD');
const ktextToClipboardStr = app.stringIDToTypeID( "textToClipboard" ); 
var textStrDesc = new ActionDescriptor(); 
textStrDesc.putString( keyTextData, txt );
executeAction( ktextToClipboardStr, textStrDesc, DialogModes.NO ); 
};