I'm not sure this is a 'bug,' I think this is a design choice. The Layer object is a view and, indeed, it does not see master page items. I agree with you that this may be confusing, but there are pros and cons on this issue. If the Layer.pageItems collection had been designed to include both regular page items and master page items, it would also be misleading, especially when we have to deal with overriden objects originated on a master spread. Note that in the InDesign GUI, the Layers panel only displays regular page items, unless you explicitly select a master spread in the Pages panel (in which case the Layers panel only displays the master page items). It makes sense to me that these two levels are not mixed.
As a consequence, myLayer.pageItems (and myLayer.allPageItems) can be empty—in the sense that no regular page item is instantiated on that layer—although the master spreads may contain page items whose itemLayer is myLayer. So, if you need to check whether myLayer is empty at the master level, you probably have to look if it belongs to myDoc.masterSpreads.everyItem().pageItems.everyItem().itemLayer
Thank you for commenting. Indeed I was a little confused ;-)
After thinking about it and doing some testing:
On document level pageItems and allPageItems will find objects on regular pages AND master pages.
(allPageItems will find anchored objects, pageItems will not)
On layer level we have the principal division between regular spreads and master spreads.
So if we have to make sure a particular layer is empty, we have to do two checks:
1. check if: doc.mylayer.pageItems.length = 0
2. loop through all pageItems on master pages and looking if the itemLayer property is equal to the particular layer we want to remove
In that case we do not need to use allPageItems, because there are no anchored objects without an object they are anchored on.
Thank you for clarification…
I'm not sure I agree with you.
Master Page Items can exist on different layers as well, so I don't see why layers applies to local pages any more than master pages. Layer collection are not limited to a specific page.
When a master page item is overridden, what actually happens under the hood is that the master page item is COPIED to the local page and a reference to the master page item is kept. So when you override the master page item, you actually have multiple copies (and each one can be moved to a different layer).
To me it makes much more sense for the collection to return all items, and you can filter out "obj.parent instanceof MasterSpread" if you want.
I'd log it as a bug...
I totally understand your point, but I still think that the Layer.pageItems collection has been deliberately designed to focus on regular page items. In many scripts this is the behavior we implicitly expect from it, as we usually don't want to retrieve master components when we are retrieving layer items. Anyway I could be wrong. Only Adobe devs can tell us whether it is a bug, or a design choice.
I'll file it as a bug, and see what happens...
(Or will that mess up your scripts?)