使用Javascript在Photoshop中编辑文本层的内容
Editing Content of a Text Layer in Photoshop Using Javascript
我正在尝试编写一个脚本来编辑Photoshop CS6中文本层的内容。这可能吗?
我有大约2000个图像,我需要处理一个工作项目。首先,我使用我已经拥有的javascript在Photoshop中添加每个图像的文件名作为文本层(见下文)。示例文件名为"UCMC_0018015 D FSH E."。我的脚本成功地将此文件名作为Photoshop中的文本层添加到图像中。
但是,我想编辑文本层,用空格替换下划线,并从文本字符串的末尾删除"FSH E"(所有文件名都有这些元素,但文件名中的数字因文件而异)。有人能帮我写剧本吗?我刚开始写和运行脚本,但我正在尽我最大的努力在工作中学习。你能给我任何建议都将不胜感激。
这是我当前用于将文件名添加到图像的脚本。我不确定我是否可以编辑它,或者是否需要编写一个新的脚本来编辑文本层。谢谢你的帮助!
//Check if a document is open
if ( documents.length > 0 )
{
var originalRulerUnits = preferences.rulerUnits;
preferences.rulerUnits = Units.PERCENT;
try
{
var docRef = activeDocument;
// Create a text layer at the front
var myLayerRef = docRef.artLayers.add();
myLayerRef.kind = LayerKind.TEXT;
myLayerRef.name = "Filename";
var myTextRef = myLayerRef.textItem;
//Set your parameters below this line
//If you wish to show the file extension, change the n to y in the line below, if not use n.
var ShowExtension = "n";
// Insert any text to appear before the filename, such as your name and copyright info between the quotes.
//If you do not want extra text, delete between the quotes (but leave the quotes in).
var TextBefore = "";
// Insert any text to appear after the filename between the quotes.
//If you do not want extra text, delete between the quotes (but leave the quotes in).
var TextAfter = "";
// Set font size in Points
myTextRef.size = 30;
//Set font - use GetFontName.js to get exact name
myTextRef.font = "Times New Roman";
//Set text colour in RGB values
var newColor = new SolidColor();
newColor.rgb.red = 0;
newColor.rgb.green = 0;
newColor.rgb.blue = 0;
myTextRef.color = newColor;
// Set the position of the text - percentages from left first, then from top.
myTextRef.position = new Array( 75, 98);
// Set the Blend Mode of the Text Layer. The name must be in CAPITALS - ie change NORMAL to DIFFERENCE.
myLayerRef.blendMode = BlendMode.NORMAL;
// select opacity in percentage
myLayerRef.opacity = 100;
// The following code strips the extension and writes tha text layer. fname = file name only
di=(docRef.name).indexOf(".");
fname = (docRef.name).substr(0, di);
//use extension if set
if ( ShowExtension == "y" )
{
fname = docRef.name
}
myTextRef.contents = TextBefore + " " + fname + " " + TextAfter;
}
catch( e )
{
// An error occurred. Restore ruler units, then propagate the error back
// to the user
preferences.rulerUnits = originalRulerUnits;
throw e;
}
// Everything went Ok. Restore ruler units
preferences.rulerUnits = originalRulerUnits;
}
else
{
alert( "You must have a document open to add the filename!" );
}
您可以使用正则表达式删除所有空白并用下划线替换它们。据我所知,你可以先把"FSH E"替换成一个空字符串。如果这些字母不同,那么你将不得不使用不同的策略。但这将在目前起作用。这是您需要的代码的基本部分。
var myFileName = "UCMC_0018015 D FSH E";
// remove " FSH E"
myFileName = myFileName.replace(" FSH E", "");
// replace whitespce with underscores
myFileName = myFileName.replace(/'s/gi, "_");
alert(myFileName);
你的最终代码应该像这个
//Check if a document is open
if ( documents.length > 0 )
{
var originalRulerUnits = preferences.rulerUnits;
preferences.rulerUnits = Units.PERCENT;
try
{
var docRef = activeDocument;
// Create a text layer at the front
var myLayerRef = docRef.artLayers.add();
myLayerRef.kind = LayerKind.TEXT;
myLayerRef.name = "Filename";
var myTextRef = myLayerRef.textItem;
//Set your parameters below this line
//If you wish to show the file extension, change the n to y in the line below, if not use n.
var ShowExtension = false;
// Insert any text to appear before the filename, such as your name and copyright info between the quotes.
//If you do not want extra text, delete between the quotes (but leave the quotes in).
var TextBefore = "";
// Insert any text to appear after the filename between the quotes.
//If you do not want extra text, delete between the quotes (but leave the quotes in).
var TextAfter = "";
// Set font size in Points
myTextRef.size = 30;
//Set font - use GetFontName.js to get exact name
myTextRef.font = "Times New Roman";
//Set text colour in RGB values
var newColor = new SolidColor();
newColor.rgb.red = 0;
newColor.rgb.green = 0;
newColor.rgb.blue = 0;
myTextRef.color = newColor;
// Set the position of the text - percentages from left first, then from top.
myTextRef.position = new Array( 75, 98);
// Set the Blend Mode of the Text Layer. The name must be in CAPITALS - ie change NORMAL to DIFFERENCE.
myLayerRef.blendMode = BlendMode.NORMAL;
// select opacity in percentage
myLayerRef.opacity = 100;
// The following code strips the extension and writes tha text layer. fname = file name only
var fname = docRef.name;
// code changes here.
// remove " FSH E"
fname = fname.replace(" FSH E", "");
// replace whitespaces with underscores
fname = fname.replace(/'s/gi, "_");
//use extension if set
if ( ShowExtension == true )
{
di =(fname).lastIndexOf(".");
fname = (fname).substr(0, di);
}
myTextRef.contents = TextBefore + " " + fname + " " + TextAfter;
}
catch( e )
{
// An error occurred. Restore ruler units, then propagate the error back
// to the user
preferences.rulerUnits = originalRulerUnits;
throw e;
}
// Everything went Ok. Restore ruler units
preferences.rulerUnits = originalRulerUnits;
}
else
{
alert( "You must have a document open to add the filename!" );
}
- 作为JavasScript的新手,我想指出的是,您有一个字符串形式的showextension变量。它可能更容易成为布尔值。所以它只能是真或假。一根绳子可以,嗯。。。任何东西
- 另一点是,您使用indexOf来查找扩展。这会很好用的。除非你有一个文件名,比如"my.可爱.照片.jpg";而您的扩展名是"可爱的.photo.jpg"。您可能会猜测,使用lastIndexOf可以在字符串末尾附近找到项目的索引
相关文章:
- 文本编辑后,append函数不适用于文本区域
- Sitecore富文本编辑器-添加按钮
- 实时协作富文本编辑
- 正在寻找具有多种功能的文本编辑器
- 简单的Javascript文本编辑器或一组函数
- 具有.txt输出的富文本编辑器
- 文本编辑工具栏解决方案 (js),具有低级自定义
- 无法在实时服务器中发布富文本编辑器内容
- 是否可以在没有CMS的情况下使用富文本编辑器
- 用PHP文本编辑器压缩和合并JS文件
- HTML5中具有过程格式的单行文本编辑器
- TinyMCE文本编辑器最大字符限制
- Bootstrap 3的就地文本编辑组件
- 如何使用剑道富文本编辑器自动大写第一个字母
- 替换富文本编辑器中的 HTML 标记
- JQuery/Javascript源;图像模板”;文本编辑
- JQuery文本编辑器-在工具栏中添加其他下拉列表
- Redactor文本编辑器– 在代码视图中重新缩进缩小的HTML
- YUI富文本编辑器和Chrome中的同源策略
- 使用JQUERY/Javascript获取DotNetNuke文本编辑器HTML值