Illustrator: すべてのアートボードのサイズをvisibleBoundsでフィットさせる。解説は記事【解決】全アートボードを中身に合わせてフィットさせたい!(https://sttk3.com/blog/tips/illustrator/fit-artboards-ai.html)へ
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
/** | |
* @file すべてのアートボードのサイズをvisibleBoundsでフィットさせる | |
* @version 1.1.0 | |
* @author sttk3.com | |
* @copyright © 2022 sttk3.com | |
*/ | |
//#target 'illustrator' | |
(function() { | |
// config | |
var boundsType = 'visibleBounds' ; | |
if(app.documents.length <= 0) {return ;} | |
var doc = app.documents[0] ; | |
if(!doc.saved) { | |
var dialogRes = confirm('書類のアートボードをすべてリサイズします。よろしいですか?') ; | |
if(!dialogRes) {return ;} | |
} | |
for(var i = 0, artboardLength = doc.artboards.length ; i < artboardLength ; i++) { | |
fitArtboardToContents(doc, i, boundsType) ; | |
} | |
})() ; | |
/** | |
* 対象アートボードの中身に合わせてアートボードをフィットさせる | |
* @param {Document} doc 対象の書類 | |
* @param {Document} targetArtboardIndex 対象アートボードのindex | |
* @param {String} [boundsType] boundsの種類。'geometricBounds', 'visibleBounds', 'controlBounds'が指定可能。初期値はgeometricBounds | |
* @return {Boolean} 実行したかどうか | |
*/ | |
function fitArtboardToContents(doc, targetArtboardIndex, boundsType) { | |
var res = false ; | |
switch(boundsType) { | |
case 'controlBounds': | |
case 'geometricBounds': | |
case 'visibleBounds': | |
break ; | |
default: | |
boundsType = 'geometricBounds' ; | |
break ; | |
} | |
// 対象アートボードを指定する | |
doc.artboards.setActiveArtboardIndex(targetArtboardIndex) ; | |
// 対象アートボードの中身を取得する | |
doc.selectObjectsOnActiveArtboard(targetArtboardIndex) ; | |
var artboardContens = doc.selection ; | |
doc.selection = [] ; | |
if(artboardContens.length <= 0) {return res ;} | |
// アートボードの中身全体の領域を取得する | |
var rect = boundsOfPageItems(artboardContens, boundsType) ; | |
// アートボードをリサイズする | |
var targetArtboard = doc.artboards[targetArtboardIndex] ; | |
targetArtboard.artboardRect = rect ; | |
res = true ; | |
return res ; | |
} | |
/** | |
* boundsを取得する | |
* @param {Array} targetItem 対象のアイテム | |
* @param {String} [boundsType] boundsの種類。'geometricBounds', 'visibleBounds', 'controlBounds'が指定可能。初期値はgeometricBounds | |
* @return {Array} [left, top, right, bottom] | |
*/ | |
function getBounds(targetItem, boundsType) { | |
switch(boundsType) { | |
case 'controlBounds': | |
case 'geometricBounds': | |
case 'visibleBounds': | |
break ; | |
default: | |
boundsType = 'geometricBounds' ; | |
break ; | |
} | |
// boundsを取得する | |
var outputBounds = targetItem[boundsType] ; | |
// クリッピンググループの場合,マスクのboundsを使う | |
if( (targetItem.constructor.name === 'GroupItem') && (targetItem.clipped) ) { | |
// 高速化・安定化のため,マスク候補は最初のchildのみを調べる | |
if( (targetItem.pageItems.length > 0) && (targetItem.pageItems[0].clipping) ) { | |
outputBounds = targetItem.pageItems[0][boundsType] ; | |
} | |
} | |
return outputBounds ; | |
} | |
/** | |
* 複数のpageItemの最大領域boundsを取得する | |
* @param {Array} targetItems 対象のアイテムの配列 | |
* @param {String} [boundsType] boundsの種類。'geometricBounds', 'visibleBounds', 'controlBounds'が指定可能。初期値はgeometricBounds | |
* @return {Array} [left, top, right, bottom] | |
*/ | |
function boundsOfPageItems(targetItems, boundsType) { | |
switch(boundsType) { | |
case 'controlBounds': | |
case 'geometricBounds': | |
case 'visibleBounds': | |
break ; | |
default: | |
boundsType = 'geometricBounds' ; | |
break ; | |
} | |
var outputBounds = getBounds(targetItems[0], boundsType) ; | |
for (var i = 1, len = targetItems.length ; i < len ; i++) { | |
var currentBounds = getBounds(targetItems[i], boundsType) ; | |
if(outputBounds[0] > currentBounds[0]) {outputBounds[0] = currentBounds[0] ; } | |
if(outputBounds[1] < currentBounds[1]) {outputBounds[1] = currentBounds[1] ; } | |
if(outputBounds[2] < currentBounds[2]) {outputBounds[2] = currentBounds[2] ; } | |
if(outputBounds[3] > currentBounds[3]) {outputBounds[3] = currentBounds[3] ; } | |
} | |
return outputBounds ; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment