Photoshop脚本:更改文本图层的文本 [英] Photoshop scripting: changing text of a text layer
问题描述
因为我没有足够的时间来学习PS-Scripting,所以我想知道,如果你能帮助我的话。
Because I don't have enough time to learn all about PS-Scripting, I was wondering, if you may help me.
这很简单。我想要一个JS脚本,它改变顶层的文本。
例如:Text是#005,脚本应该加1,所以它说#006。
之后,它应该使用当前号码(006)导出(Save for Web& Devices w.d透明度@ 1280x720)。
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).
这是一个层的屏幕( omg its in german !! 11 ): imageshack.us /photo/my-images/706/helpal.png
Here's a screen of the layers (omg its in german!!11): imageshack.us/photo/my-images/706/helpal.png
推荐答案
编辑downvoters:
请,为了帮助社区并避免误导/错误的信息(如果我在这种情况下做了任何话)这样做StackOverflow是一个更好的地方,在下面添加一条评论,指出是什么让你认为代码或方向是值得低估的。如果有任何错误或误导,我会再学习一件事,我将不胜感激。
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.
- 使用
.jsx $ c $保存以下代码c> extension。
- 打开其中一张图片
- 创建一个新动作并按下面板下方的记录按钮(如果它尚未激活)
- 转到
文件>脚本>浏览
并选择该脚本 - 停止操作记录
- 转到创建文件的文件夹并删除新文件文件
- 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,
- 转到
文件>自动化>批次
- 从选项中选择必要的设置和操作名称
- 对于来源保留选择文件夹,然后通过单击选择...按钮选择包含分层文件的文件夹
- 可能没有必要(取决于您的颜色设置)但您可以选择第3和第4个选项:
禁止文件打开选项对话框
和抑制颜色配置文件警告
。由于在录制时您没有包含打开文件的操作保持第一个选项覆盖操作打开命令
未选中。否则它将不会打开任何文件,但仍然会尝试执行文件的脚本*编号。如有必要,请选择第二个选项包括所有子文件夹
。 - 单击确定按钮。
- 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
andSuppress Color Profile Warnings
. Since at the time of recording you did not include the action of opening the file keep the 1st optionOverride 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 optionInclude All Subfolders
if necessary. - Click the "OK" button.
使用CS6的人的另一点: Adobe Developer Connection 表示...
Adobe Photoshop CS6不安装Scripting文件夹。请使用以下链接手动安装Samples,Documentation和Scripting Listener插件。
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
这篇关于Photoshop脚本:更改文本图层的文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!