Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
398 views
in Technique[技术] by (71.8m points)

javascript - (脚本)Photoshop删除特殊字符((Scripting) Photoshop removes special characters)

I have a script (with a lot of stolen parts you may recognise) that runs through a selected group of images, copies the image and filename and applies to a template in Photoshop.

(我有一个脚本(可能识别出许多偷来的零件),遍历选定的一组图像,复制图像和文件名,并应用于Photoshop中的模板。)

Everything works just fine, except that Photoshop somehow strips umlauts from my strings, ie, Bj?rn becomes Bjorn.

(一切工作正常,只是Photoshop不知何故从我的琴弦上去除了变音符号,即Bj?rn成为Bjorn。)

"Logging" through an alert inside of Photoshop (line #30 below) shows that it has the correct string all the way until it's applied as the textItem.contents.

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

Code provided below, thanks for any help!

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

#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);
        }
    }
}
  ask by Micke Alm translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Had the same problem and tried everything I had in my developer toolbox... for around 3 hours !

(遇到了同样的问题,尝试了我开发人员工具箱中的所有内容……大约3个小时!)

without any success and then I found a little hack !

(没有任何成功,然后我发现了一点小技巧!)

It seems that photoshop is uri encoding the name of the file but don't do it in a way that allow us to do a decodeURI() and get back those special characters.

(看来photoshop是用uri编码文件名的,但不要以允许我们执行的URI的方式进行解码URI()并取回那些特殊字符。)

For exemple instead of "%C3%A9" that should be "é" we get "e%CC%81".

(例如,应该用“é”代替“%C3%A9”,我们得到“ e%CC%81”。)

So what i do is a replace on the file name to the right UTF-8 code and then a decodeURI.

(因此,我要做的是将文件名替换为正确的UTF-8代码,然后替换为encodeURI。)

Exemple :

(范例:)

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

Then you can successfully get special chars and in my case accent from filename in your text layer.

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

You can do a replace for each characters you need for me it was :

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

'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' //ê

I took UTF-8 code from this HTML URL Encoding reference : https://www.w3schools.com/tags/ref_urlencode.asp

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

Hope it will help somebody one day because nothing existed online on this bug.

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...