Created
October 31, 2016 18:27
-
-
Save hinzundcode/974593d7a245065cd95168263c863c08 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// 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