Copy link to clipboard
Copied
as always pls post scripting solutions only and don't waste your time on thinking of manual ways 😉
I wonder if there is a simple way to get all visible objects on different layers to duplicate them Artboard by Artboard? File holds:
- several Artboards
- several Layers (visibility of some true and some false)
- several Objects on those layers (some hidden and some not)
- some are grouped some aren't
Now I want to get all visible Objects only Artboard by Artboard to copy them.
I'm struggeling with visible objects on invisible layers...
Is there a nice proper way to accomplish this task by script?
Only way I'd been thinking of is to cycle through all layers then setting objects of invisible layers to be hidden, then collect all visible objects at the end... there must be a better way!?
Pls see attached screen + rename attached .pdf to .ai for details and testing
Thanks in advance for your help!
Nils
I think this will get you close to what you want if I understand your request...
Script Steps:
In the end, you are left with an object consisting of each artboard name and an array of the visible items within its bounds.
As always,
...Copy link to clipboard
Copied
I think this will get you close to what you want if I understand your request...
Script Steps:
In the end, you are left with an object consisting of each artboard name and an array of the visible items within its bounds.
As always, there are potential gotchas and edge cases (groups, clipping masks, etc.)this rough draft script won't cover. It really depends on how the files you are working with are structured. If you are always dealing with paths you may want to work with pathItems and not pageItems. and more could also cause issues.
For example, in your sample file, you have a group and when you run the script below, the group along with its child items are added to the array. You could catch this edge case and only add the group (skipping the child item) but what happens when you have a visible group with some child items visible and some others hidden?
Hopefully, this gets you headed in the right direction. Cheers!
var doc = app.activeDocument;
// get all artboards and create and object to hold their pageItems
var captured = {};
for (var i = 0; i < doc.artboards.length; i++) {
captured[doc.artboards[i].name] = [];
}
// go through all page items
var item;
for (var i = 0; i < doc.pageItems.length; i++) {
item = doc.pageItems[i];
// first check to see if the page item is visible and its parent layer is visible
if (!item.hidden && item.layer.visible) {
// then iterate over the captured artboards
var ab;
for (var j in captured) {
var ab = doc.artboards.getByName(j);
// and check if the item is within the artboard bounds
if (withinArtboardBounds(item.visibleBounds, ab.artboardRect)) {
captured[j].push(item);
}
}
}
}
// for example purposes, show the capture items for each artboard
for (var a in captured) {
alert(a + "\n" + captured[a]);
}
function withinArtboardBounds(itemVisibleBounds, artboardBounds) {
return (
itemVisibleBounds[0] >= artboardBounds[0] &&
itemVisibleBounds[1] <= artboardBounds[1] &&
itemVisibleBounds[2] <= artboardBounds[2] &&
itemVisibleBounds[3] >= artboardBounds[3]
);
}