
#Illustrator で選択しているオブジェクトに、[スウォッチ]パネルで選択しているカラーを適用するスクリプト
選択しているオブジェクトに、[スウォッチ]パネルで選択しているカラーを適用するスクリプトをChatGPTで作成しました。
結構、重宝しそうです。
次のような仕様です。
[スウォッチ]パネルで選択しているスウォッチを利用
カラーグループを選択していてもOK
文字数にスウォッチ数が足りない場合、最初のスウォッチに戻って繰り返し
重ね順でなく、アートボードの左側から適用
テキストオブジェクトも対象
選択しているのがテキストオブジェクトひとつだけの場合、1文字ずつ適用(別記事のスクリプトをマージ)
スクリプト
/*
作成日:2024年11月03日
更新日:
このスクリプトは、Adobe Illustratorで選択されたオブジェクトに対し、スウォッチパネルで選択されているスウォッチを適用します。
- 1つのテキストオブジェクトが選択されている場合は、文字ごとに順番にスウォッチを適用します。
- 複数のオブジェクトが選択されている場合は、位置によってソートした後にスウォッチを順に適用します(横長の場合は左から右、縦長の場合は上から下)。
適用完了後の通知は行いません。
*/
// 選択しているオブジェクトを取得
var selectedObjects = app.selection;
// オブジェクトが選択されているか確認
if (selectedObjects.length > 0) {
// 選択しているスウォッチを取得
var selectedSwatches = app.activeDocument.swatches.getSelected();
// スウォッチが選択されているか確認
if (selectedSwatches.length > 0) {
// 1つのテキストフレームが選択されている場合
if (selectedObjects.length === 1 && selectedObjects[0].typename === "TextFrame") {
var selectedTextFrame = selectedObjects[0];
var characterCount = selectedTextFrame.contents.length;
// 文字ごとにスウォッチを繰り返し適用
for (var i = 0; i < characterCount; i++) {
var swatchIndex = i % selectedSwatches.length;
selectedTextFrame.characters[i].fillColor = selectedSwatches[swatchIndex].color;
}
} else {
// 複数のオブジェクトが選択されている場合、位置でソート
sortByPosition(selectedObjects);
// 並べ替えた順にスウォッチを適用
for (var i = 0; i < selectedObjects.length; i++) {
var swatchIndex = i % selectedSwatches.length;
var swatchColor = selectedSwatches[swatchIndex].color;
// PathItem, CompoundPathItem, TextFrameのオブジェクトに色を適用
if (selectedObjects[i].typename === "PathItem") {
selectedObjects[i].fillColor = swatchColor;
} else if (selectedObjects[i].typename === "CompoundPathItem") {
// 複合パスの場合、すべてのパスアイテムに色を適用
var paths = selectedObjects[i].pathItems;
for (var j = 0; j < paths.length; j++) {
paths[j].fillColor = swatchColor;
}
} else if (selectedObjects[i].typename === "TextFrame") {
// テキストオブジェクトの場合、テキスト全体に色を適用
selectedObjects[i].textRange.fillColor = swatchColor;
}
}
}
} else {
// スウォッチが選択されていない場合のエラーメッセージ
alert("スウォッチパネルでスウォッチを選択してください。");
}
} else {
// オブジェクトが選択されていない場合のエラーメッセージ
alert("オブジェクトを選択してください。");
}
// 位置によってソートする関数
function sortByPosition(r){
var hs = [];
var vs = [];
for(var i = 0, iEnd = r.length; i < iEnd; i++){
hs.push(r[i].left);
vs.push(r[i].top);
}
if(rMax(hs) - rMin(hs) > rMax(vs) - rMin(vs)){
r.sort(function(a,b){ return compPosition(a.left, b.left, b.top, a.top) });
} else {
r.sort(function(a,b){ return compPosition(b.top, a.top, a.left, b.left) });
}
}
// 比較関数
function compPosition(a1, b1, a2, b2){
return a1 == b1 ? a2 - b2 : a1 - b1;
}
// 配列の最大値を取得する関数
function rMax(r){
return Math.max.apply(null, r);
}
// 配列の最小値を取得する関数
function rMin(r){
return Math.min.apply(null, r);
}
縦方向の調整には、shspageさんのsort_by_position.jsxを参照させていただきました。ありがとうございます!
さらによいもの
Sergey Osokinさんが完璧なスクリプトを公開されていました。
まったくの二番煎じでした…
The free MatchColors script provides several options for applying and repeating colors to paths or editable text https://t.co/7m7YU5MJxf
— Sergey Osokin (@Creold) November 5, 2024
ここから先は
37字
/
2ファイル
¥ 100
定期マガジンを購読されるとサンプルファイルをダウンロードいただけます。 https://note.com/dtp_tranist/m/mebd7eab21ea5