Display window on OSX using Swift without XCode or NIB - swift

Disclaimer: I'm attempting the following exercise because I think it will be instructive. I'm interested in how it might be done. So please don't be too hasty to jump in with "This is the wrong way to do it, you should never do it like this!"
Working from the commandline with my favourite text editor, I would like to construct a minimal Swift program that displays a window.
It's a GUI/Cococa hello world, if you like.
In the same spirit, I want to avoid NIB.
So, No XCode, No NIB.
I would like to:
compile it with the swift compiler
create a #! swift script which runs using the Swift interpreter
If I can do both of those things I will feel my feet on the ground and be much more at ease upgrading to Xcode.
I tried the following:
window.swift
import Cocoa
#NSApplicationMain
class AppDelegate: NSObject, NSApplicationDelegate {
#IBOutlet weak var window: NSWindow!
let newWindow = NSWindow(contentRect : NSScreen.mainScreen()!.frame
, styleMask : NSBorderlessWindowMask
, backing : NSBackingStoreType.Buffered
, defer : false)
func applicationDidFinishLaunching(aNotification: NSNotification) {
// Insert code here to initialize your application
newWindow.opaque = false
newWindow.movableByWindowBackground = true
newWindow.backgroundColor = NSColor.whiteColor()
newWindow.makeKeyAndOrderFront(nil)
}
func applicationWillTerminate(aNotification: NSNotification) {
// Insert code here to tear down your application
}
}
However, attempting to run this from the command line fails:
pi#piBookAir.local ~ /Users/pi/dev/macdev:
⤐ swift window.swift
window.swift:3:1: error: 'NSApplicationMain' attribute cannot be used in a
module that contains top-level code
#NSApplicationMain
^
window.swift:1:1: note: top-level code defined in this source file
import Cocoa
^
✘
What's the correct way to eliminate the error?

Porting this code from objective-c to Swift, you get
import Cocoa
let nsapp = NSApplication.shared()
NSApp.setActivationPolicy(NSApplicationActivationPolicy.regular)
let menubar = NSMenu()
let appMenuItem = NSMenuItem()
menubar.addItem(appMenuItem)
NSApp.mainMenu = menubar
let appMenu = NSMenu()
let appName = ProcessInfo.processInfo.processName
let quitTitle = "Quit " + appName
let quitMenuItem = NSMenuItem.init(title:quitTitle,
action:#selector(NSApplication.terminate),keyEquivalent:"q")
appMenu.addItem(quitMenuItem);
appMenuItem.submenu = appMenu;
let window = NSWindow.init(contentRect:NSMakeRect(0, 0, 200, 200),
styleMask:NSTitledWindowMask,backing:NSBackingStoreType.buffered,defer:false)
window.cascadeTopLeft(from:NSMakePoint(20,20))
window.title = appName;
window.makeKeyAndOrderFront(nil)
NSApp.activate(ignoringOtherApps:true)
NSApp.run()
Save it as minimal.swift, compile
swiftc minimal.swift -o minimal
and run
./minimal
You will get an empty window and a menu bar with a menu named like the application and a quit button.
Why it works exactly, I don't know. I'm new to Swift and Cocoa programming, but the linked website explains a bit.

Make a file TestView.swift (like this):
import AppKit
class TestView: NSView
{
override init(frame: NSRect)
{
super.init(frame: frame)
}
required init?(coder: NSCoder)
{
fatalError("init(coder:) has not been implemented")
}
var colorgreen = NSColor.greenColor()
override func drawRect(rect: NSRect)
{
colorgreen.setFill()
NSRectFill(self.bounds)
let h = rect.height
let w = rect.width
let color:NSColor = NSColor.yellowColor()
let drect = NSRect(x: (w * 0.25),y: (h * 0.25),width: (w * 0.5),height: (h * 0.5))
let bpath:NSBezierPath = NSBezierPath(rect: drect)
color.set()
bpath.stroke()
NSLog("drawRect has updated the view")
}
}
Make a file TestApplicationController.swift (like this):
import AppKit
final class TestApplicationController: NSObject, NSApplicationDelegate
{
/// Seems fine to create AppKit UI classes before `NSApplication` object
/// to be created starting OSX 10.10. (it was an error in OSX 10.9)
let window1 = NSWindow()
let view1 = TestView(frame: NSRect(x: 0, y: 0, width: 1000, height: 1000))
func applicationDidFinishLaunching(aNotification: NSNotification)
{
window1.setFrame(CGRect(x: 0, y: 0, width: 1000, height: 1000), display: true)
window1.contentView = view1
window1.opaque = false
window1.center();
window1.makeKeyAndOrderFront(self)
// window1.backgroundColor = view1.colorgreen
// window1.displayIfNeeded()
}
func applicationWillTerminate(aNotification: NSNotification) {
// Insert code here to tear down your application
}
}
Make a file main.swift (like this):
//
// main.swift
// CollectionView
//
// Created by Hoon H. on 2015/01/18.
// Copyright (c) 2015 Eonil. All rights reserved.
//
import AppKit
let app1 = NSApplication.sharedApplication()
let con1 = TestApplicationController()
app1.delegate = con1
app1.run()
The last file must not be renamed, main.swift is apparently a special name for swift (otherwise the example will not compile).
Now, enter this (to compile the example):
swiftc -sdk /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.11.sdk TestView.swift TestApplicationController.swift main.swift
Run the code by entering:
./main
It shows a main window (centered) with a nice green collor and a yellow rectangle within it).
You can kill the app by entering Control-C.
Note that this is a swift compilation not an interpreter running, so you have a native app.
Note that -sdk and the path to MacOSX10.11.sdk is essential (otherwise the code will not compile).
Note also that this compilation depends on the latest Xcode distribution, so update MacOSX10.11.sdk to MacOSX10.10.sdk or whatever is in the SDKs directory.
It took a while to find this out ...

