How to en-/disable Menu-Object programmatically in Swift

macos menu item disabled

I would like to en-/disable a Menu-Object programmatically in Swift. I found something one the Apple Developer Library:

But how to get a reference to the Menu-Object with the code and set its state programmatically?

Enable or Disable menu item without subclassing with the following code.

let mainMenu = NSApplication.sharedApplication().mainMenu!
let appMenu = mainMenu.itemAtIndex(0)!.submenu

appMenu?.itemWithTitle("Title")?.enabled = false

Disable/enable NSMenu item, Disable/enable NSMenu item � swift xcode macos. I've created a menu bar app, a NSMenu object using the Interface Builder (following this� Swift에서 프로그래밍 방식으로 Menu-Object를 활성화 / 비활성화하는 방법 2016-05-29 swift cocoa menuitem Swift에서 프로그래밍 방식으로 Menu-Object 를 활성화 / 비활성화하고 싶습니다.

For Swift 4+, some of the code in Barath's Answer has become deprecated. I also think the code is more adaptable by searching for menuitem titles rather than their index:

let mainMenu = NSApplication.shared.mainMenu!
let subMenu = mainMenu.item(withTitle: "Edit")?.submenu

subMenu?.item(withTitle: "Cut")?.isEnabled = false

Also, setting isEnabled to true or false will not toggle menu items as long as setAutoenablesItems is set to true. It can be disabled from the storyboard or programmatically.

isEnabled, If a menu item is disabled, its keyboard equivalent is also disabled. See the NSMenuValidation informal protocol specification for cautions regarding this method. I am new to both ObjC & Swift and am looking to understand how to programmatically restart OS X using the kAERestart AppleEvent in Swift. Can someone point me to a Swift implementation of http

You can just set the enabled-attribute programmatically: menObj.enabled = false

Enabling Menu Items, Explains how Cocoa menus and lists work and how to manipulate items within them. Stack Overflow | The World’s Largest Online Community for Developers

macOS Programming Tutorial: Using Menus and the Toolbar, Beginning iOS 13 Programming with Swift � Intermediate iOS 13 This is happening because disabled menu items are not connected to an Open the Library again and this time drag and drop a Menu object on top In it you can assign a tag value to the menu item so you can access it programmatically,� Teams. Q&A for Work. Stack Overflow for Teams is a private, secure spot for you and your coworkers to find and share information.

Beginning iPhone Development with Swift 5: Exploring the iOS SDK, Exploring the iOS SDK Wallace Wang 206 tintColor property, 232,497,498, 500 Toolbar commands, 78–79 hide/show, 79–82 Toolbar 528 IBAction methods, 527, 529 Object Library window, 526 system item popup menu, 528 preview feature, 45–49 programmatically, create objects, 22 Progress View, 200, 201 vs. Stack Overflow | The World’s Largest Online Community for Developers

iOS 10 App Development Essentials: Learn to Develop iOS 10 Apps , Learn to Develop iOS 10 Apps Using Xcode 8 and Swift 3 Neil Smyth Click twice on the Image View object in the scene so that the width constraint marker Use the Align menu to position the Button horizontally within the container and the top edge using the current value with the Constrain to Margins option disabled. Is there a solution to setting the system's master volume from within my Swift app? I read a lot about AudioToolbox and read some source examples in Objective-C. For example, I found this: Setting Mac OS X Volume Programatically after 10.6. But I can't get it working in Swift.