• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

How to copy (save) styles (typeface, font size, design, etc.) of arbitrary text fragments and apply them to new text layer fragments?

Guide ,
Jun 29, 2019 Jun 29, 2019

Copy link to clipboard

Copied

Hello!
How to copy (save) styles (typeface, font size, design, etc.) of arbitrary text fragments and apply them to new text layer fragments?

I am writing a script that replaces arbitrary (user-defined) parts of the text within a single text layer. I currently use the function stringIDToTypeID ("findReplace"), but it often does not work correctly (for example, when the text contains repeated fragments, but with different styles).

I'm trying to figure out how to do it through AAM, but so far unsuccessfully ...

TOPICS
Actions and scripting

Views

1.6K

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

People's Champ , Jul 01, 2019 Jul 01, 2019

If you change some words in the text, this script will preserve the style of each word (something separated by a dot, comma, space, tab, or newline).

Suppose you have text "Text Text Text"

set_text_contents("Text Nothing\rText")
function set_text_contents(text)
    {      
    try {      
        var sep = /(,|\.|\s)/;
        var r = new ActionReference();      
        r.putEnumerated(stringIDToTypeID("layer"), stringIDToTypeID("ordinal"), stringIDToTypeID("targetEnum"));
        var textKey = 
...

Votes

Translate

Translate
Adobe
People's Champ ,
Jun 29, 2019 Jun 29, 2019

Copy link to clipboard

Copied

I do not understand what you want. Try to show an example, perhaps with screenshots.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guide ,
Jun 30, 2019 Jun 30, 2019

Copy link to clipboard

Copied

I'm looking for a way to replace the green "Text" with an arbitrary set of characters in the text layer, keeping the style of this all the other letters.

2019-06-30_19-12-32.png

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
People's Champ ,
Jul 01, 2019 Jul 01, 2019

Copy link to clipboard

Copied

What you can not do when using "find Replace"?

var d = new ActionDescriptor();

var r = new ActionReference();

r.putProperty(stringIDToTypeID("property"), stringIDToTypeID("replace"));

r.putEnumerated(stringIDToTypeID("textLayer"), stringIDToTypeID("ordinal"), stringIDToTypeID("allEnum"));

d.putReference(stringIDToTypeID("null"), r);

var d1 = new ActionDescriptor();

d1.putString(stringIDToTypeID("find"), "Text");

d1.putString(stringIDToTypeID("replace"), "New text");

d1.putBoolean(stringIDToTypeID("checkAll"), true);

d1.putBoolean(stringIDToTypeID("forward"), true);

d1.putBoolean(stringIDToTypeID("caseSensitive"), false);

d1.putBoolean(stringIDToTypeID("wholeWord"), false);

d1.putBoolean(stringIDToTypeID("ignoreAccents"), true);

d.putObject(stringIDToTypeID("using"), stringIDToTypeID("findReplace"), d1);

executeAction(stringIDToTypeID("replace"), d, DialogModes.NO);

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guide ,
Jul 01, 2019 Jul 01, 2019

Copy link to clipboard

Copied

I use "find Replace" now, but it replaces all "Text" (or first "Text") in layer. I need to replace only middle (for example) green "Text" (and save style of "New Text" green too)

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
People's Champ ,
Jul 01, 2019 Jul 01, 2019

Copy link to clipboard

Copied

You can use DialogModes.ALL to replace only the desired suitable fragment.


It is possible to write a script to replace only the middle (or other) text with preserving the style. But it will be quite a long and complex script. I'm too lazy to write it.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guide ,
Jul 01, 2019 Jul 01, 2019

Copy link to clipboard

Copied

The script should work silently - DialogModes.ALL does not fit.

I roughly imagine how the styles are described and assigned to fragments of the text, just hoped that someone had already dealt with such a question and had a working code.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
People's Champ ,
Jul 01, 2019 Jul 01, 2019

Copy link to clipboard

Copied

I think that the main problem is how you are going to tell the script that you only need to replace, for example, the "green Text".

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guide ,
Jul 01, 2019 Jul 01, 2019

Copy link to clipboard

Copied

No, it's not a problem

Script has a user interface in which the user with the help of various filters can break the text of the layer into separate words, change their position in the text, replace with other values, break into substrings, etc. (that is, it allows you to easily select any word of the line "Text Text Text"). The script can be written into an action and the user can apply it to text layers with the same type of structure. The problem is that I cannot control the source text and sometimes it contains repeated patterns of characters that cannot be properly processed using the “find and replace” function.

(I apologize, I haven’t translated interface it into English yet, because haven’t finished the work)

2019-07-01_23-16-25.png

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Jul 01, 2019 Jul 01, 2019

Copy link to clipboard

Copied

If that is russian you don't have to apologize r-bin for 😄

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guide ,
Jul 01, 2019 Jul 01, 2019

Copy link to clipboard

Copied

unfortunately (or fortunately) I don’t often visit this forum to know the native language of individual users 🙂

 

for example, the script cannot process this sample text normally, and instead of the output that gives in the preview window, the first (not second) word will be replaced. This is quite a rare case, but I'm looking for ways to get around it.

2019-07-01_23-39-25.png

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
People's Champ ,
Jul 01, 2019 Jul 01, 2019

Copy link to clipboard

Copied

If you change some words in the text, this script will preserve the style of each word (something separated by a dot, comma, space, tab, or newline).

Suppose you have text "Text Text Text"

set_text_contents("Text Nothing\rText")
function set_text_contents(text)
    {      
    try {      
        var sep = /(,|\.|\s)/;
        var r = new ActionReference();      
        r.putEnumerated(stringIDToTypeID("layer"), stringIDToTypeID("ordinal"), stringIDToTypeID("targetEnum"));
        var textKey = executeActionGet(r).getObjectValue(stringIDToTypeID("textKey"));    
        var style_list = textKey.getList(stringIDToTypeID("textStyleRange"));  
        var parag_list = textKey.getList(stringIDToTypeID("paragraphStyleRange"));              
        var style0 = style_list.getObjectValue(0).getObjectValue(stringIDToTypeID("textStyle"));
        var parag0 = parag_list.getObjectValue(0).getObjectValue(stringIDToTypeID("paragraphStyle"));
        var old_text = textKey.getString(stringIDToTypeID("textKey"));  
        var styles = new Array();
        var from = 0;
        var to = old_text.length+1;
        var def_style;
        for (var i = 0; i < old_text.length; i++)  
            {
            if (old_text.charAt(i).match(sep))
                {
                to = i+1;
                styles.push([from,to, style0, parag0]);
                from = to;
                }    
            }
        styles.push([from,to, style0, parag0]);
        for (var i = 0; i < style_list.count; i++)  
            {  
            var d = style_list.getObjectValue(i);  
            var x0 = d.getInteger(stringIDToTypeID("from"));  
            var x1 = d.getInteger(stringIDToTypeID("to"));  
            var st = d.getObjectValue(stringIDToTypeID("textStyle"));  
            for (var n = 0; n < styles.length; n++)
                {
                if (styles[n][0] >= x0)
                    {
                    styles[n][2] = st;
                    }
                }
            }
        for (var i = 0; i < parag_list.count; i++)  
            {  
            var d = parag_list.getObjectValue(i);  
            var x0 = d.getInteger(stringIDToTypeID("from"));  
            var x1 = d.getInteger(stringIDToTypeID("to"));  
            var st = d.getObjectValue(stringIDToTypeID("paragraphStyle"));  
            if (!i && st.hasKey(stringIDToTypeID("defaultStyle"))) def_style = st.getObjectValue(stringIDToTypeID("defaultStyle"));  
            for (var n = 0; n < styles.length; n++)
                {
                if (styles[n][0] >= x0)
                    {
                    styles[n][3] = st;
                    }
                }
            }
        var from = 0;
        var to = text.length+1;
        var idx = 0;
        for (var i = 0; i < text.length; i++)  
            {
            if (text.charAt(i).match(sep))
                {
                to = i+1;
                styles[idx][0] = from;
                styles[idx][1] = to;
                from = to;
                if (idx >= styles.length-1) break;
                ++idx;
                }    
            }
        if (idx > 0) styles[idx][0] = styles[idx-1][1];
        styles[idx][1] = text.length+1;
        var new_style = new ActionList();    
        var new_parag = new ActionList();    
        for (var i = 0; i < styles.length; i++)  
            {  
            var d = new ActionDescriptor();    
            d.putInteger(stringIDToTypeID("from"), styles[i][0]);    
            d.putInteger(stringIDToTypeID("to"),   styles[i][1]);    
            if (def_style) extend_descriptor(def_style, styles[i][2])                
            d.putObject(stringIDToTypeID("textStyle"), stringIDToTypeID("textStyle"), styles[i][2]);    
            new_style.putObject(stringIDToTypeID("textStyleRange"), d);  
            var d = new ActionDescriptor();    
            d.putInteger(stringIDToTypeID("from"), styles[i][0]);    
            d.putInteger(stringIDToTypeID("to"),   styles[i][1]);    
            d.putObject(stringIDToTypeID("paragraphStyle"), stringIDToTypeID("paragraphStyle"), styles[i][3]);    
            new_parag.putObject(stringIDToTypeID("paragraphStyleRange"), d);  
            }  
        textKey.putList(stringIDToTypeID("textStyleRange"), new_style);    
        textKey.putList(stringIDToTypeID("paragraphStyleRange"), new_parag);    
        textKey.putString(stringIDToTypeID("textKey"), text);  
    
        var d = new ActionDescriptor();      
        d.putReference(stringIDToTypeID("null"), r);      
        d.putObject(stringIDToTypeID("to"), stringIDToTypeID("textLayer"), textKey);      
        executeAction(stringIDToTypeID("set"), d, DialogModes.NO);      
        }      
    catch (e) { alert(e); }       
    }   
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); }
    }

ЗЫ. Как воткнуть эту функцию в ваш скрипт, решайте сами ).

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guide ,
Jul 01, 2019 Jul 01, 2019

Copy link to clipboard

Copied

Thank you very much!

Разберусь. Я начал писать в той логике, что нужно сначала определить все точки вхождения стилей, а затем уже разбираться на какое слово какой стиль попал. Ваш подход более логичный. У меня мало опыта работы с action manager, просто закопался в коде. Спасибо! В любом случае мне есть от чего оттолкнуться.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guide ,
Sep 18, 2019 Sep 18, 2019

Copy link to clipboard

Copied

LATEST

Hello! I modified your code, trying to preserve the styles of the text to the fullest extent (i.e., compare the size of the source text and the new one and either crop the style inside the line or supplement it with the last character of the line), but somewhere I made a mistake Photoshop did not accept the generated object. If not difficult, look at the problem.

 

UPD: problem solved. I use one name for different objects

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines