From ea5fbf0ee867dc05ab87f8771d63d81083217db8 Mon Sep 17 00:00:00 2001 From: Serg Date: Thu, 12 Apr 2018 08:48:24 +0700 Subject: [PATCH] action has unified source too --- MTMR/AppleScriptTouchBarItem.swift | 24 +++++++++++------------- MTMR/ItemsParsing.swift | 4 ++-- MTMR/TouchBarController.swift | 4 ++-- MTMR/TouchBarItems.swift | 19 +++++-------------- 4 files changed, 20 insertions(+), 31 deletions(-) diff --git a/MTMR/AppleScriptTouchBarItem.swift b/MTMR/AppleScriptTouchBarItem.swift index b5eed27..9c01521 100644 --- a/MTMR/AppleScriptTouchBarItem.swift +++ b/MTMR/AppleScriptTouchBarItem.swift @@ -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) + } +} diff --git a/MTMR/ItemsParsing.swift b/MTMR/ItemsParsing.swift index c6b59ce..bb34b72 100644 --- a/MTMR/ItemsParsing.swift +++ b/MTMR/ItemsParsing.swift @@ -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) diff --git a/MTMR/TouchBarController.swift b/MTMR/TouchBarController.swift index e2bcb5f..dba4dd0 100644 --- a/MTMR/TouchBarController.swift +++ b/MTMR/TouchBarController.swift @@ -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 {} } diff --git a/MTMR/TouchBarItems.swift b/MTMR/TouchBarItems.swift index 89b41c1..1297d43 100644 --- a/MTMR/TouchBarItems.swift +++ b/MTMR/TouchBarItems.swift @@ -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")