javascript Photoshop 脚本:更改文本图层的文本

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14571008/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 21:59:40  来源:igfitidea点击:

Photoshop scripting: changing text of a text layer

javascripttextscriptingphotoshoplayer

提问by inhan

Because I don't have enough time to learn all about PS-Scripting, I was wondering, if you may help me.

因为我没有足够的时间来学习所有关于 PS-Scripting 的知识,我想知道您是否可以帮助我。

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).

这很简单。我想要一个 JS-Script,它改变了Top Layer的文本。例如:文本是“#005”,脚本应该加1,所以它说“#006”。之后,它应该导出(保存为 Web 和设备 w. 透明度 @ 1280x720)具有当前编号 (006) 的文件。

Here's a screen of the layers (omg its in german!!11): imageshack.us/photo/my-images/706/helpal.png

这是图层的屏幕(omg 它是德语!!11):imageshack.us/photo/my-images/706/helpal.png

回答by inhan

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 wrongor misleading, I will learn one more thing, for which I will be grateful.

为downvoters编辑:

,为了帮助社区并避免误导/错误信息(如果我在这种情况下提供了任何信息)从而使 StackOverflow 成为一个更好的地方,请在下面添加评论,指出是什么让您认为代码或方向值得反对。如果有任何错误或误导,我会再学习一件事,对此我将不胜感激。

First you will need to create an action.

首先,您需要创建一个动作。

  1. Save the following code with .jsxextension.
  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 > Browseand select that script
  5. Stop action recording
  6. Go to the folder where the file was created and delete that new file
  1. 使用.jsx扩展名保存以下代码。
  2. 打开您拥有的图像之一
  3. 创建一个新动作并按下面板下方的录制按钮(如果它尚未激活)
  4. 转到File > Scripts > Browse并选择该脚本
  5. 停止动作录制
  6. 转到创建文件的文件夹并删除该新文件

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 Dialogsand Suppress Color Profile Warnings. Since at the time of recording you did not include the action of opening the filekeep the 1st option Override Action Open Commandsunselected. 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 Subfoldersif necessary.
  5. Click the "OK" button.
  1. File > Automate > Batch
  2. 从选项中选择必要的“设置”和“操作”名称
  3. 对于“源”保持选择“文件夹”,然后通过单击“选择...”按钮选择包含分层文件的文件夹
  4. 这可能不是必需的(取决于您的颜色设置),但您可以选择第三个和第四个选项:Suppress File Open Options DialogsSuppress Color Profile Warnings。由于在录制时您没有包含打开文件的操作,因此请不要Override Action Open Commands选择第一个选项。否则它不会打开任何文件,但它仍然会尝试执行脚本 * 您的文件数。Include All Subfolders如有必要,请选择第二个选项。
  5. 单击“确定”按钮。

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

对于使用 CS6 的人来说,还有一点:Adobe Developer Connection表明……

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.

Adobe Photoshop CS6 不安装 Scripting 文件夹。请使用以下链接手动安装示例、文档和脚本侦听器插件。

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