Sometimes is useful to break out things like colours into a tabular field and then populate these as a set of frames, one for each colour. The frames might contain a Swatch and a swatch name.
The example below iterates though the Colour tabular field, duplicating the frame given the Script Label group. It then populates it with the data for each cell. If the group is anchored in a text frame it will be duplicated immediately after in the text flow.
-- Get the frame by its Script Label
frame = formattingrule:getframe("group");
-- Store the original frame
original_frame = frame;
-- Get the tabular field called "Colour"
mytable = SELECTION.root():getfield('Colour'):content();
-- Loop for each row in the tabular field
for i = 1, mytable:rowcount() do
-- duplicate the frame and populate it with data from the given cell
frame = frame:duplicate(mytable:cell(i,1));
-- Delete the original frame