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:
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
...
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
Copy link to clipboard
Copied
Actually I would like to delete all elements, which are on white margins.
Everything is on one layer.
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
Copy link to clipboard
Copied
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;
}
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 ?
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;
}
Copy link to clipboard
Copied
Works perfectly, Mark 🙂
Such small improvement and makes people happy.
Thank you very much!