Skip to content

Instantly share code, notes, and snippets.

@hinzundcode
Created October 31, 2016 18:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hinzundcode/974593d7a245065cd95168263c863c08 to your computer and use it in GitHub Desktop.
Save hinzundcode/974593d7a245065cd95168263c863c08 to your computer and use it in GitHub Desktop.
//
// AppDelegate.swift
// Test3
//
// Created by Chris Hinze on 28.05.15.
// Copyright (c) 2015 Chris Hinze. All rights reserved.
//
import Cocoa
@NSApplicationMain
class AppDelegate: NSObject, NSApplicationDelegate {
func applicationDidFinishLaunching(aNotification: NSNotification) {
for app in NSWorkspace.sharedWorkspace().runningApplications {
if (app.active) {
outputActiveApp(app)
}
}
NSWorkspace.sharedWorkspace().notificationCenter.addObserver(self, selector: #selector(AppDelegate.appChange(_:)), name: NSWorkspaceDidActivateApplicationNotification, object: nil)
//NSWorkspace.sharedWorkspace().notificationCenter.addObserver(self, selector: Selector("appDeactivate:"), name: NSWorkspaceDidDeactivateApplicationNotification, object: nil)
NSWorkspace.sharedWorkspace().notificationCenter.addObserver(self, selector: #selector(AppDelegate.receiveSleep(_:)), name: NSWorkspaceWillSleepNotification, object: nil)
NSWorkspace.sharedWorkspace().notificationCenter.addObserver(self, selector: #selector(AppDelegate.receiveWakeup(_:)), name: NSWorkspaceDidWakeNotification, object: nil)
}
func applicationWillTerminate(aNotification: NSNotification) {
//println("end")
}
func appChange(notification: NSNotification) {
let app : NSRunningApplication = notification.userInfo![NSWorkspaceApplicationKey] as! NSRunningApplication
outputActiveApp(app)
}
func receiveSleep(notification: NSNotification) {
// works on forced, test idle
outputJson([
"type": "sleep"
]);
}
func receiveWakeup(notification: NSNotification) {
outputJson([
"type": "wakeup"
]);
}
func outputActiveApp(app: NSRunningApplication) {
outputJson([
"type": "activeAppChange",
"bundleIdentifier": app.bundleIdentifier!,
"localizedName": app.localizedName!
]);
}
func outputJson(data: NSDictionary) {
let jsonData = try! NSJSONSerialization.dataWithJSONObject(data, options: NSJSONWritingOptions.PrettyPrinted)
let jsonString = NSString(data: jsonData, encoding: NSUTF8StringEncoding)! as String
print(jsonString)
fflush(__stdoutp)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment