10

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

  • 1) What do you mean by exporting? "Save As" or "Save"? 2) Is this (#num) the only content in your text layer? 3) Is that the topmost layer? 4) Is there any parent folder holding this text layer? A screenshot of the layers palette would help a lot. – inhan Jan 29 '13 at 02:42
  • If the "@ 1280x720" part indicates the size that is different than the original size of your files then the script will need some extra for this purpose. Note that PHS scripts need **exact** details. – inhan Jan 30 '13 at 00:33
  • @inhan 1280x720 is the original size of the image. Yeah, I cuold maybe use `Save as` also, but this is just an option, if exporting is more complex. –  Jan 30 '13 at 19:29
  • I already edited my answer. Why don't you check it? – inhan Jan 30 '13 at 19:34
  • Did it already, just want to say it ;) Thank you very much! –  Jan 30 '13 at 20:44
  • As I mentioned earlier, each possibility is a parameter in coding, so for example "save as" itself does not mean much. You need to indicate all the settings that you are provided in small windows while saving. Now that it's working as desired you can accept my answer below to encourage me. – inhan Jan 30 '13 at 23:05

1 Answers1

16

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.

  1. Save the following code with .jsx extension.
  2. Open one of those images you have
  3. Create a new action and push the record button below the panel if it's not already active
  4. Go to File > Scripts > Browse and select that script
  5. Stop action recording
  6. 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,

  1. Go to File > Automate > Batch
  2. Select the necessary "Set" and "Action" names from the options
  3. For the "Source" keep the "Folder" selected, then select the folder with your layered files by clicking on the "Choose…" button
  4. 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.
  5. 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
Community
  • 1
  • 1
inhan
  • 7,394
  • 2
  • 24
  • 35
  • So, I'm using CS5 Extended, and I want the script to export (Save for Web & Devices...) the file as PNG-24 with transparency @ 1280x720px. The text layer, which should be edited, is the top layer, it's only content is e.g. #005 No folders, just 3 layers, here's a screen: http://imageshack.us/photo/my-images/706/helpal.png/ –  Jan 29 '13 at 13:41
  • Hi inhan! I tried it a few times, but it doesn't seem to work. –  Apr 01 '13 at 12:13
  • I tried it a few times, but it doesn't seem to work the way I want it to. Perhaps I did not express myself clearly enough: Thumb.psd has a text layer, which has the following format: "#001" (w/o the quotes). I want to generate a few pictures (the Quantity can be changed in the script) with the same format, but counting up, so the exported image 002.png has the textlayer "#002", 003.png has "#003" and so on. When the specified Quantity is reached, the script stops, saving the latest value (e.g. #020) in Thumb.psd Maybe PS-Scripting isn't the right way, but I don't have any other ideas. Thanks! –  Apr 01 '13 at 12:22
  • 1
    Many lines helped to get into Photoshop Scripting! Thanks! – Rozkalns Sep 26 '13 at 22:01
  • 2
    Instead of declaring the big ```getTextLayer``` function, you can use the built in ```var txtLayer = doc.layers.getByName('#032');``` method (it searches a layer with a given name -- here "#032"). – romainsalles Aug 22 '15 at 11:56