mirror of
https://github.com/Toxblh/MTMR.git
synced 2026-01-10 17:08:39 +00:00
action has unified source too
This commit is contained in:
parent
d0cfa45b5d
commit
ea5fbf0ee8
@ -1,27 +1,20 @@
|
||||
import Foundation
|
||||
|
||||
class AppleScriptTouchBarItem: NSCustomTouchBarItem {
|
||||
class AppleScriptTouchBarItem: CustomButtonTouchBarItem {
|
||||
private var script: NSAppleScript!
|
||||
private let button = NSButton(title: "", target: nil, action: nil)
|
||||
private let interval: TimeInterval
|
||||
private var forceHideConstraint: NSLayoutConstraint!
|
||||
|
||||
init?(identifier: NSTouchBarItem.Identifier, source: Source, interval: TimeInterval) {
|
||||
init?(identifier: NSTouchBarItem.Identifier, source: Source, interval: TimeInterval, onTap: @escaping ()->()) {
|
||||
self.interval = interval
|
||||
super.init(identifier: identifier)
|
||||
self.view = button
|
||||
super.init(identifier: identifier, title: "compile", onTap: onTap)
|
||||
self.forceHideConstraint = self.view.widthAnchor.constraint(equalToConstant: 0)
|
||||
guard let source = source.string else {
|
||||
guard let script = source.appleScript else {
|
||||
button.title = "no script"
|
||||
return
|
||||
}
|
||||
guard let script = NSAppleScript(source: source) else {
|
||||
button.title = "isn't a script"
|
||||
return
|
||||
}
|
||||
self.script = script
|
||||
button.bezelColor = .clear
|
||||
button.title = "compile"
|
||||
DispatchQueue.main.async {
|
||||
var error: NSDictionary?
|
||||
guard script.compileAndReturnError(&error) else {
|
||||
@ -51,8 +44,6 @@ class AppleScriptTouchBarItem: NSCustomTouchBarItem {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
func execute() -> String {
|
||||
var error: NSDictionary?
|
||||
let output = script.executeAndReturnError(&error)
|
||||
@ -64,3 +55,10 @@ class AppleScriptTouchBarItem: NSCustomTouchBarItem {
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
extension Source {
|
||||
var appleScript: NSAppleScript? {
|
||||
guard let source = self.string else { return nil }
|
||||
return NSAppleScript(source: source)
|
||||
}
|
||||
}
|
||||
|
||||
@ -145,7 +145,7 @@ enum ActionType: Decodable {
|
||||
case none
|
||||
case hidKey(keycode: Int)
|
||||
case keyPress(keycode: Int)
|
||||
case appleSctipt(source: String)
|
||||
case appleSctipt(source: Source)
|
||||
case shellScript(executable: String, parameters: [String])
|
||||
case custom(closure: ()->())
|
||||
|
||||
@ -175,7 +175,7 @@ enum ActionType: Decodable {
|
||||
let keycode = try container.decode(Int.self, forKey: .keycode)
|
||||
self = .keyPress(keycode: keycode)
|
||||
case .some(.appleScript):
|
||||
let source = try container.decode(String.self, forKey: .actionAppleScript)
|
||||
let source = try container.decode(Source.self, forKey: .actionAppleScript)
|
||||
self = .appleSctipt(source: source)
|
||||
case .some(.shellScript):
|
||||
let executable = try container.decode(String.self, forKey: .executablePath)
|
||||
|
||||
@ -105,7 +105,7 @@ class TouchBarController: NSObject, NSTouchBarDelegate {
|
||||
case .staticImageButton(title: let title, image: let image):
|
||||
barItem = CustomButtonTouchBarItem(identifier: identifier, title: title, onTap: action, image: image)
|
||||
case .appleScriptTitledButton(source: let source, refreshInterval: let interval):
|
||||
barItem = AppleScriptTouchBarItem(identifier: identifier, source: source, interval: interval)
|
||||
barItem = AppleScriptTouchBarItem(identifier: identifier, source: source, interval: interval, onTap: action)
|
||||
case .timeButton(formatTemplate: let template):
|
||||
barItem = TimeTouchBarItem(identifier: identifier, formatTemplate: template)
|
||||
case .flexSpace:
|
||||
@ -127,7 +127,7 @@ class TouchBarController: NSObject, NSTouchBarDelegate {
|
||||
case .keyPress(keycode: let keycode):
|
||||
return { GenericKeyPress(keyCode: CGKeyCode(keycode)).send() }
|
||||
case .appleSctipt(source: let source):
|
||||
guard let appleScript = NSAppleScript(source: source) else {
|
||||
guard let appleScript = source.appleScript else {
|
||||
print("cannot create apple script for item \(item)")
|
||||
return {}
|
||||
}
|
||||
|
||||
@ -14,28 +14,19 @@ extension NSTouchBarItem.Identifier {
|
||||
|
||||
class CustomButtonTouchBarItem: NSCustomTouchBarItem {
|
||||
let tapClosure: () -> ()
|
||||
private(set) var button: NSButton!
|
||||
|
||||
init(identifier: NSTouchBarItem.Identifier, title: String, onTap callback: @escaping () -> ()) {
|
||||
init(identifier: NSTouchBarItem.Identifier, title: String, onTap callback: @escaping () -> (), image: NSImage? = nil) {
|
||||
self.tapClosure = callback
|
||||
super.init(identifier: identifier)
|
||||
var button = NSButton(title: "", target: nil, action: nil)
|
||||
button = NSButton(title: title, target: self, action: #selector(didTapped))
|
||||
self.view = button
|
||||
}
|
||||
|
||||
init(identifier: NSTouchBarItem.Identifier, title: String, onTap callback: @escaping () -> (), image: NSImage?) {
|
||||
self.tapClosure = callback
|
||||
super.init(identifier: identifier)
|
||||
var button = NSButton(title: "", target: nil, action: nil)
|
||||
if ((image) != nil) {
|
||||
button = NSButton(title: title, image: image!, target: self, action: #selector(didTapped))
|
||||
if let image = image {
|
||||
button = NSButton(title: title, image: image, target: self, action: #selector(didTapped))
|
||||
button.bezelColor = .clear
|
||||
} else {
|
||||
button = NSButton(title: title, target: self, action: #selector(didTapped))
|
||||
}
|
||||
self.view = button
|
||||
button.bezelColor = .clear
|
||||
}
|
||||
|
||||
|
||||
required init?(coder: NSCoder) {
|
||||
fatalError("init(coder:) has not been implemented")
|
||||
|
||||
Loading…
Reference in New Issue
Block a user