我想使用Photoshop
将文件名添加到图像中示例文件名
image 123-back.jpg image 123-front.jpg
我希望从添加到图像的名称中删除-back和-front
到目前为止,我可以添加文件名但无法删除-back和-front
这是我到目前为止的代码
// this script is a variation of the script addTimeStamp.js that is installed with PH7
if ( documents.length > 0 )
{
var originalDialogMode = app.displayDialogs;
app.displayDialogs = DialogModes.ERROR;
var originalRulerUnits = preferences.rulerUnits;
preferences.rulerUnits = Units.PIXELS;
try
{
var docRef = activeDocument;
// Now create a text layer at the front
var myLayerRef = docRef.artLayers.add();
myLayerRef.kind = LayerKind.TEXT;
myLayerRef.name = "Filename";
var myTextRef = myLayerRef.textItem;
// strip the extension off
var fileNameNoExtension = docRef.name;
fileNameNoExtension = fileNameNoExtension.split( "." );
if ( fileNameNoExtension.length > 1 ) {
fileNameNoExtension.length--;
}
fileNameNoExtension = fileNameNoExtension.join(".");
myTextRef.contents = '*' + fileNameNoExtension +'*';
// Set the position of the text percentages from left first, then from top
myTextRef.position = new Array( docRef.width / 2.5, docRef.height / 6 );
myTextRef.size = 15.76;
}
catch( e )
{
// An error occurred. Restore ruler units, then propagate the error back
// to the user
preferences.rulerUnits = originalRulerUnits;
app.displayDialogs = originalDialogMode;
throw e;
}
// Everything went Ok. Restore ruler units
preferences.rulerUnits = originalRulerUnits;
app.displayDialogs = originalDialogMode;
}
else
{
alert( "You must have a document open to add the filename!" );
}
答案 0 :(得分:0)
PhotoShop的JavaScript实现支持String replace()
方法。这可以用来满足您的要求。
1)使用replace()
以下代码段显示了replace()
方法的工作原理。
var string1 = "image 123-front"
var result1 = string1.replace("-front", "");
// --> The value for result1 equals: "image 123"
var string2 = "image 123-back"
var result2 = string2.replace("-back", "");
// --> The value for result2 equals: "image 123"
2)将replace()
与正则表达式
还可以为要匹配的字符串指定Regular Expression,如下所示:
var string3 = "image XYZ-front 123-back"
var result3 = string3.replace(/-(front|back)/g, "");
// --> The value for result3 equals: "image XYZ 123"
正则表达式部分/-(front|back)/g
与-front
和-back
都匹配。
可以找到此正则表达式的进一步说明here。
3)在您的脚本中实施replace()
为清楚起见,您的脚本可以修改如下。它利用上面例2中解释的解决方案:
if ( documents.length > 0 )
{
var originalDialogMode = app.displayDialogs;
app.displayDialogs = DialogModes.ERROR;
var originalRulerUnits = preferences.rulerUnits;
preferences.rulerUnits = Units.PIXELS;
try
{
var docRef = activeDocument;
// Now create a text layer at the front
var myLayerRef = docRef.artLayers.add();
myLayerRef.kind = LayerKind.TEXT;
myLayerRef.name = "Filename";
var myTextRef = myLayerRef.textItem;
// strip the extension off
var fileNameNoExtension = docRef.name;
fileNameNoExtension = fileNameNoExtension.split( "." );
if ( fileNameNoExtension.length > 1 ) {
fileNameNoExtension.length--;
}
fileNameNoExtension = fileNameNoExtension.join(".");
// Remove -front and -back from filename
fileNameNoExtension = fileNameNoExtension.replace(/-(front|back)/g, "");
myTextRef.contents = '*' + fileNameNoExtension +'*';
// Set the position of the text percentages from left first, then from top
myTextRef.position = new Array( docRef.width / 2.5, docRef.height / 6 );
myTextRef.size = 15.76;
}
catch( e )
{
// An error occurred. Restore ruler units, then propagate the error back
// to the user
preferences.rulerUnits = originalRulerUnits;
app.displayDialogs = originalDialogMode;
throw e;
}
// Everything went Ok. Restore ruler units
preferences.rulerUnits = originalRulerUnits;
app.displayDialogs = originalDialogMode;
}
else
{
alert( "You must have a document open to add the filename!" );
}
注意:从第一行开始27仅添加了以下代码段:
// Remove -front and -back from filename
fileNameNoExtension = fileNameNoExtension.replace(/-(front|back)/g, "");