Skip to main content
Known Participant
December 30, 2021
Answered

Crop all open in Illustrator files

  • December 30, 2021
  • 1 reply
  • 2195 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.

BeffAuthor
Known Participant
January 4, 2022

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;
}

 


Works perfectly, Mark 🙂

Such small improvement and makes people happy.

Thank you very much!