Skip to main content
Balaji Murugesan
Known Participant
July 17, 2021
Answered

Find postscript font upto textstylerange and replace a new postscript name based on Input data.

  • July 17, 2021
  • 2 replies
  • 1067 views

Hi Team,

Select the text by textstylerange, find the PostScript font name and change the font to the corresponding one.

 

Sample Input data shared below:

 

1. need to select text by textstylerange.

2. get postscript font name.

3. match with given input data (like as if gotham font found).

4. replace font with new one(matching respective column with next column, like as if found gotham font and need to replace kefa).

 

Thanks

 

This topic has been closed for replies.
Correct answer r-bin

Here's the code for the active text layer

 

//////////////////////////////////////////////////////
// define your csv-file 
// headers must be: "Findfont" and "Replacefont", case sensitive and without spaces 

var file = new File("C:\\1\\aaa.csv"); 

//////////////////////////////////////////////////////
// depending on the data in the csv-file, use the posrscipt name or just the name of the font. 
// ex. "Arial-BoldMT" => fontPostScriptName, "Arial" => fontName                               

var use_postscript_name = false;

//////////////////////////////////////////////////////

file.open("r");
var s = file.read();
file.close();

var data = csv_to_array(s);

replace_styles(activeDocument.activeLayer.id); 

alert("done");
 
//////////////////////////////////////////////////////
function replace_styles(layer_id) 
    {
    try {
        var r = new ActionReference(); 
        r.putEnumerated(stringIDToTypeID("textLayer"), stringIDToTypeID("ordinal"), stringIDToTypeID("targetEnum")); 
        var textKey = executeActionGet(r).getObjectValue(stringIDToTypeID("textKey"));
    
        var list = textKey.getList(stringIDToTypeID("textStyleRange"));
    
        var new_list = new ActionList(); 
    
        for (var i = 0; i < list.count; i++)
            {
            var d = list.getObjectValue(i);
        
            var fr = d.getInteger(stringIDToTypeID("from"));
            var to = d.getInteger(stringIDToTypeID("to"));
        
            var st = d.getObjectValue(stringIDToTypeID("textStyle"));
        
            var postscript_name = st.getString(stringIDToTypeID("fontPostScriptName")); 
            
            var font_name = st.getString(stringIDToTypeID("fontName"));  
        
            for (var x = 0; x < data.length; x++)
                {
                if (use_postscript_name)
                    {
                    if (postscript_name == data[x].Findfont)
                        {
                        st.putString(stringIDToTypeID("fontPostScriptName"), data[x].Replacefont);
                        st.erase(stringIDToTypeID("fontName"));
                        break;
                        }
                    }
                else
                    {
                    if (font_name == data[x].Findfont)
                        {
                        st.putString(stringIDToTypeID("fontName"), data[x].Replacefont);
                        st.erase(stringIDToTypeID("fontPostScriptName"));
                        break;
                        }
                    }
                }
        
            var d = new ActionDescriptor(); 
            d.putInteger(stringIDToTypeID("from"), fr); 
            d.putInteger(stringIDToTypeID("to"), to); 
            d.putObject(stringIDToTypeID("textStyle"), stringIDToTypeID("textStyle"), st); 
        
            new_list.putObject(stringIDToTypeID("textStyleRange"), d);
            }
    
        textKey.putList(stringIDToTypeID("textStyleRange"), new_list); 
    
        var d = new ActionDescriptor(); 
        var r = new ActionReference(); 
        r.putIdentifier(stringIDToTypeID("textLayer"), layer_id); 
        d.putReference(stringIDToTypeID("null"), r); 
        d.putObject(stringIDToTypeID("to"), stringIDToTypeID("textLayer"), textKey); 
    
        executeAction(stringIDToTypeID("set"), d, DialogModes.NO); 
        }
    catch(e) { alert(e); }
    }
    
//////////////////////////////////////////////////////
function csv_to_array(data, sep) 
    {
    try {
        if (sep == undefined) sep = /[;,]/;
    
        var lines = data.split(/\r\n|\n/);
        var headers = lines[0].split(sep);
        var ret = new Array();
    
        for (var i = 1; i < lines.length; i++) 
            {
            var data = lines[i].split(sep);
    
            if (data.length == headers.length) 
                {
                var o = new Object();
                for (var j = 0; j < headers.length; j++) 
                    {
                    o[headers[j]] = data[j];
                    }
    
                ret.push(o);
                }
            }
    
        return ret;
        }
    catch(e) { alert(e); }
    }

2 replies

r-binCorrect answer
Braniac
July 19, 2021

Here's the code for the active text layer

 

//////////////////////////////////////////////////////
// define your csv-file 
// headers must be: "Findfont" and "Replacefont", case sensitive and without spaces 

var file = new File("C:\\1\\aaa.csv"); 

//////////////////////////////////////////////////////
// depending on the data in the csv-file, use the posrscipt name or just the name of the font. 
// ex. "Arial-BoldMT" => fontPostScriptName, "Arial" => fontName                               

var use_postscript_name = false;

//////////////////////////////////////////////////////

file.open("r");
var s = file.read();
file.close();

var data = csv_to_array(s);

replace_styles(activeDocument.activeLayer.id); 

alert("done");
 
//////////////////////////////////////////////////////
function replace_styles(layer_id) 
    {
    try {
        var r = new ActionReference(); 
        r.putEnumerated(stringIDToTypeID("textLayer"), stringIDToTypeID("ordinal"), stringIDToTypeID("targetEnum")); 
        var textKey = executeActionGet(r).getObjectValue(stringIDToTypeID("textKey"));
    
        var list = textKey.getList(stringIDToTypeID("textStyleRange"));
    
        var new_list = new ActionList(); 
    
        for (var i = 0; i < list.count; i++)
            {
            var d = list.getObjectValue(i);
        
            var fr = d.getInteger(stringIDToTypeID("from"));
            var to = d.getInteger(stringIDToTypeID("to"));
        
            var st = d.getObjectValue(stringIDToTypeID("textStyle"));
        
            var postscript_name = st.getString(stringIDToTypeID("fontPostScriptName")); 
            
            var font_name = st.getString(stringIDToTypeID("fontName"));  
        
            for (var x = 0; x < data.length; x++)
                {
                if (use_postscript_name)
                    {
                    if (postscript_name == data[x].Findfont)
                        {
                        st.putString(stringIDToTypeID("fontPostScriptName"), data[x].Replacefont);
                        st.erase(stringIDToTypeID("fontName"));
                        break;
                        }
                    }
                else
                    {
                    if (font_name == data[x].Findfont)
                        {
                        st.putString(stringIDToTypeID("fontName"), data[x].Replacefont);
                        st.erase(stringIDToTypeID("fontPostScriptName"));
                        break;
                        }
                    }
                }
        
            var d = new ActionDescriptor(); 
            d.putInteger(stringIDToTypeID("from"), fr); 
            d.putInteger(stringIDToTypeID("to"), to); 
            d.putObject(stringIDToTypeID("textStyle"), stringIDToTypeID("textStyle"), st); 
        
            new_list.putObject(stringIDToTypeID("textStyleRange"), d);
            }
    
        textKey.putList(stringIDToTypeID("textStyleRange"), new_list); 
    
        var d = new ActionDescriptor(); 
        var r = new ActionReference(); 
        r.putIdentifier(stringIDToTypeID("textLayer"), layer_id); 
        d.putReference(stringIDToTypeID("null"), r); 
        d.putObject(stringIDToTypeID("to"), stringIDToTypeID("textLayer"), textKey); 
    
        executeAction(stringIDToTypeID("set"), d, DialogModes.NO); 
        }
    catch(e) { alert(e); }
    }
    
//////////////////////////////////////////////////////
function csv_to_array(data, sep) 
    {
    try {
        if (sep == undefined) sep = /[;,]/;
    
        var lines = data.split(/\r\n|\n/);
        var headers = lines[0].split(sep);
        var ret = new Array();
    
        for (var i = 1; i < lines.length; i++) 
            {
            var data = lines[i].split(sep);
    
            if (data.length == headers.length) 
                {
                var o = new Object();
                for (var j = 0; j < headers.length; j++) 
                    {
                    o[headers[j]] = data[j];
                    }
    
                ret.push(o);
                }
            }
    
        return ret;
        }
    catch(e) { alert(e); }
    }
Balaji Murugesan
Known Participant
July 19, 2021

Hi @r-bin, Amazing lines, its work perfectly and the addtional features to use font postscript is very great one.

If possible, please explain the how to find action script object model.

I tried to study action script but i am facing chanllenges to find object models.

A heartful thanks @r-bin 

Thanks

 

Braniac
July 19, 2021
Most of the code is taken from the output of the ScriptListener plugin.
You can also use this script Script Events Listener
 
P.S. "action script object model" - I did not come across any documentation on this.
Kukurykus
Braniac
July 17, 2021

First point is not clear. I don't understand how and where you want to do that.

 

btw did you find your answer in your previous thread:

Find text and redefine font family name, style, size and leading.

(I'm asking as you did not respond for given answer of the other user)

Balaji Murugesan
Known Participant
July 17, 2021

Hi @Kukurykus ,

For point 1, select the text by text-style-range (A continuous range of identical text formatting attributes).

 

For above snap, style ranges are described. They have similar properties, but different in color.

Note: I found the result in the previous questions, which was very helpful to my study. But the given result did not exactly match my questions.

Please allow some time to respond, I have already mentioned this as my schedule. I will do it when everything is done.

Thanks for understanding.

Thanks