adding menu items to the right click app menu in the dock casues them to be re-added each time i click
You have to create the NSMenu
inside the method
func applicationDockMenu(_ sender: NSApplication) -> NSMenu? {
let dockMenu = NSMenu()
dockMenu.addItem(withTitle: "test1", action: nil, keyEquivalent: "")
return dockMenu
}
or remove the items
let dockMenu = NSMenu()
func applicationDockMenu(_ sender: NSApplication) -> NSMenu? {
dockMenu.removeAllItems()
dockMenu.addItem(withTitle: "test1", action: nil, keyEquivalent: "")
return dockMenu
}
However I would declare it in Interface Builder and use an outlet.
Adding Items to the Dock Menu from my View Controller in my Cocoa App
Since applicationDockMenu:
is a delegate method, having an instance method add menu items would conflict with the delegate return.
What you could do is make the dock menu a property/instance variable in your application delegate class. This way, your view controller could modify the menu either by passing the reference to the menu from your application delegate to your view controller (which you would have a dockMenu
property) or referencing it globally (less recommended).
class AppDelegate: NSObject, NSApplicationDelegate {
@IBOutlet weak var window: NSWindow!
var dockMenu = NSMenu(title: "MyMenu")
func applicationDidFinishLaunching(aNotification: NSNotification) {
if let viewController = ViewController(nibName: "ViewController", bundle: nil) {
viewController.dockMenu = self.dockMenu
self.window.contentViewController = viewController
}
}
func applicationDockMenu(sender: NSApplication) -> NSMenu? {
return self.dockMenu
}
class ViewController: NSViewController {
var dockMenu: NSMenu?
// Button action
@IBAction func updateDockMenu(sender: AnyObject) {
self.dockMenu?.addItem(NSMenuItem(title: "An Item", action: nil, keyEquivalent: ""))
}
}
How to Make macOS App Window Hidden When Closed and Reopened With Menu Bar Item?
Don't use the Application is agent
approach, but change the activationPolicy
of the NSApp
.
To dynamically hide the icon after closing the (last) window use this in your AppDelegate:
func applicationShouldTerminateAfterLastWindowClosed(_ sender: NSApplication) -> Bool {
NSApp.setActivationPolicy(.accessory)
return false
}
And use something simular to this to initialise your menubar icon and activate the window including a dock icon:
class ViewController: NSViewController {
var status: NSStatusItem?
override func viewDidLoad() {
super.viewDidLoad()
status = NSStatusBar.system.statusItem(withLength: NSStatusItem.variableLength)
status?.button?.title = "Test"
status?.button?.action = #selector(activateWindow(_:))
status?.button?.target = self
}
@IBAction func activateWindow(_ sender: AnyObject) {
NSApp.setActivationPolicy(.regular)
DispatchQueue.main.async {
NSApp.windows.first?.orderFrontRegardless()
}
}
}
MacOSX Add command to Dock icon
There are multiple ways, one would be to assign NSMenu
in applicationDockMenu:
in AppDelegate
.
The second option is to use xib file and Info.plist
. This is Xamarin's tutorial for that, but it is similar in any framework: https://learn.microsoft.com/en-us/xamarin/mac/user-interface/menu
Related Topics
Swift Override Static Method Compile Error
Remove from Array of Anycancellable When Publisher Finishes
How to Find Realm File Location of a MAC App
Include Dictionary or Array Value in Swift String with Backslash Notation
Why Is Swift Counting This Grapheme Cluster as Two Characters Instead of One
Perform Segue After UIalertcontroller Is Dismissed
Cannot Decode Object of Class (Ampathpopupbutton)'
How to Disable The Automatic Activation of an Arcoachingoverlayview
Difference Between Userdefaults() and Userdefaults.Standard
How to Create a Single Page Vertical Scrolling Pdfview in Swift
How to Update UIviewrepresentable with Observableobject
Simulating Pressing The Home Button in Xcode 7 UI Automation
Uisearchcontroller Searchbar Misaligns While Active During Rotation
Swift Combine How Set<Anycancellable> Works
Combine Sink: Ignore Receivevalue, Only Completion Is Needed
Bundle.Main.Path(Forresource... Always Returns Nil When Looking for Xml File
How to Emit Items, One by One, from Collection with a Delay in Rxswift