Based on the other solutions I wrote a recent version of a single file app.
Requirements: Swift 5.6, Command Line Tools, no XCode, no XIB, no Storyboard.
One file, one class, run it with swift app.swift.
file: app.swift
import AppKit
class App : NSObject, NSApplicationDelegate {
let app = NSApplication.shared
let name = ProcessInfo.processInfo.processName
let status = NSStatusBar.system.statusItem(withLength: NSStatusItem.variableLength)
let window = NSWindow.init(
contentRect: NSMakeRect(0, 0, 200, 200),
styleMask: [.titled, .closable, .miniaturizable],
backing: .buffered,
defer: false
)
override init() {
super.init()
app.setActivationPolicy(.accessory)
window.center()
window.title = name
window.hidesOnDeactivate = false
window.isReleasedWhenClosed = false
let statusMenu = newMenu()
status.button?.title = "🤓"
status.menu = statusMenu
let appMenu = newMenu()
let sub = NSMenuItem()
sub.submenu = appMenu
app.mainMenu = NSMenu()
app.mainMenu?.addItem(sub)
}
#IBAction func activate(_ sender:Any?) {
app.setActivationPolicy(.regular)
DispatchQueue.main.async { self.window.orderFrontRegardless() }
}
#IBAction func deactivate(_ sender:Any?) {
app.setActivationPolicy(.accessory)
DispatchQueue.main.async { self.window.orderOut(self) }
}
private func newMenu(title: String = "Menu") -> NSMenu {
let menu = NSMenu(title: title)
let q = NSMenuItem.init(title: "Quit", action: #selector(app.terminate(_:)), keyEquivalent: "q")
let w = NSMenuItem.init(title: "Close", action: #selector(deactivate(_:)), keyEquivalent: "w")
let o = NSMenuItem.init(title: "Open", action: #selector(activate(_:)), keyEquivalent: "o")
for item in [o,w,q] { menu.addItem(item) }
return menu
}
func applicationDidFinishLaunching(_ n: Notification) { }
func applicationDidHide(_ n: Notification) {
app.setActivationPolicy(.accessory)
DispatchQueue.main.async { self.window.orderOut(self) }
}
}
let app = NSApplication.shared
let delegate = App()
app.delegate = delegate
app.run()
The 64 lines of code above provide the following features and fixes over the previous solutions:
app menu is visible and usable
status menu can be clicked and is usable
both menus have working key keybindings
dock item appears when window is open:
app.setActivationPolicy(.regular)
dock item hides when window is closed:
app.setActivationPolicy(.accessory)
window is preserved on close and reused on open:
window.hidesOnDeactivate = false
window.isReleasedWhenClosed = false
Tested on M1 Pro. Here is how it looks.

Pass the -parse-as-library flag to swiftc:
swiftc -parse-as-library window.swift
That said, you'll just end up running into a second error when you do this:
2015-06-10 14:17:47.093 window[11700:10854517] No Info.plist file in application bundle or no NSPrincipalClass in the Info.plist file, exiting

Related

Draw shape around cursor on macOS using Swift [duplicate]

This question already has an answer here:
How do I change my cursor on the whole screen? (not only current view/window)
(1 answer)
Closed 1 year ago.
I'm pretty new to macOS and swift development and I have been watching a few tutorials to figure this out. I also watched a udemy course that worked on 18 macOS projects which got me nowhere
All I want to do is a macOS menu bar app that will add a cursor highlight that should look something like:
I could get the cursor changed to an image doing the following
import SpriteKit
class CursorView: SKView {
override func resetCursorRects() {
if let targetImage = NSImage(named: "cursor") {
let cursor = NSCursor(image: targetImage,
hotSpot: CGPoint(x: targetImage.size.width / 2,
y: targetImage.size.height / 2))
addCursorRect(frame, cursor: cursor)
}
}
}
Three things wrong with this:
SKView is a class from SpriteKit and I don't think I should use that for my use-case
This calls the addCursorRect that add the changes to a window frame (I need to all the time regardless of the frame)
I can't have 100's of images for each style I would set in the future for the highlight color, size, or opacity
So, I'm here trying to understand how I can do this for a menu bar app that should be available on all screens and achieve a highlight as should in the above picture
Not sure if this matters but I'm using storyboard and don't mind switching to SwiftUI
I could really use some help from the community on this. Thank you
You can annotate the system mouse by drawing something around it. This can be done by
adding an CGEvent tap to capture mouse events
drawing the annotation around the cursor using a custom window
Here is a simple example application
import SwiftUI
#main
class AppDelegate: NSObject, NSApplicationDelegate {
var mouseTap: CFMachPort?
private var window: NSWindow = {
// Create the SwiftUI view that provides the window contents.
let contentView = Circle()
.stroke(lineWidth: 2)
.foregroundColor(.blue)
.frame(width: 30, height: 30)
.padding(2)
// Create the window and set the content view.
let window = NSWindow(
contentRect: NSRect(x: 0, y: 0, width: 34, height: 34),
styleMask: [.borderless],
backing: .buffered,
defer: false
)
window.contentView = NSHostingView(rootView: contentView)
window.backgroundColor = .clear
window.level = NSWindow.Level.statusBar
window.makeKeyAndOrderFront(nil)
return window
}()
func applicationDidFinishLaunching(_ aNotification: Notification) {
if let tap = createMouseTap() {
if CGEvent.tapIsEnabled(tap: tap) {
let runLoopSource = CFMachPortCreateRunLoopSource(kCFAllocatorDefault, tap, 0)
CFRunLoopAddSource(CFRunLoopGetCurrent(), runLoopSource, CFRunLoopMode.commonModes)
mouseTap = tap
} else {
print("tap not enabled")
mouseTap = nil
}
} else {
print("tap not enabled")
}
}
func createMouseTap() -> CFMachPort? {
withUnsafeMutableBytes(of: &window) { pointer in
CGEvent.tapCreate(
tap: .cgSessionEventTap,
place: .headInsertEventTap,
options: CGEventTapOptions.listenOnly,
eventsOfInterest: (1 << CGEventType.mouseMoved.rawValue | 1 << CGEventType.leftMouseDragged.rawValue),
callback: mouseMoved,
userInfo: pointer.baseAddress
)
}
}
}
func mouseMoved(proxy: CGEventTapProxy, type: CGEventType, event: CGEvent, context: UnsafeMutableRawPointer?) -> Unmanaged<CGEvent>? {
let window = context!.assumingMemoryBound(to: NSWindow.self).pointee
// using CGPoint+SIMD extension from https://gist.github.com/Dev1an/7973cee9d960479b35b705f88b7f38c4
window.setFrameOrigin(event.unflippedLocation - 17)
return nil
}
Drawback
Note that this implementation does require the user to allow the keyboard input option in "Universal Access" in "System Preferences".

NSWorkspace didMountNotification / didUnmountNotification not triggered

I am in the process of creating a new macOS application that is going to perform some action whenever a USB device (or other storage device) has been attached to the computer. It also needs to run silently in the background.
For the USB part, I have added the following class to the project:
import Foundation
import AppKit
class UsbDetector {
init() {
print("Hello from UsbDetector")
let notificationCenter = NSWorkspace.shared.notificationCenter
notificationCenter.addObserver(self, selector: #selector(didMount(_:)), name: NSWorkspace.didMountNotification, object: nil)
notificationCenter.addObserver(self, selector: #selector(didUnmount(_:)), name: NSWorkspace.didUnmountNotification, object: nil)
}
#objc func didMount(_ notification: NSNotification) {
print("Hello from mounted")
if let devicePath = notification.userInfo!["NSDevicePath"] as? String {
print("Mounted: \(devicePath)")
}
}
#objc func didUnmount(_ notification: NSNotification) {
print("Hello from unmounted")
if let devicePath = notification.userInfo!["NSDevicePath"] as? String {
print("Unmounted: \(devicePath)")
}
}
}
I instantiate the UsbDetector from AppDelegate.swift as follows:
import Cocoa
import SwiftUI
#NSApplicationMain
class AppDelegate: NSObject, NSApplicationDelegate {
var window: NSWindow!
func applicationDidFinishLaunching(_ aNotification: Notification) {
print("Hello from AppDelegate")
// Start UsbDetector
_ = UsbDetector()
// Create the SwiftUI view that provides the window contents.
let contentView = ContentView()
// Create the window and set the content view.
window = NSWindow(
contentRect: NSRect(x: 0, y: 0, width: 480, height: 300),
styleMask: [.titled, .closable, .miniaturizable, .resizable, .fullSizeContentView],
backing: .buffered, defer: false)
window.center()
window.setFrameAutosaveName("Main Window")
window.contentView = NSHostingView(rootView: contentView)
window.makeKeyAndOrderFront(nil)
}
func applicationWillTerminate(_ aNotification: Notification) {
// Insert code here to tear down your application
}
}
To make it run in the background, I have added the following key to the Info.plist file of the project:
<key>LSBackgroundOnly</key>
<true/>
When the app is started, it is visible in the Activity Monitor but nowhere else, which is fine. But when I attach / detach a USB stick, the NSWorkspace event is not triggered. All that gets printed to the console is:
Hello from AppDelegate
Hello from UsbDetector
I am using Xcode 11.5 and macOS Catalina 10.15.5.
Can someone tell me what I am doing wrong here? Thanks in advance!

Adding a new label where clicked SWIFT

I would like to write a simple swift code for a MacOS app which add a new label wherever the user perform a mouse click in a window.
This code compiles but make the app crash:
#IBOutlet var here2 = [NSTextField]()
var count: Int = 0
func getCoordinates(){
NSEvent.addLocalMonitorForEvents(matching: [.leftMouseDown]) {
if self.location.x < 700 && self.location.y<750 {
self.here2.append(NSTextField.init())
self.here2[self.count].frame.origin = CGPoint(x: self.location.x, y: self.location.y)
self.here2[self.count].stringValue = String(self.count)
print("count is: " + String(self.here2.count))
self.count+=1
}
return $0
}
Here is a programmatic approach which may be run in Terminal. Note that window.acceptsMouseMovedEvents is set to true.
//May be run in Terminal with:
//swiftc label.swift -framework Cocoa -o label && ./label
import Cocoa
class AppDelegate: NSObject, NSApplicationDelegate {
var window : NSWindow!
var here2 = [NSTextField]()
var count: Int = 0
let _wndW : CGFloat = 700
let _wndH : CGFloat = 750
let _labelW : CGFloat = 24
let _labelH : CGFloat = 24
#objc func getCoordinates(_ sender:AnyObject ) {
NSEvent.addLocalMonitorForEvents(matching: [.leftMouseDown]) {
var pt: NSPoint? { self.window.mouseLocationOutsideOfEventStream }
if let location = pt {
let msPt:NSPoint = self.window.contentView!.convert(location, from: nil)
print("x = \(msPt.x) : y = \(msPt.y)")
if msPt.x < (self._wndW - self._labelW) && msPt.y < (self._wndH - self._labelH ) {
self.here2.append(NSTextField.init())
self.here2[self.count].frame = NSMakeRect(msPt.x, msPt.y, self._labelW, self._labelH)
print(self.here2[self.count].frame)
self.window.contentView!.addSubview (self.here2[self.count])
self.here2[self.count].backgroundColor = NSColor.white
self.here2[self.count].isSelectable = false
self.here2[self.count].stringValue = String(self.count)
print("count is: " + String(self.here2.count))
self.count+=1
}
}
return $0
}
}
func buildMenu() {
let mainMenu = NSMenu()
NSApp.mainMenu = mainMenu
// **** App menu **** //
let appMenuItem = NSMenuItem()
mainMenu.addItem(appMenuItem)
let appMenu = NSMenu()
appMenuItem.submenu = appMenu
appMenu.addItem(withTitle: "Quit", action:#selector(NSApplication.terminate), keyEquivalent: "q")
}
func buildWnd() {
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)
window.acceptsMouseMovedEvents = true
// **** Button **** //
let myBtn = NSButton (frame:NSMakeRect( _wndW - 180, 15, 135, 24 ))
myBtn.bezelStyle = .rounded
myBtn.autoresizingMask = [.maxXMargin,.minYMargin]
myBtn.title = "Start Label Maker"
myBtn.action = #selector(self.getCoordinates(_:))
window.contentView!.addSubview (myBtn)
// **** Quit btn **** //
let quitBtn = NSButton (frame:NSMakeRect( _wndW - 50, 10, 40, 40 ))
quitBtn.bezelStyle = .circular
quitBtn.autoresizingMask = [.minXMargin,.maxYMargin]
quitBtn.title = "Q"
quitBtn.action = #selector(NSApplication.terminate)
window.contentView!.addSubview(quitBtn)
}
func applicationDidFinishLaunching(_ notification: Notification) {
buildMenu()
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()
You cannot call copy on NSTextView, that causes the error message.
You implement all the required methods by subclassing the view, but I'd suggest you allocate new text views programmatically and set their style in code instead of Interface builder.
Here's another thread dealing with copying objects: "[something copyWithZone:]: unrecognized selector sent to instance" when using Bindings / Core Data
You can't connect and array of NSTextField in a storyboard. In my opinion it's a bug in Xcode, IB shouldn't make the connection. It's also a bug in AppKit, I tried this and got this weird error:
Failed to set (contentViewController) user defined inspected property on (NSWindow): [<__NSTimeZone 0x6040000a2280> valueForUndefinedKey:]: this class is not key value coding-compliant for the key identifier.
Solution: remove the connection in IB and change
#IBOutlet var here2 = [NSTextField]()
to
var here2 = [NSTextField]()
#IBOutlet weak var here2TextField: NSTextField!
Connect here2TextField in IB and append here2TextField to here2 in viewDidLoad().
#Marco: The code which I posted is a template (aka boilerplate) example of the programmatic approach to creating applications. All that I did was copy/paste your function into the AppDelegate class of the template and connect it to a button. From there I revised your code until the desired result was obtained. Instead of using a XIB or storyboard the technique requires that source code be written by the author (or use templates). The good news is that you have control of your application and nothing is done behind your back; what you see is what you get. The downside is that the technique is cumbersome for really large projects; for this I rely on Xcode with XIBs. The autocompletion feature of Xcode is also very helpful, while with the programmatic approach you have to rely on Xcode/Help/DeveloperDocumentation as well as internet searches, especially StackOverflow (usually someone else has had the same problem that you are experiencing). In lieu of using the CommandLine from Terminal for every compile I automated the process with a very simple editor which was written in Swift using Xcode (with XIB). However, the posted code may also be run in Xcode by doing the following:
1) Create a new macOS App using Swift (User Interface is XIB but it won’t be needed for the demo)
2) Use File/New/File to add a Swift file and name it ‘main.swift’
3) In the new main.swift file change ‘import Foundation’ to ‘import Cocoa’
4) Copy/paste the last five lines of the demo (labelled main.swift) into this file
5) Go into the AppDelegate file that Apple provided and delete everything there except for ‘import Cocoa’
6) Copy/paste the entire AppDelegate class of the posted code (down to the main.swift section which you have already used)
7) Hit the ‘Run’ button, and it should compile without error.
Hope it helps and good luck with your project. Thanks for posting.

