I'm attempting to learn Swift in my spare time and have embarked on a mini-project to try and learn with purpose. I'm trying to create a MacOS app that shows the first 10 characters of the clipboard on the menu bar.
I've had some success, I've managed to get the app to show the clipboard content, however it works only once; if the clipboard content changes it doesn't update.
I've discovered that using ChangeCount I can keep track of the clipboard, however I'm now stuck on how to structure the code to make the menu bar update.
Here's the (relevant) code so far:
timer = Timer.scheduledTimer(withTimeInterval: 0.05, repeats: true) { (t) in
if self.lastChangeCount != self.pasteboard.changeCount {
}
}
var clipboardItems: [String] = []
for element in pasteboard.pasteboardItems! {
if let str = element.string(forType: NSPasteboard.PasteboardType(rawValue: "public.utf8-plain-text")) {
clipboardItems.append(str)
}
}
let ClipboardItem = String(clipboardItems[0].prefix(10)) // Gets 10 chars of first item
let statusBar = NSStatusBar.system
statusBarItem = statusBar.statusItem(
withLength: NSStatusItem.variableLength)
statusBarItem.button?.title = ClipboardItem
Any help would be much appreciated.