Skip to content

Instantly share code, notes, and snippets.

@UskeS
Created July 22, 2024 07:01
Show Gist options
  • Save UskeS/c9761d15a11cec2d2ae13767c6d662cf to your computer and use it in GitHub Desktop.
Save UskeS/c9761d15a11cec2d2ae13767c6d662cf to your computer and use it in GitHub Desktop.
[Illustrator] 選択したオブジェクトのサイズをテキストとして挿入する
/**
* @fileoverview Insert the size of the selected object as text.
* @version v1.0.0
* @author Yusuke SAEGUSA
* @description
*/
var doc = app.activeDocument;
var sel = doc.selection[0];
var w = new UnitValue(sel.width, "pt"); //選択範囲の幅
var h = new UnitValue(sel.height, "pt"); //選択範囲の高さ
var d = new UnitValue(20, "mm"); // 凸版データとテキストの距離(任意)、デフォは20mm
var t = new UnitValue(sel.top, "pt"); //選択範囲の天座標
var l = new UnitValue(sel.left, "pt"); //選択範囲の左座標
makeText(
"H" + roundup(h.as("mm"), 1) + "×W" + roundup(w.as("mm"), 1) + "mm",
[ l.as("pt") + w.as("pt") / 2 , t.as("pt") - h.as("pt") - d.as("pt")]
);
/**
* 小数点以下を指定桁数で切り上げる関数
*
* @param {Number} num 数値
* @param {Number} digits 切り上げて有効にする小数点以下桁数(整数)
* @return {Number} Number
*/
function roundup(num, digits) {
return Math.ceil(num * Math.pow(10, digits)) / Math.pow(10, digits);
}
/**
* テキストを生成する関数
*
* @param {String} cont 生成するテキスト
* @param {Number[]} pos テキストオブジェクトの座標
*/
function makeText(cont, pos) {
var txt = doc.textFrames.add();
txt.contents = cont;
txt.position = pos;
txt.textRange.characterAttributes.textFont = app.textFonts.getByName("KozGoPro-Regular"); // フォントを一旦 小塚ゴシックPro R に設定(バグ回避)
// バグについてはここを参照 → https://sppy.stars.ne.jp/blog/102
txt.textRange.characterAttributes.size = new UnitValue(14 / 4, "mm").as("pt"); // Qで指定してもうまくいかないのでmm表記でQを指定
txt.paragraphs[0].justification = Justification.CENTER;
txt.textRange.characterAttributes.textFont = app.textFonts.getByName("KozGoPr6N-Regular"); // フォントを改めて 小塚ゴシックPro R に設定
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment