Skip to main content
Micke Alm
Participant
January 10, 2019
Question

Photoshop strips special characters

  • January 10, 2019
  • 1 reply
  • 1109 views

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);

  }

  }

}

This topic has been closed for replies.

1 reply

Legend
January 10, 2019

What does the alert show if you insert it after line 71?

alert(newTextString +"\n\n"+ decodeURI(newTextString) +"\n\n"+ layerRef.textItem.contents);

Micke Alm
Micke AlmAuthor
Participant
January 10, 2019

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?

Legend
January 10, 2019

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?