• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

How to check the pageItem is in artboard area?

Participant ,
May 01, 2022 May 01, 2022

Copy link to clipboard

Copied

Hi All,

 

I need to check the pageitem is in artboard area or not using extendscript JSX.

 

I can get the pageItems in activeDocument. But I want only artboard items.

if (app.documents.length > 0) {
	var getPageItems = app.documents[0].pageItems;
	for (var i=0; i<getPageItems.length; i++) {
		alert(getPageItems[i].constructor.name);
	}
}

 

Kindly anyone help me on this.

 

Thanks.

TOPICS
Scripting

Views

128

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Adobe
Community Expert ,
May 01, 2022 May 01, 2022

Copy link to clipboard

Copied

Does that help a little?

activeDocument.selectObjectsOnActiveArtboard();

 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guide ,
May 01, 2022 May 01, 2022

Copy link to clipboard

Copied

 

var doc = app.documents[0];
var items = doc.pageItems;
for (var i = 0; i < items.length; i++) {
    var b1 = items[i].geometricBounds;
    var b2 = doc.artboards[0].artboardRect;
    var item = items[i].name || items[i].typename + " " + i;
    if ((b1[0] > b2[0] && b1[2] < b2[2]) && 
        (b1[1] < b2[1] && b1[3] > b2[3])) {
        alert(item + " is INSIDE artboard.");
    } else {
        alert(item + " is OUTSIDE artboard.");
    }
}

 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
May 01, 2022 May 01, 2022

Copy link to clipboard

Copied

LATEST

Hi @AD4003, both answers are very helpful already, but since I have this function already written, I thought I would post it here. It isn't perfect, but it does try to handle various situations and give a sensible result. It will give you an array of artboards (usually only one in most cases) that contain a page item.

- Mark

/*
    function to get artboard(s) that
    contain (ie. intersect) a pageitem

    if preferIndex == true, return array
    of artboard indices, otherwise return
    array of artboards (empty if item
    is not positioned on an artboard)
*/


// example usage
var doc = app.activeDocument,
    myItem = doc.selection[0],
    myItemsArtboards = getArtboardsOfItem(doc.artboards, myItem, false);

if (myItemsArtboards.length == 0)
    alert('Item is not on an artboard.');
else
    alert('Item is on\n' + myItemsArtboards.join('\n'));



function getArtboardsOfItem(_artboards, item, preferIndex) {
    // returns array of artboards that intersect with item
    // preferIndex param causes function to return index of each artboard
    _artboards = _artboards || app.activeDocument.artboards;
    var bounds = getItemBounds(item);
    var result = [];
    for (var i = 0; i < _artboards.length; i++) {
        if (boundsDoIntersect(bounds, _artboards[i].artboardRect))
            result.push(preferIndex
                ? i
                : _artboards[i]
            );
    }
    return result
}

// returns visibleBounds or geometricBounds of item
// including correct bounds of clipped group
function getItemBounds(item, geometric, bounds) {
    if (item == undefined) return;
    var newBounds = [];
    if (item.typename == 'GroupItem') {

        var children = item.pageItems,
            contentBounds = [];
        if (item.hasOwnProperty('clipped') && item.clipped == true) {
            // item is clipping group
            var clipBounds;
            for (var i = 0; i < children.length; i++) {
                var child = children[i];
                if (child.hasOwnProperty('clipping') && child.clipping == true) {
                    // the clipping item
                    clipBounds = child.geometricBounds;
                } else {
                    // a clipped content item
                    var b = expandBounds(getItemBounds(child, geometric, bounds), contentBounds);
                }
            }
            if (clipBounds != undefined)
                newBounds = intersectionOfBounds([clipBounds, contentBounds]);
            else
                newBounds = contentBounds;
        }

        else {
            // item is a normal group
            for (var i = 0; i < children.length; i++) {
                var b = expandBounds(getItemBounds(children[i], geometric, bounds), contentBounds);
            }
            newBounds = contentBounds;
        }
    } else {
        // item is not clipping group
        newBounds = geometric ? item.geometricBounds : item.visibleBounds;
    }
    if (bounds == undefined) {
        bounds = newBounds;
    } else {
        bounds = expandBounds(newBounds, bounds);
    }
    return bounds;
}


// returns bounds that encompass two bounds
function expandBounds(b1, b2) {
    var expanded = b2;
    for (var i = 0; i < 4; i++) {
        if (b1[i] != undefined && b2[i] == undefined) expanded[i] = b1[i];
        if (b1[i] == undefined && b2[i] != undefined) expanded[i] = b2[i];
        if (b1[i] == undefined && b2[i] == undefined) return;
    }
    if (b1[0] < b2[0]) expanded[0] = b1[0];
    if (b1[1] > b2[1]) expanded[1] = b1[1];
    if (b1[2] > b2[2]) expanded[2] = b1[2];
    if (b1[3] < b2[3]) expanded[3] = b1[3];
    return expanded;
}

function intersectionOfBounds(arrayOfBounds) {
    bounds = arrayOfBounds.slice(0).sort(function (a, b) { return b[0] - a[0] || a[1] - b[1] });
    var intersection = bounds.shift();
    while (b = bounds.shift()) {
        if (!boundsDoIntersect(intersection, b)) return;
        var l = Math.max(intersection[0], b[0]),
            t = Math.min(intersection[1], b[1]),
            r = Math.min(intersection[2], b[2]),
            b = Math.max(intersection[3], b[3]);
        intersection = [l, t, r, b];
    }
    return intersection;
}

function boundsDoIntersect(b1, b2) {
    return !(
        b2[0] > b1[2] ||
        b2[2] < b1[0] ||
        b2[1] < b1[3] ||
        b2[3] > b1[1]
    );
}

 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines