Programmatically create MenuBarExtra elements

I have the following conceptual code:

import SwiftUI

@main
struct SwiftBarApp: App {
    @NSApplicationDelegateAdaptor(AppDelegate.self) var appDelegate

    var body: some Scene {
        WindowGroup {
            ContentView()
        }
        ForEach(appDelegate.menuItems, id:\.self) { menuItemSet in
            MenuBarExtra("Title") {
                MenuBarView(menuItemSet: menuItemSet)
            }
        }
    }

I am attempting to create multiple menu bar items using MenuBarExtra, based upon the content of appDelegate.menuItems. Clearly, my approach does not work as ForEach is supported on Views and not Scenes.

Is there an alternate approach that would allow me to have a variable number of menu bar icons?

Use ForEach inside MenuBarExtra as shown below:

@main
struct SwiftBarApp: App {
    @NSApplicationDelegateAdaptor(AppDelegate.self) var appDelegate
    
    var body: some Scene {
        WindowGroup {
            ContentView()
        }
        MenuBarExtra("Title") {
            ForEach(appDelegate.menuItems, id:\.self) { menuItemSet in
                MenuBarView(menuItemSet: menuItemSet)
            }
        }
    }
}

Here is an example of the outcome I am trying to achieve:

import SwiftUI

@main
struct SwiftBarApp: App {
    @NSApplicationDelegateAdaptor(AppDelegate.self) var appDelegate

    var body: some Scene {
        WindowGroup {
            ContentView()
        }
        MenuBarExtra("Title") {
            MenuBarView(menuItemSet: appDelegate.menuItems[0])
        }
        MenuBarExtra("Title") {
            MenuBarView(menuItemSet: appDelegate.menuItems[1])
        }
    }

MenuBarExtra vs Menu

MenuBarExtra is like adding a mini app that lives on the menu bar

What you are trying to achieve seems similar to what Menu does (https://developer.apple.com/documentation/swiftui/menu)

Menu items could even have keyboard shortcuts.

Pasted below is from Apple's documentation.

Menu("Actions") {
    Button("Duplicate", action: duplicate)
    Button("Rename", action: rename)
    Button("Delete…", action: delete)
    Menu("Copy") {
        Button("Copy", action: copy)
        Button("Copy Formatted", action: copyFormatted)
        Button("Copy Library Path", action: copyPath)
    }
}

Thanks for your responses.

What you have described is not what I am looking for. I want to have multiple instances of MenuBarExtra.

Programmatically create MenuBarExtra elements
 
 
Q