Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
378 views
in Technique[技术] by (71.8m points)

swift - Create Cocoa application without NSMenu

I'd like to create an application that pops up dialogs based on system events. However, this application should not have an NSMenu associated with it.

What I've tried, none of which worked:

  • Removing the NSMenu from the main storyboard but it doesn't allow me to delete it.
  • Overriding NSApplication and setting menu to nil.
  • Looking through Info.plist settings for a setting which allows my app to not have an NSMenu.

Any help would be appreciated.

question from:https://stackoverflow.com/questions/65946048/create-cocoa-application-without-nsmenu

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
/*
  Save this file with name 'delegate_noMenu.swift'
  Create executable in Terminal: swiftc delegate_noMenu.swift -o nm
*/

import Cocoa

class AppDelegate: NSObject, NSApplicationDelegate {
var window:NSWindow!
    
func buildWnd() {
    
let _wndW : CGFloat = 400
let _wndH : CGFloat = 300

 window = NSWindow(contentRect:NSMakeRect(0,0,_wndW,_wndH),styleMask:[.titled, .closable, .miniaturizable, .resizable], backing:.buffered, defer:false)
 window.center()
 window.title = "Swift Test Window"
 window.makeKeyAndOrderFront(window)
 }

func applicationDidFinishLaunching(_ notification: Notification) {
 buildWnd()
}

func applicationShouldTerminateAfterLastWindowClosed(_ sender: NSApplication) -> Bool {
 return true
}
}

let appDelegate = AppDelegate()

// **** main.swift **** //
let app = NSApplication.shared
app.delegate = appDelegate
app.setActivationPolicy(.regular)
app.activate(ignoringOtherApps:true)
app.run()

  1. Save this file with name 'delegate_noMenu.swift'
  2. Create an executable in Terminal by using: swiftc delegate_noMenu.swift -o nm
  3. Manually build a folder system to create an app bundle: A. Create a folder called 'myApp'. B. Inside the top folder create a folder called ‘Contents’. C. Inside ‘Contents’ create two folders called ‘MacOS’ and ‘Resources’. D. Inside ‘Contents’ folder also add an info.plist file to match the image below. Note that Executable file and Bundle name fields are left blank.
  4. Add the executable file ’nm’ to the MacOS folder.
  5. Rename the ‘myApp’ folder to ’nm.app’ which should change the icon and create a double clickable application.
  6. Hopefully you will not see any menu items in the menu bar and have a functional app. I used MacOS 10.14.6.

app_noMenu


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...