Created
August 9, 2019 11:04
-
-
Save shspage/02c6d8654cf6b3798b6c0b69d976a891 to your computer and use it in GitHub Desktop.
Illustrator script。オブジェクトの位置によるソートのサンプル。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#target illustrator | |
// 選択オブジェクトを位置によってソートし、 | |
// ソート順に連番の名前をつける。 | |
// ソート方法は sortByPosition を参照 | |
function main(){ | |
var sels = app.activeDocument.selection; | |
if(sels){ | |
sortByPosition(sels); | |
for(var i = 0, iEnd=sels.length; i < iEnd; i++){ | |
sels[i].name = "No." + (i+1); | |
} | |
} | |
} | |
// --------------------- | |
function sortByPosition(r){ | |
// r は PageItem の配列 | |
// 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; | |
} | |
// ---- | |
// ref: https://www.softel.co.jp/blogs/tech/archives/1377 | |
function rMax(r){ | |
return Math.max.apply(null, r); | |
} | |
// ---- | |
function rMin(r){ | |
return Math.min.apply(null, r); | |
} | |
// --------------------- | |
main(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment