Skip to main content
Mohamed Hameed21513110
Inspiring
June 6, 2022
Answered

Change Font and number

  • June 6, 2022
  • 1 reply
  • 989 views

Hello

- I have a text with letters and numbers, and I created a dialog with 2 dropdowns

- I want to make the first change of characters for a particular font

The second changes the numbers for another specified line

I attached an example showing what is required

https://drive.google.com/file/d/1GF63Hr7CKqTdkHm-i_E4RmGDQ3rTvH6i/view?usp=sharing 

 

This is the initial code I created and I want to complete the code

win = new Window('dialog',"قاعدة بيانات العملاء - ابن حميدو")
var tfontname = win.add ('statictext', undefined, "TextFontName");
tfontname.graphics.font = ScriptUI.newFont ("Arial", "Bold", 15);
var textfontname = win.add("dropdownlist", undefined, "");
textfontname.graphics.font = ScriptUI.newFont ("Arial", "Bold", 20);
textfontname.preferredSize  = [200, 30]; textfontname.helpTip = "textfontname";
var Nfontname = win.add ('statictext', undefined, "Numberfontname");
Nfontname.graphics.font = ScriptUI.newFont ("Arial", "Bold", 15);
var numberfontname = win.add("dropdownlist", undefined, "");
numberfontname.graphics.font = ScriptUI.newFont ("Arial", "Bold", 20);
numberfontname.preferredSize  = [200, 30];numberfontname.helpTip = "Numberfontname";
btn = win.add('button', undefined, 'Change')
btn.preferredSize  = [200, 35];

btn.onClick = function() {
        
    }
win.show()

 

This topic has been closed for replies.
Correct answer jazz-y

Forgive me for taking so long, and please don't get upset with me

 

For more clarification
The numbers do not have to be at the beginning of the sentence.. The numbers can be inside the sentence, in the middle of the sentence, or at the end of the sentence.

Also, the problem I personally encounter is that in recent versions of Photoshop when writing a paragraph with text and numbers and with a certain font. I find that the text takes one line and the numbers take another line.. so I want to assign a line to letters and a line to numbers

Also, I'm very simple at coding


You can't learn to write scripts by asking others to do all the hard work for you ¯\_(ツ)_/¯

 

This code has a couple of problems: firstly, I still don't understand what exactly you meant by "numbers" - your comments are at odds with your example in the file. As a result, the script replaces the font only for numbers. Secondly, since you didn't even try to write a font replacement function and didn't describe the dialog box logic, I was forced to follow your example and use fontName for replacement. This is wrong, since each fontName actually describes several font styles, and in case of replacing one font with another, it is important that they both support the same style (thin, bold, italic, etc.). If one of the fonts does not have the desired style, the replacement will not work. Perhaps this option will suit you, but it would be more correct to use fontPostScriptName (accordingly, you need to store two lists of fonts separately in your dialog box - a friendly name for the user and a more precise name for the script).

 

 

#target photoshop

changeFontForLettersAndNumbers('Times New Roman', 'Arial')

