Created
July 1, 2015 02:43
-
-
Save thomasfinch/4b7166a81e56facf919d to your computer and use it in GitHub Desktop.
Day progress mac app
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
#!/usr/bin/xcrun swift | |
import Foundation | |
import Cocoa | |
import AppKit | |
class AppDelegate: NSObject, NSApplicationDelegate, NSMenuDelegate { | |
let statusItem: NSStatusItem = NSStatusBar.systemStatusBar().statusItemWithLength(100) | |
let progressIndicator = NSProgressIndicator() | |
let progressMenuItem = NSMenuItem(title:"", action:"", keyEquivalent:"") | |
func applicationDidFinishLaunching(aNotification: NSNotification) { | |
let progressIndicatorMenuItem = NSMenuItem() | |
progressIndicatorMenuItem.view = NSView(frame:CGRectMake(0, 0, 200, 20)) | |
progressIndicator.frame = progressIndicatorMenuItem.view!.bounds | |
progressIndicator.indeterminate = false | |
progressIndicatorMenuItem.view!.addSubview(progressIndicator) | |
let menu = NSMenu() | |
menu.delegate = self | |
menu.addItem(progressIndicatorMenuItem) | |
menu.addItem(progressMenuItem) | |
menu.addItem(NSMenuItem.separatorItem()) | |
menu.addItemWithTitle("Quit", action:"terminate:", keyEquivalent:"") | |
statusItem.title = "Day Progress" | |
statusItem.highlightMode = true | |
statusItem.menu = menu | |
} | |
func menuWillOpen(menu: NSMenu) { | |
let progressPercent = Double(currentDayProgress() * 100) | |
progressIndicator.doubleValue = progressPercent | |
progressMenuItem.title = String(format: "Progress: %.2f%@", progressPercent, "%") | |
} | |
func currentDayProgress() -> Float { | |
let curComponents = NSCalendar.currentCalendar().components(NSCalendarUnit(UInt.max), fromDate:NSDate()) | |
let curProgressMinutes : Float = (Float(curComponents.hour) - 9) * 60 + Float(curComponents.minute) | |
let progress : Float = curProgressMinutes / (8 * 60) | |
return progress | |
} | |
} | |
NSApplication.sharedApplication() | |
let delegate = AppDelegate() | |
NSApplication.sharedApplication().delegate = delegate | |
NSApp.run() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment