Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Illustratorで選択しているオブジェクトのCMYK値を入れ替えるExtendScript
var palette = new Window('palette', 'colorChanger');
var lists = [], buttons = [];
(function () {
var titles = ['塗り:', '線 :'];
var selectListItems = ['変更しない', 'シアン←→マゼンタ', 'シアン←→イエロー', 'マゼンタ←→イエロー'];
var buttonTitles = ['キャンセル', 'OK'];
palette.add('staticText', undefined, 'CMYK版を入れ替えるJavaScript');
var listContainer = palette.add('group', undefined);
listContainer.alignChildren = ['fill', 'center'];
listContainer.orientation = 'column';
lists.push(listContainer.add('dropDownList', undefined, selectListItems));
lists.push(listContainer.add('dropDownList', undefined, selectListItems));
for (var i = 0, len = lists.length; i < len; i++) {
lists[i].selection = 0;
lists[i].title = titles[i];
}
var buttonContainer = palette.add('group', undefined);
buttonContainer.alignChildren = ['fill', 'center'];
buttonContainer.orientation = 'row';
buttons.push(buttonContainer.add('button', undefined, buttonTitles[0], {name: 'cancel'}));
buttons.push(buttonContainer.add('button', undefined, buttonTitles[1], {name: 'ok'}));
})();
buttons[0].onClick = function () {
palette.close();
}
buttons[1].onClick = function () {
buttons[1].active = false;
var bt = new BridgeTalk(); // BridgeTalkしておく(動かないときがあるっぽい)
bt.target = 'Illustrator'; // ターゲットをIllustratorにする
bt.body = 'main()'; // 実際のIllustratorの処理
bt.send(); // BridgeTalkに送る
};
palette.show();
function main() {
if (app.documents.length == 0) {
alert('ドキュメントが開かれていません');
return;
}
var selObjs = app.selection;
if (selObjs.length == 0) {
alert('オブジェクトが選択されていません');
return;
}
for (var k = 0, selLen = selObjs.length; k < selLen; k++) {
var elem = selObjs[k];
// undefinedのときはスキップする
if (elem == undefined) {
continue;
}
if (elem.typename == 'TextFrame') {
for (var i = 0, cLen = elem.textRange.characters.length; i < cLen; i++) {
var chara = elem.textRange.characters[i];
for (var j = 0, lLen = lists.length; j < lLen; j++) {
var list = lists[j];
if (list.selection.index == 0) {
continue;
}
var isFillColor = (j == 0);
changeColor(chara, isFillColor);
}
}
} else {
for (var i = 0, lLen = lists.length; i < lLen; i++) {
var list = lists[i];
if (list.selection.index == 0) {
continue;
}
var isFillColor = (i == 0);
changeColor(elem, isFillColor);
}
}
}
if (selObjs.typename == 'TextRange') {
for (var i = 0, len = selObjs.characters.length; i < len; i++) {
var chara = selObjs.characters[i];
for (var j = 0, lLen = lists.length; j < lLen; j++) {
var list = lists[j];
if (list.selection.index == 0) {
continue;
}
var isFillColor = (j == 0);
changeColor(chara, isFillColor);
}
}
}
}
function changeColor(obj, isFillColor) {
var currentColorObj = undefined; // 現在のカラー
var currentColorComponent = undefined; // 現在のカラー成分
var tint = undefined; // 濃淡
var list = undefined;
var isSpotColor = false;
if (isFillColor) {
currentColorObj = obj.fillColor;
list = lists[0];
} else {
currentColorObj = obj.strokeColor;
list = lists[1];
}
// CMYK
if (currentColorObj.typename == 'CMYKColor') {
currentColorComponent = {cyan: Math.round(currentColorObj.cyan),
magenta: Math.round(currentColorObj.magenta),
yellow: Math.round(currentColorObj.yellow),
black: Math.round(currentColorObj.black)};
}
// スポットカラー
else if (currentColorObj.typename == 'SpotColor') {
// レジストレーションは処理しない
if (currentColorObj.spot.name == '[レジストレーション]') {
return;
} else {
tint = currentColorObj.spot.tint;
var internalColor = currentColorObj.spot.getInternalColor();
currentColorComponent = {cyan: Math.round(internalColor.cyan),
magenta: Math.round(internalColor.magenta),
yellow: Math.round(internalColor.yellow),
black: Math.round(internalColor.black)};
}
isSpotColor = true;
}
// 現在のカラーが取得できなければ処理しない
if (currentColorComponent == undefined) {
return;
}
var newColorComponent = {cyan: currentColorComponent.cyan,
magenta: currentColorComponent.magenta,
yellow: currentColorComponent.yellow,
black: currentColorComponent.black};
// カラー変更
switch (list.selection.index) {
case 0:
// 変更しない
break;
case 1:
// シアン←→マゼンタ
newColorComponent.cyan = currentColorComponent.magenta;
newColorComponent.magenta = currentColorComponent.cyan;
break;
case 2:
// シアン←→イエロー
newColorComponent.cyan = currentColorComponent.yellow;
newColorComponent.yellow = currentColorComponent.cyan;
break;
case 3:
// マゼンタ←→イエロー
newColorComponent.magenta = currentColorComponent.yellow;
newColorComponent.yellow = currentColorComponent.magenta;
break;
}
//スポットの名前を作成
var spotName = "NC C=" + newColorComponent.cyan +
" M=" + newColorComponent.magenta +
" Y=" + newColorComponent.yellow +
" K=" + newColorComponent.black;
var spotColor = undefined;
//スウォッチの重複をチェックするためにtry~catchでくくる
try {
spotColor = activeDocument.swatches[spotName].color;
} catch (e){ // スウォッチが存在しなかったら
// 新しいカラーを元にCMYKのスポットカラーを作成
spotColor = addSpotColor(newColorComponent, spotName,);
}
if (isFillColor) {
obj.filled = true; // 塗りをありに
obj.fillColor = spotColor;
} else {
obj.stroked = true; // 線をありに
obj.strokeColor = spotColor;
}
if (isSpotColor) {
currentColorObj.tint = tint;
}
}
// スポットカラー生成
function addSpotColor(newColorComponent, spotName) {
var cmykColor = new CMYKColor(); // CMYKColor生成
cmykColor.cyan = newColorComponent.cyan;
cmykColor.magenta = newColorComponent.magenta;
cmykColor.yellow = newColorComponent.yellow;
cmykColor.black = newColorComponent.black;
var newSpotColor = activeDocument.spots.add();
newSpotColor.color = cmykColor;
newSpotColor.name = spotName;
var spotColor = new SpotColor();
spotColor.spot = newSpotColor;
return spotColor;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.