osx NSRunLoop simple daemon application


A daemon process executes a program in the background, usually without user interaction. The example below shows how to create a daemon and register a listener, which monitors all open applications. The main part is the function call NSRunLoop.mainRunLoop().run(), which starts the daemon.

class MyObserver: NSObject
    override init() {

        // app listeners
        NSWorkspace.sharedWorkspace().notificationCenter.addObserver(self, selector: "SwitchedApp:", name: NSWorkspaceDidActivateApplicationNotification, object: nil)

    func SwitchedApp(notification: NSNotification!)

let observer = MyObserver()

// simply to keep the command line tool alive - as a daemon process

You can also use this code as a basis for a server process.