mirror of
https://github.com/Toxblh/MTMR.git
synced 2026-01-10 17:08:39 +00:00
commit
e10eed17ad
@ -1,8 +1,9 @@
|
|||||||
import Foundation
|
import Foundation
|
||||||
|
|
||||||
class AppleScriptTouchBarItem: NSCustomTouchBarItem {
|
class AppleScriptTouchBarItem: NSCustomTouchBarItem {
|
||||||
let script: NSAppleScript
|
private let script: NSAppleScript
|
||||||
private var timer: Timer!
|
private let queue = DispatchQueue(label: "apple script touchbar queue", qos: .utility, attributes: [], autoreleaseFrequency: .workItem, target: nil)
|
||||||
|
private let interval: TimeInterval
|
||||||
private let button = NSButton(title: "", target: nil, action: nil)
|
private let button = NSButton(title: "", target: nil, action: nil)
|
||||||
|
|
||||||
init?(identifier: NSTouchBarItem.Identifier, appleScript: String, interval: TimeInterval) {
|
init?(identifier: NSTouchBarItem.Identifier, appleScript: String, interval: TimeInterval) {
|
||||||
@ -10,19 +11,37 @@ class AppleScriptTouchBarItem: NSCustomTouchBarItem {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
self.script = script
|
self.script = script
|
||||||
|
self.interval = interval
|
||||||
super.init(identifier: identifier)
|
super.init(identifier: identifier)
|
||||||
timer = Timer.scheduledTimer(timeInterval: interval, target: self, selector: #selector(refresh), userInfo: nil, repeats: true)
|
|
||||||
self.view = button
|
self.view = button
|
||||||
button.bezelColor = .clear
|
button.bezelColor = .clear
|
||||||
refresh()
|
button.title = "compile"
|
||||||
|
queue.async {
|
||||||
|
var error: NSDictionary?
|
||||||
|
guard script.compileAndReturnError(&error) else {
|
||||||
|
print(error?.description ?? "unknown error")
|
||||||
|
DispatchQueue.main.async {
|
||||||
|
self.button.title = "compile error"
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
self.refreshAndSchedule()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
required init?(coder: NSCoder) {
|
required init?(coder: NSCoder) {
|
||||||
fatalError("init(coder:) has not been implemented")
|
fatalError("init(coder:) has not been implemented")
|
||||||
}
|
}
|
||||||
|
|
||||||
@objc func refresh() {
|
func refreshAndSchedule() {
|
||||||
self.button.title = self.execute()
|
print("refresh happened")
|
||||||
|
let scriptResult = self.execute()
|
||||||
|
DispatchQueue.main.async {
|
||||||
|
self.button.title = scriptResult
|
||||||
|
}
|
||||||
|
queue.asyncAfter(deadline: .now() + self.interval) { [weak self] in
|
||||||
|
self?.refreshAndSchedule()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func execute() -> String {
|
func execute() -> String {
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user