NSApplication menubar does not respond as expected in nib-less Cocoa application

I have the following single file of code, in which I'm trying to create a Cocoa application with basic functionality in as little code as possible without using nibs or Xcode. I have been getting most of my information from the following blog post, in which the equivalent Objective-C code has been posted: (http://www.cocoawithlove.com/2010/09/minimalist-cocoa-programming.html). The only major change that I have made is an AppDelegate class to manage the window, which is typically what is done in Xcode projects.
import Cocoa
class AppDelegate: NSObject, NSApplicationDelegate {
var window: NSWindow
override init() {
self.window = NSWindow()
self.window.setFrame(NSRect(x: 0, y: 0, width: 1280, height: 720), display: true)
self.window.collectionBehavior = .FullScreenPrimary
self.window.styleMask = NSTitledWindowMask | NSClosableWindowMask | NSMiniaturizableWindowMask
self.window.title = "Main Window"
}
func applicationDidFinishLaunching(notification: NSNotification) {
window.makeKeyAndOrderFront(self)
}
func applicationShouldTerminateAfterLastWindowClosed(sender: NSApplication) -> Bool {
return true
}
}
autoreleasepool {
NSApplication.sharedApplication()
NSApp.setActivationPolicy(.Regular)
let delegate = AppDelegate()
NSApp.delegate = delegate
let mainMenu = NSMenu()
NSApp.mainMenu = mainMenu
let applicationMenuItem = NSMenuItem()
mainMenu.addItem(applicationMenuItem)
let applicationMenu = NSMenu()
let quitMenuItem = NSMenuItem()
quitMenuItem.action = #selector(NSApp.terminate(_:))
quitMenuItem.keyEquivalent = "q"
quitMenuItem.title = "Quit Cocoa Window Test"
applicationMenu.addItem(quitMenuItem)
applicationMenuItem.submenu = applicationMenu
NSApp.activateIgnoringOtherApps(true)
NSApp.run()
}
I successfully compile from Terminal with the following command:
swiftc -o bin/CocoaWindowTest -g -framework Cocoa ./src/main.swift
My issue arises with the menu. Although the ⌘Q keyboard shortcut works as expected, the application menu that I've created won't open, and I haven't been able to figure out why.
Solved the problem by calling NSApp.activateIgnoringOtherApps() after NSApp.run() has already been called. To summarize, the main segment of my code now reads something like this:
autoreleasepool {
NSApplication.sharedApplication()
NSApp.setActivationPolicy(.Regular)
let delegate = AppDelegate()
NSApp.delegate = delegate
// Menu setup here...
NSApp.run()
NSApp.activateIgnoringOtherApps(true)
}
You should use mainMenu.setSubmenu(applicationMenu, forItem:applicationMenuItem) to set the submenu. Items with submenus have a special action, submenuAction(_:), assigned, which is responsible for actually showing the submenu. The above method properly assigns that action (and is preferred to setting it yourself).
For what it's worth, I wouldn't set NSApp.mainMenu until the menu is complete.

