Skip to main content
Participant
February 9, 2023
Answered

Changer le style de police d'un texte avec javascript

  • February 9, 2023
  • 1 reply
  • 678 views

Bonjour à tous,

J'ai un script qui ouvre une boite de dialogue pour que je puiise entrer le texte que je souhaite afficher à un endroit spécifique de mon plan de travail. Tout fonctionne sauf que je n'arive pas à changer le style de police de mon texte.. J'aimerais que le texte soit en police "Myriad pro" et en style de police "Bold" et que la taille de la police soit de 14 pt.

Voici le script que j'utilise

var text = prompt("Entrez le texte à écrire:", "");

if (text) {
  var doc = app.activeDocument;
 
  var layer = doc.layers.getByName("FOND DE PLAN");
  layer.locked = false;
 
  var textFrame = layer.textFrames.add();
  textFrame.contents = text;
  textFrame.textRange.characterAttributes.font = "Myriad Pro Bold";
  textFrame.textRange.characterAttributes.fontWeight = "Bold";
 
  var nonPrintableLayer = doc.layers.getByName("NON IMPRIMABLE");
  var pointOfGathering = nonPrintableLayer.pathItems.getByName("POINT DE RASSEMBLEMENT");
 
  var textBounds = textFrame.geometricBounds;
  var pointBounds = pointOfGathering.geometricBounds;
 
  var x = pointBounds[0] + (pointBounds[2] - pointBounds[0] - textBounds[2] + textBounds[0]) / 2;
  var y = pointBounds[1] + (pointBounds[3] - pointBounds[1] - textBounds[3] + textBounds[1]) / 2;
 
  textFrame.left = x;
  textFrame.top = y;
 
  layer.locked = true;
}



This topic has been closed for replies.
Correct answer CarlosCanto

characterAttributes.font is not part of the API, you should use

characterAttributes.textFont

 

characterAttributes.fontWeight is not part of the API

 

= "Myriad Pro Bold"; you can't assign the font by providing the String name, you need to get the Font Object and use the internal name illustrator understands

textFonts.getByName(Internal_fontname);

 

 

var text = prompt("Entrez le texte à écrire:", "");

if (text) {
    var fontname = "MyriadPro-Bold";
  var doc = app.activeDocument;
 
  var layer = doc.layers.getByName("FOND DE PLAN");
  layer.locked = false;
 
  var textFrame = layer.textFrames.add();
  textFrame.contents = text;
  textFrame.textRange.characterAttributes.textFont = textFonts.getByName(fontname); 
 
  var nonPrintableLayer = doc.layers.getByName("NON IMPRIMABLE");
  var pointOfGathering = nonPrintableLayer.pathItems.getByName("POINT DE RASSEMBLEMENT");
 
  var textBounds = textFrame.geometricBounds;
  var pointBounds = pointOfGathering.geometricBounds;
 
  var x = pointBounds[0] + (pointBounds[2] - pointBounds[0] - textBounds[2] + textBounds[0]) / 2;
  var y = pointBounds[1] + (pointBounds[3] - pointBounds[1] - textBounds[3] + textBounds[1]) / 2;
 
  textFrame.left = x;
  textFrame.top = y;
 
  layer.locked = true;
}

 

 

1 reply

CarlosCanto
Community Expert
CarlosCantoCommunity ExpertCorrect answer
Community Expert
February 9, 2023

characterAttributes.font is not part of the API, you should use

characterAttributes.textFont

 

characterAttributes.fontWeight is not part of the API

 

= "Myriad Pro Bold"; you can't assign the font by providing the String name, you need to get the Font Object and use the internal name illustrator understands

textFonts.getByName(Internal_fontname);

 

 

var text = prompt("Entrez le texte à écrire:", "");

if (text) {
    var fontname = "MyriadPro-Bold";
  var doc = app.activeDocument;
 
  var layer = doc.layers.getByName("FOND DE PLAN");
  layer.locked = false;
 
  var textFrame = layer.textFrames.add();
  textFrame.contents = text;
  textFrame.textRange.characterAttributes.textFont = textFonts.getByName(fontname); 
 
  var nonPrintableLayer = doc.layers.getByName("NON IMPRIMABLE");
  var pointOfGathering = nonPrintableLayer.pathItems.getByName("POINT DE RASSEMBLEMENT");
 
  var textBounds = textFrame.geometricBounds;
  var pointBounds = pointOfGathering.geometricBounds;
 
  var x = pointBounds[0] + (pointBounds[2] - pointBounds[0] - textBounds[2] + textBounds[0]) / 2;
  var y = pointBounds[1] + (pointBounds[3] - pointBounds[1] - textBounds[3] + textBounds[1]) / 2;
 
  textFrame.left = x;
  textFrame.top = y;
 
  layer.locked = true;
}

 

 

Participant
February 9, 2023

Thank you so much @CarlosCanto !!!