最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Photoshop scripting: changing text of a text layer - Stack Overflow

programmeradmin1浏览0评论

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

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

Share Improve this question edited Jan 29, 2013 at 13:58 asked Jan 28, 2013 at 20:51 user2019559user2019559 6
  • 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 Commented Jan 29, 2013 at 2: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 Commented Jan 30, 2013 at 0: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. – user2019559 Commented Jan 30, 2013 at 19:29
  • I already edited my answer. Why don't you check it? – inhan Commented Jan 30, 2013 at 19:34
  • Did it already, just want to say it ;) Thank you very much! – user2019559 Commented Jan 30, 2013 at 20:44
 |  Show 1 more comment

1 Answer 1

Reset to default 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
发布评论

评论列表(0)

  1. 暂无评论