How can I make an editable text field in a swift shell application

I'm trying to make a editable text region in a NSWindow. So far I can make
a window and add a text field - but when I select it and type characters the characters are echoed in the shell and NOT the text area.
NOTE: this is NOT an Xcode project - I am trying to do this in a single
file in the shell - my goal is to do this in code only
To replicate the error put the following code into a file (experiment.swift) and give the shell command
> swift experiment.swift
Here's the code
import Cocoa
class MyAppDelegate: NSObject, NSApplicationDelegate {
let window = NSWindow()
let ed = NSTextField(frame: NSMakeRect(20, 10, 180, 160))
func applicationDidFinishLaunching(aNotification: NSNotification) {
window.setContentSize(NSSize(width:600, height:200))
window.styleMask = NSTitledWindowMask | NSClosableWindowMask |
NSMiniaturizableWindowMask |
NSResizableWindowMask
window.opaque = false
window.center();
window.title = "My window"
ed.font = NSFont(name:"Helvetica Bold", size:20)
ed.stringValue = "edit me"
ed.editable = true
ed.selectable = true
window.contentView!.addSubview(ed)
window.makeKeyAndOrderFront(window)
window.level = 1
}
func applicationWillTerminate(aNotification: NSNotification) {
// Insert code here to tear down your application
}
}
let app = NSApplication.sharedApplication()
let obj = MyAppDelegate()
app.delegate = obj
app.run()
Before app.run(), add
app.setActivationPolicy(.Regular)
According to the docs, the default activationPolicy is Prohibited:
Prohibited
The application does not appear in the Dock and may not create windows or be activated. [...] This is also the default for unbundled executables that do not have Info.plists.