Question

I'm trying to extract text from a LayerKind.TEXT art layer in Photoshop using JS - textItem.contents. It works very well up until I have an apostrophe in the string I am trying to extract. It returns this special character, � , every time an apostrophe is found in the string.

Is there a way to stop this from happening or a way to find that special character and replace with an apostrophe (single quote)? From one post I read, the poster used .replace("EM", "'"), but that doesn't seem to work for me.

Thanks!

Était-ce utile?

La solution

Just a quick check: Have you used an escape slash for the apostrophe? Are you sure it's an apostrophe (U+0027) and not a right single quotation mark (U+2019); I get them mixed up all the time

Anyhoo, the script here will go over all the layers and replace an apostrophe with an empty string - change the replace string to whatever you want. That should help you along.

// call the source document
var srcDoc = app.activeDocument;
var numOfLayers = srcDoc.layers.length;

// main loop starts here
for (var i = numOfLayers -1; i >= 0; i--)
{

    var tempLayer = srcDoc.layers[i]
    reName = justGetFontContents(tempLayer);

}//end loop


// function JUST GET FONT CONTENTS (layer)
// --------------------------------------------------------
function justGetFontContents(alayer)
{
    if (alayer.kind == 'LayerKind.TEXT')
    {
        //alert(alayer.name + " " + alayer.textItem.contents)
        var c = replaceApostrophe(alayer.textItem.contents)
        alert("reName = " + c)
        alayer.textItem.contents = c
    }
    return c
}


// function REPLACE APOSTROPHE WITH NOTHING (text string)
// --------------------------------------------------------
function replaceApostrophe(string)
{
    var niceString = string.replace(/[\']/g, "");
    return niceString;
}
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top