• Global community
    • Language:
      • Deutsch
      • English
      • EspaƱol
      • FranƧais
      • PortuguĆŖs
  • ę—„ęœ¬čŖžć‚³ćƒŸćƒ„ćƒ‹ćƒ†ć‚£
    Dedicated community for Japanese speakers
  • ķ•œźµ­ ģ»¤ė®¤ė‹ˆķ‹°
    Dedicated community for Korean speakers
Exit
0

Crop all open in Illustrator files

Explorer ,
Dec 30, 2021 Dec 30, 2021

Copy link to clipboard

Copied

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:

 

 

TOPICS
Draw and design , Scripting

Views

753

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

correct answers 1 Correct answer

Community Expert , Jan 03, 2022 Jan 03, 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 eac
...

Votes

Translate

Translate
Adobe
Community Expert ,
Dec 30, 2021 Dec 30, 2021

Copy link to clipboard

Copied

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

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
Explorer ,
Dec 31, 2021 Dec 31, 2021

Copy link to clipboard

Copied

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

 

Everything is on one layer.

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 ,
Dec 31, 2021 Dec 31, 2021

Copy link to clipboard

Copied

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

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
Explorer ,
Jan 01, 2022 Jan 01, 2022

Copy link to clipboard

Copied

Sure, here I have prepared example file.

 

I made it in CS6 version.

 

https://we.tl/t-7QeMw3vk6q

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 ,
Jan 03, 2022 Jan 03, 2022

Copy link to clipboard

Copied

Hi @Beff, I've written a quick script that worked on your sample file. Let me know if it does what you expect. If you are using CS6, I hope all the features in the script work in the old version.

- Mark

// by m1b, https://community.adobe.com/t5/illustrator-discussions/crop-all-open-in-illustrator-files/m-p/12626375

removeAllButMap();

function removeAllButMap() {
    var doc = app.activeDocument;

    // 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();
    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;
}

 

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
Explorer ,
Jan 03, 2022 Jan 03, 2022

Copy link to clipboard

Copied

Mark, it works perfectly šŸ™‚

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

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 ,
Jan 03, 2022 Jan 03, 2022

Copy link to clipboard

Copied

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

 

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
Explorer ,
Jan 04, 2022 Jan 04, 2022

Copy link to clipboard

Copied

LATEST

Works perfectly, Mark šŸ™‚

Such small improvement and makes people happy.

Thank you very much!

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