((脚本)Photoshop删除特殊字符

问题描述 投票:1回答:1

我有一个脚本(可能识别出很多偷来的零件),该脚本遍历选定的一组图像,复制图像和文件名并应用于Photoshop中的模板。一切正常,只是Photoshop会以某种方式从我的琴弦中去除变音符号,即Björn成为Bjorn。

“通过Photoshop内部的警报记录”(下面的第30行)显示,直到将其用作textItem.contents为止,它一直具有正确的字符串。

下面提供的代码,感谢您的帮助!

#target photoshop
app.displayDialogs = DialogModes.NO;

var templateRef = app.activeDocument;
var templatePath = templateRef.path;
var photo = app.activeDocument.layers.getByName("Photo"); // keycard_template.psd is the active document


// Check if photo layer is SmartObject;
if (photo.kind != "LayerKind.SMARTOBJECT") {
    alert("selected layer is not a smart object")
} else {
    // Select Files;
    if ($.os.search(/windows/i) != -1) {
        var photos = File.openDialog("Select photos", "*.png;*.jpg", true)
    } else {
        var photos = File.openDialog("Select photos", getPhotos, true)
    };
    if (photos.length) replaceItems();
}

function replaceItems() {
    for (var m = 0; m < photos.length; m++) {
        if (photos.length > 0) {
            // Extract name
            var nameStr = photos[m].name;
            var nameNoExt = nameStr.split(".");
            var name = nameNoExt[0].replace(/\_/g, " ");

            // Replace photo and text in template
            photo = replacePhoto(photos[m], photo);
            // alert(name);
            replaceText(templateRef, 'Name', name);        

            templateRef.saveAs((new File(templatePath + "/keycards/" + name + ".jpg")), jpgOptions, true);
        }
    }
}

// OS X file picker
function getPhotos(thePhoto) {
    if (thePhoto.name.match(/\.(png|jpg)$/i) != null || thePhoto.constructor.name == "Folder") {
        return true
    };
};

// JPG output options;
var jpgOptions = new JPEGSaveOptions();  
jpgOptions.quality = 12; //enter number or create a variable to set quality  
jpgOptions.embedColorProfile = true;   
jpgOptions.formatOptions = FormatOptions.STANDARDBASELINE;


// Replace SmartObject Contents
function replacePhoto(newFile, theSO) {
    app.activeDocument.activeLayer = theSO;
    // =======================================================
    var idplacedLayerReplaceContents = stringIDToTypeID("placedLayerReplaceContents");
    var desc3 = new ActionDescriptor();
    var idnull = charIDToTypeID("null");
    desc3.putPath(idnull, new File(newFile));
    var idPgNm = charIDToTypeID("PgNm");
    desc3.putInteger(idPgNm, 1);
    executeAction(idplacedLayerReplaceContents, desc3, DialogModes.NO);
    return app.activeDocument.activeLayer
};


// Replace text strings
function replaceText(doc, layerName, newTextString) {
    for (var i = 0, max = doc.layers.length; i < max; i++) {
        var layerRef = doc.layers[i];
        if (layerRef.typename === "ArtLayer") {
        if (layerRef.name === layerName && layerRef.kind === LayerKind.TEXT) {
            layerRef.textItem.contents = decodeURI(newTextString);
        }
        } else {
            replaceText(layerRef, layerName, newTextString);
        }
    }
}
javascript batch-processing photoshop photoshop-script
1个回答
0
投票

遇到相同的问题,并尝试了我开发人员工具箱中的所有内容……大约3个小时!没有任何成功,然后我发现了一点小技巧!似乎photoshop是uri编码的文件名,但不要以允许我们执行decodeURI()并取回那些特殊字符的方式来执行。

例如,应为“ e%CC%81”,而不是应为“é”的“%C3%A9”。因此,我要做的是将文件名替换为正确的UTF-8代码,然后替换为encodeURI。例子:

var fileName = file.name
var result = fileName.replace(/e%CC%81/g, '%C3%A9') // allow é in file name
var myTextLayer.contents = decodeURI(result);

然后,您可以从文本层的文件名中成功获取特殊字符和重音。

您可以为我需要的每个字符进行替换:

'e%CC%81': '%C3%A9', //é
'o%CC%82': '%C3%B4', //ô
'e%CC%80': '%C3%A8', //è
'u%CC%80': '%C3%B9', //ù
'a%CC%80': '%C3%A0', //à
'e%CC%82': '%C3%AA' //ê

我从此HTML URL编码参考中获取了UTF-8代码:https://www.w3schools.com/tags/ref_urlencode.asp

希望它有一天能对某人有所帮助,因为此错误在线上不存在。

© www.soinside.com 2019 - 2024. All rights reserved.