選択したテキストの文章が長い場合に省略表示する (Illustrator CS2/CS3/CS4編)

文章が長い場合に途中で省略し末尾を「...」にして表記する場合があります。今回のスクリプトは、その文章が長い場合に省略表記するように処理するものです。
以下のスクリプトは選択されたものがテキストフレームの場合のみ処理します。

textLen = 10;
postfix = "...";
selObj = activeDocument.selection;
for (i=0; i<selObj.length; i++)
{
if(selObj[i].typename == "TextFrame")
{
temp = selObj[i].contents;
txt = temp.substr(0, textLen);
if (temp.length > textLen) txt = txt + postfix;
selObj[i].contents = txt;
}
}

何文字以上なら省略表記するかは

textLen = 10;

の10の値を変更してください。これは10文字より文字数が多い場合に省略の対象になることを示しています。省略表記した際に文字の末尾に付加する文字は

postfix = "...";

で指定します。
省略表記するのは先頭の文字と末尾の文字を残して中間を省略する場合があります。この場合は以下のスクリプトを使ってください。

textLen = 12;
preLen = 5;
postLen = 4;
centerfix = "...";
selObj = activeDocument.selection;
for (i=0; i<selObj.length; i++)
{
if(selObj[i].typename == "TextFrame")
{
temp = selObj[i].contents;
txt = temp;
if (temp.length > textLen)
{
preText = temp.substr(0, preLen);
postText = temp.substring(temp.length - postLen, temp.length);
txt = preText + centerfix + postText;
}
selObj[i].contents = txt;
}
}

省略した場合に先頭の文字を何文字にするかは

preLen = 5;

の値を変更してください。同様に末尾を何文字表示するかは

postLen = 4;

で指定できます。

 

[サンプルをダウンロード]