Illustratorで複数選択したオブジェクトの色をランダムカラーに設定します。線は破線にも対応しています。
if (app.documents.length > 0) {
var doc = app.activeDocument;
if (doc.selection.length > 0) {
for (var i = 0; i < doc.selection.length; i++) {
applyRandomColor(doc.selection[i]);
}
alert("ランダムカラーを適用しました。");
} else {
alert("オブジェクトを選択してください。");
}
}
function applyRandomColor(item) {
// グループ内も処理
if (item.typename == "GroupItem") {
for (var i = 0; i < item.pageItems.length; i++) {
applyRandomColor(item.pageItems[i]);
}
return;
}
// パスや複合パス
if (item.typename == "PathItem") {
// 塗り
if (item.filled) {
item.fillColor = randomRGBColor();
}
// 線
if (item.stroked) {
item.strokeColor = randomRGBColor();
}
} else if (item.typename == "CompoundPathItem") {
for (var j = 0; j < item.pathItems.length; j++) {
var path = item.pathItems[j];
if (path.filled) {
path.fillColor = randomRGBColor();
}
if (path.stroked) {
path.strokeColor = randomRGBColor();
}
}
}
}
function randomRGBColor() {
var color = new RGBColor();
color.red = Math.floor(Math.random() * 256);
color.green = Math.floor(Math.random() * 256);
color.blue = Math.floor(Math.random() * 256);
return color;
}
任意の複数オブジェクトを選択して実行すれば・・・
一気に変更されます。そのまま再実行すれば・・・
新たなランダムカラーになります。




