• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

Illustrator scripting - copy only visible objects on different layers by artboard

Community Expert ,
Feb 13, 2023 Feb 13, 2023

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

TOPICS
Scripting

Views

377

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
Adobe
Engaged ,
Feb 13, 2023 Feb 13, 2023

Copy link to clipboard

Copied

LATEST

I think this will get you close to what you want if I understand your request...

 

Script Steps:

  1.  Store each artboard into an object with an empty array
  2.  Iterate over all of the document page items
    1.  First, check to see if the item is visible and if the item's parent layer is visible
    2.  Then check to see if the item's visible bounds fit within the bounds of any artboard

 

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

 

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