9

I am building a macOS-app using SwiftUI and the new App lifecycle.

All the default macOS menu items (like cut, copy, paste) are already there after starting a new project but they’re greyed out. How can I implement methods for these default menu items?

enter image description here

Edit: I am currently using Xcode 12.2 beta 3 (12B5035g) on macOS Big Sur 11.0.1 Beta (20B5012d). I don’t want to solve this with Storyboards or within AppDelegate but instead with SwiftUI and the new App lifecycle.

ixany
  • 5,433
  • 9
  • 41
  • 65
  • Did you try to select some text and then select Copy? For me they work out-of-the-box so to say without any extra code. Or do you need copy some custom types? – Joakim Danielson Oct 31 '20 at 17:24
  • You’re right, it works out of the box if I select the contents of a TextField for example. However I want to be able to run a custom copy function if no specific input field is focused. – ixany Oct 31 '20 at 17:36
  • 1
    You might want to clarify what version of macOS and Xcode you are using. – Joakim Danielson Oct 31 '20 at 18:14
  • I just have clarified my original question. – ixany Oct 31 '20 at 21:07

1 Answers1

3

Have a look at the commands modifier, the CommandGroup and CommandMenu.

@main
struct MyApp: App {

var body: some Scene {
    WindowGroup {
        ContentView()
    }.commands {
        // for example
        CommandGroup(replacing: .help) {
            Button(action: {someActionHere()}) {
                Text("MyApp Help")
            }
        }
        CommandMenu("Edit") {
            // ...
        }
    }
}
}