skip to Main Content

Is it possible to compare filenames for a set of files that are imported as Photoshop layers ?
I have a folder of 50 jpg images which I have used in a PSD file.
Now I want to check whether all the JPG files are used or not ?
Is it possible to do so ?

2

Answers


  1. In Javascript, it is possible to get some information related to PSD file layers using PSD.js library

    Login or Signup to reply.
  2. As I’ve said, Photoshop scripting can help you achieve this by using File Objects and basic javascript knowledge. I’ve modified my old script as you’ve desired and now it should work well with any nested groups and images.

    I highly encourage you to learn scripting and ask questions here wherever you feels confused.

    Save below code as ‘Script.jsx’ and run it from ‘File > Scripts > Browse’

    Update 2 : Now it saves log.txt file too as per you requested. P.S. Learn from this script and tweak it to your desired result.

    // Managing Document
    var docs = app.documents;
    
    // Progress Bar
    var win = new Window("window{text:'Progress',bounds:[100,100,400,150],bar:Progressbar{bounds:[20,20,280,31] , value:0,maxvalue:100}};");
    
    // assigning activeDocument
    if (docs.length != 0) {
        var docRef = app.activeDocument;
    
        // Defining the folder
        alert("You will be prompted for the folder containing your images.n" +
            "Files will be selected with a '.png'/'.jpg/.jpeg' on the end in the same folder.");
        var folder = Folder.selectDialog();
        if (!folder) {
            exit;
        }
    
        var photoFiles = folder.getFiles(/.(jpg|jpeg|png)$/i);
        var matchFiles = [];
        var photoFilesName = [];
        //Searching for used images
        var increment = parseFloat(0);
        var divider = parseFloat(100/photoFiles.length);
        win.show();
        for (var i = 0; i < photoFiles.length; i++) {
            increment = increment + divider;
            var indexPhotoName = removeExtension(photoFiles[i].displayName);
            photoFilesName.push(indexPhotoName);
            var doc = activeDocument;
            var curLayer;
            goThroughLayers(doc, indexPhotoName);
        }
    
        function goThroughLayers(parentLayer, targetName) {
            for (var i = 0; i < parentLayer.layers.length; i++) {
                curLayer = parentLayer.layers[i];
                doc.activeLayer = curLayer;
                if (curLayer.typename == 'LayerSet') {
                    goThroughLayers(curLayer, targetName)
                } else {
                    if (curLayer.name == targetName) {
                        // if (curLayer.name.match(/[e]/ig)) {
                            matchFiles.push(targetName);
                        // }
                    } //end if
                } //end else
            } //end loop
        } //end function
    
    
        function arr_diff(a1, a2) {
            var a = [],
                diff = [];
            for (var i = 0; i < a1.length; i++) {
                a[a1[i]] = true;
            }
            for (var i = 0; i < a2.length; i++) {
                if (a[a2[i]]) {
                    delete a[a2[i]];
                } else {
                    a[a2[i]] = true;
                }
            }
            for (var k in a) {
                diff.push(k);
            }
            return diff;
        }
    
        function removeExtension(str) {
            return str.split('.').slice(0, -1).join('.');
        }
    
        var missItems = arr_diff(matchFiles, photoFilesName);
        if (missItems.length > 0) {
            var missFolder = new Folder(photoFiles[0].path + '/Missed%20Files');
            if(!missFolder.exists){
                missFolder.create();
            }
            for (var y = 0; y < photoFiles.length; y++) {
                var photoTrimName = removeExtension(photoFiles[y].displayName);
                for( var x = 0; x < missItems.length ; x++){
                    if(photoTrimName == missItems[x]){
                        photoFiles[y].copy(new File(missFolder+'/'+photoFiles[y].displayName));
                    }
                }
            };
            win.close();
            alert("You've missed total " + missItems.length + " files. Press OK to open folder containing missing files. Log report is generated wherever PSD is saved.");
            var FileStr = "";
            for(var m=0; m<missItems.length; m++){
                FileStr = FileStr + 'n' + (m+1) + '. ' + missItems[m];
            }
            var str = "Your missed files are : " + FileStr;
            saveTxt(str);
            missFolder.execute();
        } else {
            win.close();
            saveTxt('All Photos are used');
            alert('All Photos are used');
        }
    } else {
        alert('Open atleast one document');
    }
    
    function saveTxt(txt)
    {
        var Name = "LogReport_" + app.activeDocument.name.replace(/.[^.]+$/, '');
        var Ext = decodeURI(app.activeDocument.name).replace(/^.*./,'');
        if (Ext.toLowerCase() != 'psd')
            return;
    
        var Path = app.activeDocument.path;
        var saveFile = File(Path + "/" + Name +".txt");
    
        if(saveFile.exists)
            saveFile.remove();
    
        saveFile.encoding = "UTF8";
        saveFile.open("e", "TEXT", "????");
        saveFile.writeln(txt);
        saveFile.close();
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search