比较 JPG 文件与 Photoshop 图层

Comparing JPG files with Photoshop Layers

是否可以比较作为 Photoshop 图层导入的一组文件的文件名? 我有一个包含 50 张 jpg 图像的文件夹,我在 PSD 文件中使用了这些图像。 现在我想检查是否所有的 JPG 文件都被使用了? 可以这样做吗?

在Javascript中,可以使用PSD.js

获取一些与PSD文件图层相关的信息

正如我所说,Photoshop 脚本可以帮助您通过使用文件对象和基本 javascript 知识来实现​​这一点。我已经按照您的需要修改了我的旧脚本,现在它应该可以很好地处理任何嵌套组和图像。

我强烈建议您学习脚本编写,并在您感到困惑时在这里提问。

将下面的代码保存为 'Script.jsx' 和 运行 来自 'File > Scripts > Browse'

更新 2:现在它也会根据您的要求保存 log.txt 文件。 P.S。从此脚本中学习并将其调整为您想要的结果。

// 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();
}