Copy link to clipboard
Copied
Hi guys,
I had to register to ask a question that's been bothering me for a few days.
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. Everything works just fine, except that Photoshop somehow strips umlauts from my strings, ie, Björn becomes 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.
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);
}
}
}
Copy link to clipboard
Copied
What does the alert show if you insert it after line 71?
alert(newTextString +"\n\n"+ decodeURI(newTextString) +"\n\n"+ layerRef.textItem.contents);
Copy link to clipboard
Copied
So (to me), it again proves the string needs to be decoded, and works all the way up until the string is actually placed in Photoshop?
Copy link to clipboard
Copied
I did not understand. Explain if not difficult.
It seems that everything works for you.
And it looks like your text font is simply incorrectly displaying "non-standard" characters.
Can you manually type this text in a text layer?
Copy link to clipboard
Copied
Exactly, that's what's driving my confusion - everything SEEMS to work! Script runs fine, I can manually type the text in the layer, I've checked the character encoding of everything I can think of...
So in the screenshot above, it will get the string as Ulrika Hedba%CC%88ck, decode it to Ulrika Hedbäck, show it as Hedbäck in the alert, then paste it as Hedback without the umlauts.
Copy link to clipboard
Copied
Show a screenshot where you have a problem. I do not understand.
Copy link to clipboard
Copied
I did encodeURI and got this: Result: Hedb%C3%A4ck using that I get the text layer to show correctly with Myriad Pro. I love unicode and escaping and all that fun! Good luck to you.
Copy link to clipboard
Copied
Explain please for details. I had no problems with the text layer. Have you had?