Skip to content

Instantly share code, notes, and snippets.

@akosma
Created October 24, 2016 07:55
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save akosma/b64d77382ce6f6d4e0292d6e38cc3574 to your computer and use it in GitHub Desktop.
Save akosma/b64d77382ce6f6d4e0292d6e38cc3574 to your computer and use it in GitHub Desktop.
Small command-line macOS application using Swift
#!/usr/bin/env xcrun swift
// Adapted from
// https://forums.developer.apple.com/thread/5137
// https://joearms.github.io/2016/01/04/fun-with-swift.html
import WebKit
class ApplicationDelegate: NSObject, NSApplicationDelegate {
internal var window: NSWindow
init(window: NSWindow) {
self.window = window
}
func applicationDidFinishLaunching(_ notification: Notification) {
let webView = WebView(frame: self.window.contentView!.frame)
let search = "https://duckduckgo.com/?q=adrian+kosmaczewski"
let url = URL(string: search)!
let request = URLRequest(url: url)
self.window.contentView?.addSubview(webView)
webView.mainFrame.load(request)
}
}
class WindowDelegate: NSObject, NSWindowDelegate {
func windowWillClose(_ notification: Notification) {
NSApplication.shared().terminate(0)
}
}
let windowDelegate = WindowDelegate()
let window = NSWindow(contentRect: NSMakeRect(0, 0, 800, 600),
styleMask: [.titled, .closable, .miniaturizable],
backing: .buffered,
defer: false)
window.center()
window.title = "Adrian Kosmaczewski"
window.delegate = windowDelegate
window.makeKeyAndOrderFront(window)
let applicationDelegate = ApplicationDelegate(window: window)
let application = NSApplication.shared()
application.setActivationPolicy(.regular)
application.delegate = applicationDelegate
application.activate(ignoringOtherApps: true)
application.run()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment