Automate Batch Script - Convert filenames to text in Photoshop

前提是你 提交于 2019-12-08 12:25:03

问题


How do you convert each filename of a bunch of files, each to a text layer (and save) in Photoshop?

Folder 1: Hundereds of files

Folder 2: Nearly the same files, but each with its filename plastered onto its image

Here is a hacked up snippet. The error that I can't seem to get by is how to set the activeDocument to the currently open one. http://pastebin.com/b0fqG9v4


回答1:


Hasn't been marked as answered so here's a javascript example for CS (and should work on later versions too) in case anyone cares for such a thing. This opens each image file in a source folder and adds a new text layer. The name of the file is set as the content of the text layer, then the new file is saved to an output location.

It does not really try to fit the text on the image, that's certainly possible but complicated.

If you have color management set to warn you when opening a file with an embedded profile that doesn't match the working color space you will get a profile mismatch dialog. You could set your preferences handle such a situation automatically.

function main ()
{
    if (0 < app.documents.length)
    {
        alert ("Please close all open documents before running this script.");
        return;
    }

    // Use folder selection dialogs to get the location of the input files
    // and where to save the new output files.
    var sourceFolder = Folder.selectDialog ("Please choose the location of the source image files.", Folder.myDocuments);
    var destFolder = Folder.selectDialog ("Please choose a location where the new image files will be saved.", sourceFolder);

    var files = sourceFolder.getFiles();
    for (var i = 0; i < files.length; i++)
    {
        var f = files[i];
        if (f instanceof Folder)
            continue;

        // If there are no other documents open doc is the active document.
        var doc = app.open (f);
        var layer = doc.artLayers.add ();
        layer.bounds = [0,0,doc.width,doc.height];

        // Now make the layer into a text layer and set parameters.
        // The text will be centered, in the hideous default font, and white.
        // Note that font size depends not just on the point size, but also
        // on the resolution, which is NOT being set anywhere.
        layer.kind = LayerKind.TEXT;
        layer.textItem.position = [Math.round (doc.width / 2),Math.round (doc.height / 2)];
        layer.textItem.justification = Justification.CENTER;
        layer.textItem.color.rgb.hexValue = "FFFFFF";
        layer.textItem.size = 60;

        // Get the file name and set it as the text this assumes the full path is not wanted.
        // to set the full path swap fsname for name.
        layer.textItem.contents = File.decode (f.name);

        doc.flatten ();

        // Save as a new JPG file with these options.
        var options = new JPEGSaveOptions ();
        options.quality = 8;

        var outputFile = new File (destFolder.absoluteURI + "/" + f.name);
        doc.saveAs (outputFile, options, false, Extension.LOWERCASE);
        doc.close ();
    }
}



回答2:


Apparently, Photoshop is scriptable via Javascript, so with a dummy file as a model, you should be able to do what you need. Here are some resources about Photoshop scripting :

  • Official Photoshop scripting page
  • Tutorial about scripting


来源:https://stackoverflow.com/questions/4537316/automate-batch-script-convert-filenames-to-text-in-photoshop

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!