Photoshop脚本编写:更改文本图层的文本

10
因为我没有足够的时间去学习所有关于PS-Scripting的知识,所以我想知道是否可以帮助我。 非常简单。我想要一个JS脚本,它可以更改顶部图层的文本。 例如:文本是"#005",脚本应该加1,这样就会显示"#006"。 然后,它应该使用当前数字(006)导出(保存为Web和设备w.透明度@ 1280x720)文件。 以下是图层的屏幕截图(天哪,它是德语!11):imageshack.us/photo/my-images/706/helpal.png

  1. 导出是什么意思?是“另存为”还是“保存”?
  2. 这个(#num)是你的文本层中唯一的内容吗?
  3. 那是最上面的图层吗?
  4. 有没有父文件夹包含这个文本层?图层面板的截图会很有帮助。
- inhan
如果“@ 1280x720”部分表示的大小与您的文件的原始大小不同,则脚本将需要一些额外的内容来完成此操作。请注意,PHS脚本需要精确的细节。 - inhan
@inhan 1280x720 是图像的原始尺寸。是的,我可能也可以使用“另存为”,但这只是一个选项,如果导出更复杂的话。 - user2019559
我已经编辑了我的答案。你为什么不去检查一下呢? - inhan
已经完成了,只是想说一下而已 ;) 非常感谢! - user2019559
正如我之前提到的,每个可能性都是编码中的一个参数,因此例如“另存为”本身并没有太多意义。您需要指示在保存时提供的所有设置,这些设置通常会出现在小窗口中。现在它已经按照您的要求工作了,您可以接受下面的答案以鼓励我。 - inhan
1个回答

16

针对投票者的编辑说明:

出于帮助社区和避免误导/错误信息(如果我在这种情况下犯了任何错误)从而使StackOverflow成为一个更好的地方,添加下面的评论,说明您认为代码或指示值得投票反对的原因。如果有任何错误或误导性的内容,我将学到更多东西,对此我将感激不尽。

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

  1. 使用.jsx扩展名保存以下代码。
  2. 打开您拥有的图像之一
  3. 创建一个新操作并按下面板下方的记录按钮(如果尚未激活)
  4. 转到文件 > 脚本 > 浏览,然后选择该脚本
  5. 停止操作记录
  6. 转到创建文件的文件夹并删除该新文件

然后,您需要自动化所有操作。没有打开的文档,

  1. 转到文件 > 自动化 > 批处理
  2. 从选项中选择必要的“设置”和“操作”名称
  3. 对于“源”,保持“文件夹”选定状态,然后通过单击“选择…”按钮选择带有图层文件的文件夹
  4. 这可能不是必要的(取决于您的颜色设置),但您可以选择第3和第4个选项:抑制文件打开选项对话框抑制颜色配置文件警告。由于在录制时您未包括打开文件的操作,因此保持第1个选项覆盖操作打开命令未选中。否则,它将不会打开任何文件,但仍然会尝试执行脚本*您的文件数量。如果需要,请选择第2个选项包括所有子文件夹
  5. 单击“确定”按钮。

对于那些使用CS6的人的额外说明:Adobe Developer Connection指出...

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

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

所以,我正在使用CS5 Extended,并且我希望脚本将文件导出(另存为Web和设备...)为带有透明度的PNG-24 @ 1280x720px。 应编辑的文本图层是顶部图层,它的唯一内容是例如#005。 没有文件夹,只有3个图层,这是一个屏幕截图:http://imageshack.us/photo/my-images/706/helpal.png/ - user2019559
嗨inhan!我尝试了几次,但似乎不起作用。 - user2019559
我尝试了几次,但它似乎不按照我想要的方式工作。 也许我没有表达清楚:Thumb.psd有一个文本图层,其格式如下:"#001"(不带引号)。我想生成几张图片(数量可以在脚本中更改),具有相同的格式,但是计数增加,因此导出的图像002.png具有文本层“#002”,003.png具有“#003”等等。当达到指定的数量时,脚本停止,并将最新值(例如#020)保存在Thumb.psd中。 也许PS-Scripting不是正确的方法,但我没有其他想法。 谢谢! - user2019559
1
许多行代码帮助我进入了Photoshop脚本编程!谢谢! - Rozkalns
2
不必声明大型的getTextLayer函数,您可以使用内置的var txtLayer = doc.layers.getByName('#032');方法(它会搜索具有给定名称的图层--这里是“#032”)。 - romainsalles

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接