Skip to main content
Correct answer renél80416020

@renél80416020 The script is very good, but is there a way to center multiple texts and not just a single selected text?
Regarding the red line, there's no problem; it was simply Illustrator's spell checker.


Avec les modifications:

// JavaScript Document
// center_baselineShift.js
// author Landry René
// Sun, 21 December 2025 10:42:03 GMT
/* Select a text
   Center: Align with the point located halfway
   between the ascender and the descender of the font*/
function main() {
  if (selection.length) {
    var texts = [];
        extractTexts(selection,texts);
          if (texts.length) {
            var t, line0, dup, vect, bdDup, h;
            for (var i = 0; i < texts.length; i++) {
              t = texts[i];
              line0 = t.lines[0];
              dup = activeDocument.textFrames.add();
              copieObjtextRange (line0,dup);
              vect = dup.createOutline();
              bdDup = vect.geometricBounds; // visibleBounds
              h = bdDup[1]-bdDup[3];
              vect.remove();
              t.textRange.baselineShift = - h/2;
            }
          }
  }
}
if (app.documents.length) main();
// -------
function copieObjtextRange (obj1,obj2) {
  for (var j = 0; j < obj1.characters.length; j++) {
     if("pjgq,;".indexOf(obj1.characters[j].contents) == -1)
      obj1.characters[j].duplicate(obj2.insertionPoints[obj2.insertionPoints.length-1]);
  }
}
// -------
function extractTexts(s,tabs) {
  if(s.typename == "TextRange") return;
  for(var i = 0; i < s.length; i++){
    if(s[i].typename == "TextFrame" && s[i].locked == false){
      tabs.push(s[i]);
    } else if(s[i].typename == "GroupItem"){
      // Cherche les objets de types TextFrame dans ce groupe, recursivement
      extractTexts(s[i].pageItems, tabs);
    }
  }
}
// -------

1 reply

Glenn Wilton
December 17, 2025

You would need to convert to paths, as Illustrator only supports cented strokes on live text, regardless of scripting.

You could create a copy of the text, and drop it behind or in front of the selected text and assign a different stroke to simulate differetn stroke effects.

 

December 18, 2025

Thanks for replying, but the idea is to automate using a script because I receive several texts via a route and I need them to be centered and not on the baseline.

renél80416020
Inspiring
December 19, 2025

Bonjour @Sophia_Dawson0302 ,
Ce qui est déroutant dans votre exemple, ce sont les mots soulignés en rouge, qu'elle en est la signification?
Si ce que vous voulez est représenté sur l'image suivante, je posterai un script qui réalise cette tâche?
René

870763iB5C63A3430E64899.png