Skip to main content
Known Participant
December 30, 2021
Answered

Crop all open in Illustrator files

  • December 30, 2021
  • 1 reply
  • 2193 views

Hello,

I have something like this issue. I have maps, and I'd like to crop the collars in all of them (margins). How to do it by script in all open in Illustrator files, with the same graphical x,y coordinates for all ?

 

All maps have the same size.

 

And they look like below:

 

 

This topic has been closed for replies.
Correct answer m1b

Mark, it works perfectly 🙂

But I see it focus on an active document. How to make it running for all open documents in Illustrator ?


Glad it worked! Here's a version that applies to every open document.

var docs = app.documents;
for (var i = 0; i < docs.length; i++) {
    removeAllButMap(docs[i], false);
}
alert('Processed ' + docs.length + ' files.')

function removeAllButMap(doc, showInfo) {

    app.activeDocument = doc;

    // select all and remove one level of clipping masks
    executeMenuCommand('selectall');
    executeMenuCommand('releaseMask');
    doc.selection = null;

    // loop over the path items, removing each
    // one that doesn't match the criteria
    var items = doc.activeLayer.pageItems,
        keepCount = 0,
        removeCount = 0;
    for (var i = items.length - 1; i >= 0; i--) {
        var item = items[i];
        if (
            item.typename == 'GroupItem'
            && item.clipped
            && item.pageItems.length > 1
            && item.pageItems[0].clipping == true
            // this is the width and height of the map masks:
            && isNearEnough(item.pageItems[0].width, 1299.21, 2)
            && isNearEnough(item.pageItems[0].height, 1639.09, 2)
        ) {
            keepCount++;
        } else {
            item.remove();
            removeCount++;
        }
    }
    app.redraw();
    if (showInfo)
        alert('Removed ' + removeCount + ' items, and kept ' + keepCount + ' items.');
}

// function that compares a value within tolerance:
function isNearEnough(n, m, tolerance) {
    return Math.abs(n - m) < tolerance;
}

 

1 reply

m1b
Community Expert
Community Expert
December 30, 2021

Crop by placing everything in a rectangular clipping group? or by placing a white box with a rectangular hole in it over the top?

- Mark

BeffAuthor
Known Participant
December 31, 2021

Actually I would like to delete all elements, which are on white margins.

 

Everything is on one layer.

m1b
Community Expert
Community Expert
December 31, 2021

One possibility is to define a rectangle, lets call it keepingZone and then check each page item of the document to see if its bounding box intersects with (or possibly is contained by) keepingZone and remove any page item that doesn't (or isn't). Depending on your document construction it could be as simple as this, but is more likely that groups and masks make the process more complex.

It might be best if you share a representative map .ai file.

- Mark