Photoshop scripting: changing text of a text layer

javascript, layer, photoshop, scripting, text

Solution

EDIT for downvoters:

Please, for the sake of helping the community and avoiding misleading/wrong information (if I made any in this case) thus making StackOverflow a better place, add a comment below indicating what makes you think the code or the directions is worth downvoting for. If there's anything wrong or misleading, I will learn one more thing, for which I will be grateful.

First you will need to create an action.

- Save the following code with `.jsx` extension.

- Open one of those images you have

- Create a new action and push the record button below the panel if it's not already active

- Go to `File > Scripts > Browse` and select that script

- Stop action recording

- Go to the folder where the file was created and delete that new file

Then you will need to automate all that. With no open document,

- Go to `File > Automate > Batch`

- Select the necessary "Set" and "Action" names from the options

- For the "Source" keep the "Folder" selected, then select the folder with your layered files by clicking on the "Choose…" button

- That might not be necessary (depending on your color settings) but you can select the 3rd and 4th options: `Suppress File Open Options Dialogs` and `Suppress Color Profile Warnings`. Since at the time of recording you did not include the action of opening the file keep the 1st option `Override Action Open Commands` unselected. Otherwise it will not open any file, yet still, it will try to execute the script * number of your files. Select the 2nd option `Include All Subfolders` if necessary.

- Click the "OK" button.

An additional point for those using CS6: Adobe Developer Connection indicates that…

Adobe Photoshop CS6 does not install the Scripting folder. Please use the links below to install the Samples, Documentation and Scripting Listener plug-in manually.

function getTextLayer(target) {
// this basically loops all the layers to find the
// upmost text layer with the content #nn... and returns it
    if (target == null) return false;
    var layers      = target.layers,
        layerLen    = layers.length;
    for (var i = 0; i < layerLen; i++) {
        var layer       = layers[i],
            isLayerSet  = layer.typename == 'LayerSet',
            isValid     = layer.kind == LayerKind.TEXT &&
                          /^\s*#\d+\s*$/.test(layer.textItem.contents);
            // we're allowing spaces around the text just in case
        if (!isLayerSet && !isValid) continue;
        if (isLayerSet) {
            var found = getTextLayer(layer);
            if (found) return found;
        } else return layer;
    }
    return false;
}

var doc;
try {
    doc = app.activeDocument;
    // the front document
} catch(e) {}
var txtLayer = getTextLayer(doc);
// obviously, the text layer if found

if (txtLayer) {
    var num = txtLayer.textItem.contents.match(/\d+/)[0],
    // find the numeric part
        len = num.length,
    // find the length of the numeric part
        num = (parseInt(num,10)+1).toString();
    // add 1 to that number
    while (num.length < len) num = '0' + num;
    // and adjust length if necessary so that e.g.
    // 032 will not become 33 but it will become 033
    txtLayer.textItem.contents = '#' + num;
    // update layer content
    var ext = '.png',
        dir = decodeURI(doc.path) + '/png24',
        // to use the same directory where the layered file exists
        // just keep it as decodeURI(doc.path)
        // I added a folder here, which actually does not exist
        // but it doesn't matter because I'm making it create it
        // below in case there's no such directory.
        fileName = dir + '/' + num + ext,
        i = 0;
    if (!Folder(dir).exists) Folder(dir).create();
    // create the directory if it doesn't exist
    while (File(fileName).exists)
        fileName = dir + '/' + num + '-' + (++i) + ext;
    // if file with that name exists, add -n to the end of the name
    var file = new File(fileName),
        opts = new ExportOptionsSaveForWeb();
    with (opts) {
        format = SaveDocumentType.PNG;
        PNG8 = false;
    }
    doc.exportDocument(file, ExportType.SAVEFORWEB, opts);
    // save for web
}
if (doc) {
    doc.close(SaveOptions.DONOTSAVECHANGES);
    // close the original layered document without saving
}
doc = null;
// remove reference

Problem

Because I don't have enough time to learn all about PS-Scripting, I was wondering, if you may help me. It's very simple. I want to have a JS-Script, which changes the Text of the Top Layer. For example: The Text is "#005", the script should add 1, so it says "#006". After that, it should export (Save for Web & Devices w. transparency @ 1280x720) the file with the current number (006). Here's a screen of the layers (omg its in german!!11): imageshack.us/photo/my-images/706/helpal.png

Original source