Figma Support Forum

Findall vs selection evaluation/traversal?

Not a wizard with code, and I’m essentially hacking an existing plugin that we have that changes styles from dark to light theme.

Currently, the plug-in swaps theming/styles for ALL objects in the current page using:

Figma.currentpage.findall()

It swaps styles for basic objects, to complex components, frames, etc everything and walks thru all the child objects - that is all good.

I want it to work on the current selection , so I changed the code to:

Figma.currentpage.selection

This works, but ONLY for parent objects - circles, squares, frame background colours, but nothing nested - does not work on objects within groups, within frames or components.

I “think” this is related to what the selection returns and how it is organized compared to what findall returns?

The rest of the function works fine and steps thru findall, but doesn’t iterate through child objects.

Probably a very elementary question, but any help would be great.

Thanks,
-damian

Selection returns an array of currently selected objects. Since you can’t select both the child and the parent, only the top-level objects are in the selection array. From there you can use findAll on each of the objects in selection which are containers.

1 Like

Thanks very much, @Gleb ! That helps.

So, am I right in saying that the variable currently set by…

const allNodes = figma.currentPage.findall()

is going to be an array that contains every possible node in the page (all parents, children, etc), subsequently processed by another routine….

And if I want to run the entire contents of a selection (not just parents) in place of the entire page for allNodes, then I need to
First define the selection…

const selectedNodes = figma.currentPage.selection

Then that array needs to be walked thru with some function and used with findall for every single parent node to get a similar level of granularity as the array from currentPage.findall()

…and then all of those results added/concatenation to a new array that I could set to the variable ‘allNodes’ to work with the existing follow on routine?

Yep, you are right.

1 Like

ok, thanks very much for all fo the help!
Much appreciated :slight_smile:
-damian