function changeFontForLettersAndNumbers(letters, numbers) {
    var s2t = stringIDToTypeID;
    function t2s(t) { if (!typeIDToStringID(t)) { return typeIDToCharID(t) } else { return typeIDToStringID(t) } }
    (r = new ActionReference()).putProperty(s2t('property'), p = s2t('textKey'));
    r.putEnumerated(s2t('layer'), s2t('ordinal'), s2t('targetEnum'));
    if (executeActionGet(r).hasKey(p)) {
        var textKey = executeActionGet(r).getObjectValue(p),
            sList = textKey.getList(s2t('textStyleRange')),
            l = new ActionList(),
            styleSheet = [],
            text = textKey.getString(p);
        for (var i = 0; i < sList.count; i++) {
            var cur = sList.getObjectValue(i);
            styleSheet.push({
                from: cur.getInteger(s2t('from')),
                to: cur.getInteger(s2t('to')),
                style: function (d) {
                    if (d.hasKey(p = s2t('styleSheetHasParent')) && d.getBoolean(p)) if (d.hasKey(p = s2t('baseParentStyle'))) extend_descriptor(d.getObjectValue(p), d)
                    return d;
                }(cur.getObjectValue(s2t('textStyle'))),
            })
            styleSheet[i].text = text.substring(styleSheet[i].from, styleSheet[i].to);
        };
        var len = styleSheet.length;
        for (var i = 0; i < len; i++) {
            var customStyle = setFontName(styleSheet[i], 0, styleSheet[i].text.length, letters)
            styleSheet.splice.apply(styleSheet, [i, 1].concat(customStyle));
        }
        do {
            var len = styleSheet.length,
                found = false;
            for (var i = 0; i < len; i++) {
                var offset = 0,
                    cur = styleSheet[i].text;
                if (cur) {
                    var digits = cur.match(/\d+/),
                        from, to;
                    if (digits) {
                        from = cur.indexOf(digits[0]) + offset
                        to = from + digits[0].length
                        offset += digits[0].length
                        cur = cur.substring(cur.indexOf(digits[0]) + digits[0].length)
                        customStyle = setFontName(styleSheet[i], from, to, numbers, true)
                        styleSheet.splice.apply(styleSheet, [i, 1].concat(customStyle));
                        len += customStyle.length - 1;
                        i += customStyle.length - 1;
                        found = true
                    }
                }
            }
        } while (found)
        var l = new ActionList();
        for (var i = 0; i < styleSheet.length; i++) {
            var d = new ActionDescriptor();
            d.putObject(s2t('textStyle'), s2t('textStyle'), styleSheet[i].style)
            d.putInteger(s2t('from'), styleSheet[i].from)
            d.putInteger(s2t('to'), styleSheet[i].to)
            l.putObject(s2t('textStyleRange'), d)
        }
        textKey.putList(s2t('textStyleRange'), l);
        (r = new ActionReference()).putEnumerated(s2t('layer'), s2t('ordinal'), s2t('targetEnum'));
        (d = new ActionDescriptor()).putReference(s2t('target'), r);
        d.putObject(s2t('to'), s2t('textLayer'), textKey);
        executeAction(s2t('set'), d, DialogModes.NO);
    }
    function setFontName(baseStyleSheet, from, to, fontName, erase) {
        var output = [],
            offset = baseStyleSheet.from;
        if (baseStyleSheet.from != from + offset) {
            output.push({
                from: baseStyleSheet.from,
                to: from + offset,
                style: baseStyleSheet.style,
                text: baseStyleSheet.text.substring(0, from)
            })
        }
        var customStyle = new ActionDescriptor();
        extend_descriptor(baseStyleSheet.style, customStyle);
        customStyle.putString(s2t('fontName'), fontName);
        customStyle.erase(s2t('fontPostScriptName'));

        /* If the original and new font do not have the same style (thin, bold, italic, etc.), 
        then you need to uncomment this line - this will reset the style settings.*/
        //  customStyle.erase(s2t('fontStyleName'));

        output.push({
            from: from + offset,
            to: to + offset,
            style: customStyle,
            text: erase ? null : baseStyleSheet.text.substring(from, to)
        })
        if (baseStyleSheet.to != to + offset) {
            output.push({
                from: to + offset,
                to: baseStyleSheet.to,
                style: baseStyleSheet.style,
                text: baseStyleSheet.text.substring(to)
            })
        }
        return output;
    }
    /** tnx to 
     * https://community.adobe.com/t5/photoshop-ecosystem-discussions/how-to-copy-save-styles-typeface-font-size-design-etc-of-arbitrary-text-fragments-and-apply-them-to/m-p/10522630
     */
    function extend_descriptor(src_desc, dst_desc) {
        try {
            for (var i = 0; i < src_desc.count; i++) {
                var key = src_desc.getKey(i);
                if (dst_desc.hasKey(key)) continue;
                var type = src_desc.getType(key);
                switch (type) {
                    case DescValueType.ALIASTYPE: dst_desc.putPath(key, src_desc.getPath(key)); break;
                    case DescValueType.BOOLEANTYPE: dst_desc.putBoolean(key, src_desc.getBoolean(key)); break;
                    case DescValueType.CLASSTYPE: dst_desc.putClass(key, src_desc.getClass(key)); break;
                    case DescValueType.DOUBLETYPE: dst_desc.putDouble(key, src_desc.getDouble(key)); break;
                    case DescValueType.INTEGERTYPE: dst_desc.putInteger(key, src_desc.getInteger(key)); break;
                    case DescValueType.LISTTYPE: dst_desc.putList(key, src_desc.getList(key)); break;
                    case DescValueType.RAWTYPE: dst_desc.putData(key, src_desc.getData(key)); break;
                    case DescValueType.STRINGTYPE: dst_desc.putString(key, src_desc.getString(key)); break;
                    case DescValueType.LARGEINTEGERTYPE: dst_desc.putLargeInteger(key, src_desc.getLargeInteger(key)); break;
                    case DescValueType.REFERENCETYPE: dst_desc.putReference(key, src_desc.getReference(key)); break;
                    case DescValueType.OBJECTTYPE: dst_desc.putObject(key, src_desc.getObjectType(key), src_desc.getObjectValue(key)); break;
                    case DescValueType.ENUMERATEDTYPE: dst_desc.putEnumerated(key, src_desc.getEnumerationType(key), src_desc.getEnumerationValue(key)); break;
                    case DescValueType.UNITDOUBLE: dst_desc.putUnitDouble(key, src_desc.getUnitDoubleType(key), src_desc.getUnitDoubleValue(key)); break;
                    default: alert("Unknown data type in descriptor"); return false;
                }
            }
            return true;
        }
        catch (e) { throw (e); }
    }
}

 

 

