Skip to main content
Participant
January 7, 2025
Question

Group is visible but one the items inside group is hidden

  • January 7, 2025
  • 1 reply
  • 95 views

Hi everyone, I have a question regarding the script of visible and hidden items/groups in InDesign. I have achieved the goal for scenarios 1 and 2, however, I have an issue and question for scenario 3:

  1.  When a group is visible, and all items inside the group are visible, my expected result is group and all visible items will appear in the article panel.
  2. When a group is hidden, my expected result is group and all items inside the group will not appear in the article panel.
  3.  When a group is visible, and one of the items inside the group is hidden, is it possible to extract the group and the visible items in the article panel?

 

I have tried manually without running the script, the group appears in the article panel if all the items are visible. Is that how InDesign treats a <group>? When I tried to remove items inside the group, I needed to ungroup first then the <group> would be out from the article panel, and all the visible items would be individual appearances. Can someone explain how <group> has been treated in the InDesign?

 

I have attached my script below.

function _addSelectedLayers(ptdArticle, selectedLayers) {
		var addedItemsTracker = {}; // Tracks processed items
		var addedGroupItems = {}; // Tracks items added inside groups
		var hiddenGroups = {}; // Tracks hidden groups using an object

		selectedLayers.forEach(function (layer) {
			if (!layer.visible) return; // Skip invisible layers directly

			var allItems = layer.allPageItems || [];

			allItems.forEach(function (currentItem) {
				var uniqueKey = currentItem.id || currentItem.index; // Ensure unique identification

				if (currentItem instanceof Image) {
					if ((!currentItem.visible || currentItem.visible) && (!currentItem.locked || currentItem.locked)) {
						// continue;
						return;
					}
				}

				// Handle groups
				if (currentItem.constructor.name === "Group") {
					//alert("group");

					if (!currentItem.visible) {
						hiddenGroups[uniqueKey] = true; // Mark group as hidden
						return; // Skip this group
					}

					// Check if the group has any hidden items
					var hasHiddenItems = false;

					currentItem.allPageItems.forEach(function (groupItem) {
						if (!groupItem.visible) {
							hasHiddenItems = true;
						}
					});

					if (hasHiddenItems) {
						//alert("The group is visible, but it contains hidden items.");
						// Optional: Decide what to do with hidden items (e.g., remove, ignore, etc.)
						currentItem.allPageItems.forEach(function (groupItem) {
							if (!groupItem.visible) {
								//ptdArticle.articleMembers.add(currentItem);
								addedItemsTracker[uniqueKey] = true;
							}
						});
					} else {
						//alert("The group is visible, and all its items are visible.");
					}

					// Add visible group
					if (!addedItemsTracker[uniqueKey]) {
						try {
							ptdArticle.articleMembers.add(currentItem);
							addedItemsTracker[uniqueKey] = true;

							// Mark all items inside this group
							currentItem.allPageItems.forEach(function (groupItem) {
								var groupItemKey = groupItem.id || groupItem.index;
								addedGroupItems[groupItemKey] = true;
							});
							//alert(currentItem.allPageItems.length);
						} catch (e) {
							//alert('Error adding group:', e);
						}
					}
				}
				// Handle non-group items
				else {
					//alert("non group");
					var parentGroupKey = currentItem.parent.id || currentItem.parent.index;
					if (addedGroupItems[uniqueKey] || addedGroupItems[parentGroupKey]) {
						return; // Skip adding items that belong to an added group
					}

					if (hiddenGroups[parentGroupKey]) {
						//alert("child group: skip");
						return; // Skip items of hidden parent groups
					}

					// Add visible non-group items
					if (!addedItemsTracker[uniqueKey] && currentItem.visible) {
						try {
							ptdArticle.articleMembers.add(currentItem);
							addedItemsTracker[uniqueKey] = true;
						} catch (e) {
							//alert('Error adding item:', e);
						}
					}
				}
			});
		});
	}

 

1 reply

Robert at ID-Tasker
Legend
January 7, 2025

You can delete item in the group - without ungrouping.