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

get characterStyles

Participant ,
Dec 17, 2018 Dec 17, 2018

  I'm looking for a way to record (add) a characterStyle from one textItem and apply it to another.  I haven't done much with character scripting so I'm really not sure where to start.

var idoc = app.activeDocument;

//loop through characterAttributes to get and add current selected style?

doc.characterStyles.add("MyStyle");

selected = idoc.selection;

//Select and Apply to selected text

idoc.characterStyles.getByName("MyStyle").applyTo( selected );

TOPICS
Scripting
1.5K
Translate
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

Advocate , Dec 21, 2018 Dec 21, 2018

Salut !

Il faut faire un choix parmi les propriétés de characterAttributes,

pour moi une boucle n'est pas possible...

// JavaScript Document for Illustrator

var docRef = activeDocument;

var text = selection[0]; alert(text.contents);

var charStyle = docRef.characterStyles.add("copy");

var charAttr = charStyle.characterAttributes;

    with (text.textRange) {

      charAttr.size = characterAttributes.size;

      charAttr.verticalScale = characterAttributes.verticalScale;

      charAttr.horizontalScale = chara

...
Translate
Adobe
Explorer ,
Dec 20, 2018 Dec 20, 2018

Hi,

I think that you should use:

selected = idoc.selection[0];

of course if you have selected over 1 item,  you should use loop for it.

Translate
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
Explorer ,
Dec 20, 2018 Dec 20, 2018

I use followed code for that:

var charStyle = doc.characterStyles.add("style");

var charAttr = charStyle.characterAttributes;

   

var paraAttr_0 = t.paragraphs[0].paragraphAttributes; 

      paraAttr_0.justification = Justification.CENTER;

var kolor2 = new CMYKColor();

     kolor2.cyan = 0;

     kolor2.magenta = 100;

     kolor2.yellow = 0;

     kolor2.black = 0;

charAttr.fillColor = kolor2;

charAttr.justification = Justification.CENTER;

charStyle.applyTo(t.textRange);

Translate
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
Advocate ,
Dec 21, 2018 Dec 21, 2018

Salut !

Il faut faire un choix parmi les propriétés de characterAttributes,

pour moi une boucle n'est pas possible...

// JavaScript Document for Illustrator

var docRef = activeDocument;

var text = selection[0]; alert(text.contents);

var charStyle = docRef.characterStyles.add("copy");

var charAttr = charStyle.characterAttributes;

    with (text.textRange) {

      charAttr.size = characterAttributes.size;

      charAttr.verticalScale = characterAttributes.verticalScale;

      charAttr.horizontalScale = characterAttributes.horizontalScale;

      charAttr.capitalization = characterAttributes.capitalization;

      charAttr.fillColor = characterAttributes.fillColor;

      charAttr.textFont = characterAttributes.textFont;

    }

var textRef1 = docRef.textFrames.add();

    textRef1.contents = "Scripting is fun!";

    textRef1.top = -700;

    textRef1.left = 50;

    charStyle.applyTo(textRef1.textRange);

de elleere

Translate
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
Participant ,
Dec 21, 2018 Dec 21, 2018

thank you renél! You are awesome.

Translate
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
Participant ,
Dec 22, 2018 Dec 22, 2018

One more question.  What am I doing wrong here??  Trying to apply style to selected text. For some reason this works sometimes but not every time.

var idoc = app.activeDocument;

var text = selection[0];

idoc.characterStyles.getByName( 'BNumber' ).applyTo( text.textRange );

Translate
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
Advocate ,
Dec 23, 2018 Dec 23, 2018

Bonjour Jeremy,

Quand tu appliques manuellement un style à un texte, tu as souvent un
petit signe + qui t'indique que le style n'est pas appliqué
complètement.

caractStyle.PNG

Il faut alors cliquer sur le +

avec le script c'est la même chose.

Il faut donc ajouter le Boolean [,clearingOverrides = true]

var idoc = app.activeDocument;

var text = selection[0];

idoc.characterStyles.getByName('copy').applyTo(text.textRange,true);

de elleere

Translate
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
Participant ,
Dec 23, 2018 Dec 23, 2018

I found this last night. Thanks for all your help Renel.

Translate
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
Participant ,
Dec 27, 2018 Dec 27, 2018

function findNote( note , toApply ){

    var idoc = app.activeDocument; 

    var text = selection[0];

   

    for (var i = 0; i < idoc.pageItems.length; i++) {

        pageItemNote = idoc.pageItems;

   

        if(pageItemNote.note == note){

            idoc.selection = pageItemNote;

           

            alert(note + " " + toApply);

           

            idoc.characterStyles.getByName(toApply).applyTo( text.textRange, true );

           

            activeDocument.selection = null;

           

        }

    }

};

var LnFront = 'Front Text';

var LnBack = 'Back Text';

var PnFront = 'FText';

var PnBack = 'BText';

findNote(LnFront , PnFront);

findNote(LnBack , PnBack);

  Can someone tell me what I am missing here.  The runs only the first function "findNote(LnFront, PnFront)", then when run again "findNote(LnBack, PnBack)" tells me the selected is not an object.  But is selected as well the correct characterStyle name ??

Translate
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
Advocate ,
Dec 28, 2018 Dec 28, 2018

Salut Jeremy,

What is it ???

if(pageItemNote.note == note){ 

idoc.selection = pageItemNote; 

Translate
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
Participant ,
Dec 28, 2018 Dec 28, 2018

This selects the text needed to apply the characterStyle.

  This is what I worked on today.  The selection of the text item happens but it looks like the characterStyles are being applied before the selection takes place.  Any help would be greatly appreciated.

function findAndApply( textName , toApply ){

var idoc = app.activeDocument;

var sel = selection[0];

for (var i = 0; i < idoc.pageItems.length; i++) {

       

    pageItemNote = idoc.pageItems.note;

        if (pageItemNote == textName) {

           

            idoc.pageItems[textName].selected = true;

            idoc.characterStyles.getByName(toApply).applyTo( sel.textRange, true );

           

        }

    }

};

findAndApply("Front Text" , "Ftext");

Translate
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
Advocate ,
Dec 28, 2018 Dec 28, 2018

Salut Jeremy,

Tu mélanges tout, il faut étudier la documentation fournie par Adobe, je te donne un lien pour télécharger le pdf.

Le Cloud d'Orange

de elleere

Translate
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
Participant ,
Jan 01, 2019 Jan 01, 2019
LATEST

I think I just needed a refresher. The selection needed to be made a second time before applying.  Thanks for your help.

Translate
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