1 reply

Mohamed Hameed21513110
Inspiring
August 13, 2022

I made the model and placed the arrays.

Just need to misplace the event code to change letters, numbers and symbols

 

win = new Window('dialog',"قاعدة بيانات العملاء - ابن حميدو")
var tfontname = win.add ('statictext', undefined, "TextFontName");
tfontname.graphics.font = ScriptUI.newFont ("Arial", "Bold", 15);

//-- For letters and texts
var a = ["ae_AlYermook", "ChunkFive", "Freestyle Script","ABUHMEDA  FREE"];
var textfontname = win.add("dropdownlist", undefined, a);
textfontname.graphics.font = ScriptUI.newFont ("Arial", "Bold", 20);
textfontname.preferredSize  = [200, 30]; textfontname.helpTip = "textfontname";
var Nfontname = win.add ('statictext', undefined, "Numberfontname");
Nfontname.graphics.font = ScriptUI.newFont ("Arial", "Bold", 15);

//--- For numbers and symbols
var B = ["ae_AlYermook", "ChunkFive", "Freestyle Script","ABUHMEDA  FREE"];
var numberfontname = win.add("dropdownlist", undefined, B);
numberfontname.graphics.font = ScriptUI.newFont ("Arial", "Bold", 20);
numberfontname.preferredSize  = [200, 30];numberfontname.helpTip = "Numberfontname";
btn = win.add('button', undefined, 'Change')
btn.preferredSize  = [200, 35];

btn.onClick = function() {
 //-- I want the event of changing letters and texts by array A and changing numbers and symbols by array B
 
    }
win.show()
Legend
August 13, 2022

Layer text styles are stored like this:

 

{
    "_obj": "textStyleRange",
    "from": 0,
    "textStyle": {
        "_obj": "textStyle"
        ...many parameters here...
    },
    "to": 2
}

 

 Сan you write a script that get text from a layer to the textKey String like this:

 

var textKey = activeDocument.activeLayer.textItem.contents;

 

and then creates an array of objects describing the type and position of the characters you want, as follows:

 

[{
    text: '1/ ',
    type: 'numbers',
    from: 0,
    to: 3
},
{
    text: 'a separate article or particular \r',
    type: 'letters',
    from: 3,
    to: 37
}],
and etc...

 

(note the non-printing carriage return character \r at the end of the line and that the code should work for any of your texts)

 

???

 

This task is to work with strings, it does not require special skills.

 

This will make it much easier for us to help you 😉

Mohamed Hameed21513110
Inspiring
August 13, 2022

Sorry, but I can't understand the code
Or in other words, how does the code work?
I made the shape and the elements
I just want the event where I can use the elements to change the text of letters and numbers