How can I detect when a MenuBarExtra is opened?

Hi! I have an app that's only a MenuBarExtra (no window), and I want it to run a script when the user clicks its icon from the menu bar.

Currently I have a button on the menu that, when clicked, runs the script and updates empty, placeholder text fields, but this extra step is less than ideal.

class AppState : ObservableObject {
  @Published var textToDisplay = "Script has not run"
}

@main
struct MyApp: App {
  @StateObject var appState = AppState()
   
  var body: some Scene {
    MenuBarExtra("My App", systemImage: "pencil.and.outline") {
       
      // create text bar
      Text(appState.textToDisplay)
       
      // create button runs my script
      Button("Manually Run Script") {
        RunMyScript()
        appState.textToDisplay = "Script finished running"
      }
    }
  }

How can I have the script run automatically when the menu is opened?

Did you ever figure this out?

How can I detect when a MenuBarExtra is opened?
 
 
Q