Hello,
thank you very much for your answer.
My problem in this point is, that I'm using swift and never learnd something about objective-c (big mistake).
import Cocoa
@NSApplicationMain
class AppDelegate: NSObject, NSApplicationDelegate {
@IBOutlet weak var window: NSWindow!
@IBOutlet weak var dorTime: NSMenu!
var timer = NSTimer()
let statusItem = NSStatusBar.systemStatusBar().statusItemWithLength(-1)
func applicationDidFinishLaunching(aNotification: NSNotification) {
let icon = NSImage(named: "dorunicorn")!
icon.template = true
statusItem.image = icon
statusItem.menu = dorTime
statusItem.length = NSVariableStatusItemLength
timer = NSTimer.scheduledTimerWithTimeInterval(0.1, target: self, selector: ("getDorTime"), userInfo: nil, repeats: true)
}
@IBAction func menuClicked(sender: NSMenuItem) {
let pasteBoard = NSPasteboard.generalPasteboard()
pasteBoard.clearContents()
pasteBoard.writeObjects(NSArray(object: statusItem.title!) as! [NSPasteboardWriting])
}
@IBAction func quitClicked(sender: NSMenuItem) {
exit(0)
}
func getDorTime() {
let userCalendar = NSCalendar.currentCalendar()
let napTimeComp = NSDateComponents()
napTimeComp.timeZone = userCalendar.timeZone
napTimeComp.year = 1821
napTimeComp.month = 5
napTimeComp.day = 6
napTimeComp.hour = 3
napTimeComp.minute = 14
napTimeComp.second = 15
let napTime = userCalendar.dateFromComponents(napTimeComp)!
let dorTime = Int(NSDate().timeIntervalSinceDate(napTime))
statusItem.title = String(dorTime.addSpaceSeparator)
}
extension Int {
var addSpaceSeparator:String {
let nf = NSNumberFormatter()
nf.groupingSeparator = "."
nf.numberStyle = NSNumberFormatterStyle.DecimalStyle
return nf.stringFromNumber(self)!
}
}
}
That is my code, that I'm using.
I want to show the number of seconds from one date (for my example on 6.may 1821, 3:14:15) to today.
As you can see in line 45, in this line, I will bringt the number to the status bar.
I have seen, that I can use statusItem.attributedTitle instead of statusItem.title.
But I don't know how to use it.
Would you please explain me the usage of this on my code? I want to learn it. 🙂
This would be very nice.
Best Regards
Lukas