mirror of
https://github.com/Toxblh/MTMR.git
synced 2026-01-10 17:08:39 +00:00
Merge pull request #3 from ReDetection/master
Basic AppleScript support
This commit is contained in:
commit
74d57cdc7d
@ -7,6 +7,8 @@
|
|||||||
objects = {
|
objects = {
|
||||||
|
|
||||||
/* Begin PBXBuildFile section */
|
/* Begin PBXBuildFile section */
|
||||||
|
36C2ECD7207B6DAE003CDA33 /* TimeTouchBarItem.swift in Sources */ = {isa = PBXBuildFile; fileRef = 36C2ECD6207B6DAE003CDA33 /* TimeTouchBarItem.swift */; };
|
||||||
|
36C2ECD9207B74B4003CDA33 /* AppleScriptTouchBarItem.swift in Sources */ = {isa = PBXBuildFile; fileRef = 36C2ECD8207B74B4003CDA33 /* AppleScriptTouchBarItem.swift */; };
|
||||||
B059D622205E03F5006E6B86 /* TouchBarController.swift in Sources */ = {isa = PBXBuildFile; fileRef = B059D621205E03F5006E6B86 /* TouchBarController.swift */; };
|
B059D622205E03F5006E6B86 /* TouchBarController.swift in Sources */ = {isa = PBXBuildFile; fileRef = B059D621205E03F5006E6B86 /* TouchBarController.swift */; };
|
||||||
B059D624205E04F3006E6B86 /* TouchBarItems.swift in Sources */ = {isa = PBXBuildFile; fileRef = B059D623205E04F3006E6B86 /* TouchBarItems.swift */; };
|
B059D624205E04F3006E6B86 /* TouchBarItems.swift in Sources */ = {isa = PBXBuildFile; fileRef = B059D623205E04F3006E6B86 /* TouchBarItems.swift */; };
|
||||||
B059D62D205F11E8006E6B86 /* DFRFoundation.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = B059D62C205F11E8006E6B86 /* DFRFoundation.framework */; };
|
B059D62D205F11E8006E6B86 /* DFRFoundation.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = B059D62C205F11E8006E6B86 /* DFRFoundation.framework */; };
|
||||||
@ -41,6 +43,8 @@
|
|||||||
|
|
||||||
/* Begin PBXFileReference section */
|
/* Begin PBXFileReference section */
|
||||||
36C2ECD2207B3B1D003CDA33 /* README.md */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = net.daringfireball.markdown; path = README.md; sourceTree = "<group>"; };
|
36C2ECD2207B3B1D003CDA33 /* README.md */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = net.daringfireball.markdown; path = README.md; sourceTree = "<group>"; };
|
||||||
|
36C2ECD6207B6DAE003CDA33 /* TimeTouchBarItem.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TimeTouchBarItem.swift; sourceTree = "<group>"; };
|
||||||
|
36C2ECD8207B74B4003CDA33 /* AppleScriptTouchBarItem.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppleScriptTouchBarItem.swift; sourceTree = "<group>"; };
|
||||||
B059D621205E03F5006E6B86 /* TouchBarController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TouchBarController.swift; sourceTree = "<group>"; };
|
B059D621205E03F5006E6B86 /* TouchBarController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TouchBarController.swift; sourceTree = "<group>"; };
|
||||||
B059D623205E04F3006E6B86 /* TouchBarItems.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TouchBarItems.swift; sourceTree = "<group>"; };
|
B059D623205E04F3006E6B86 /* TouchBarItems.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TouchBarItems.swift; sourceTree = "<group>"; };
|
||||||
B059D629205E13E5006E6B86 /* TouchBarPrivateApi.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = TouchBarPrivateApi.h; sourceTree = "<group>"; };
|
B059D629205E13E5006E6B86 /* TouchBarPrivateApi.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = TouchBarPrivateApi.h; sourceTree = "<group>"; };
|
||||||
@ -132,6 +136,8 @@
|
|||||||
B082B256205C7D8000BC04DC /* Assets.xcassets */,
|
B082B256205C7D8000BC04DC /* Assets.xcassets */,
|
||||||
B0A7E9A9205D6AA400EEF070 /* KeyPress.swift */,
|
B0A7E9A9205D6AA400EEF070 /* KeyPress.swift */,
|
||||||
B059D623205E04F3006E6B86 /* TouchBarItems.swift */,
|
B059D623205E04F3006E6B86 /* TouchBarItems.swift */,
|
||||||
|
36C2ECD8207B74B4003CDA33 /* AppleScriptTouchBarItem.swift */,
|
||||||
|
36C2ECD6207B6DAE003CDA33 /* TimeTouchBarItem.swift */,
|
||||||
B059D621205E03F5006E6B86 /* TouchBarController.swift */,
|
B059D621205E03F5006E6B86 /* TouchBarController.swift */,
|
||||||
B0C1CFC9205C97D30021C862 /* WindowController.swift */,
|
B0C1CFC9205C97D30021C862 /* WindowController.swift */,
|
||||||
B082B258205C7D8000BC04DC /* Main.storyboard */,
|
B082B258205C7D8000BC04DC /* Main.storyboard */,
|
||||||
@ -302,12 +308,14 @@
|
|||||||
buildActionMask = 2147483647;
|
buildActionMask = 2147483647;
|
||||||
files = (
|
files = (
|
||||||
B059D622205E03F5006E6B86 /* TouchBarController.swift in Sources */,
|
B059D622205E03F5006E6B86 /* TouchBarController.swift in Sources */,
|
||||||
|
36C2ECD9207B74B4003CDA33 /* AppleScriptTouchBarItem.swift in Sources */,
|
||||||
B082B255205C7D8000BC04DC /* ViewController.swift in Sources */,
|
B082B255205C7D8000BC04DC /* ViewController.swift in Sources */,
|
||||||
B0C1CFCA205C97D30021C862 /* WindowController.swift in Sources */,
|
B0C1CFCA205C97D30021C862 /* WindowController.swift in Sources */,
|
||||||
B0F8771A207AC1EA00D6E430 /* TouchBarSupport.m in Sources */,
|
B0F8771A207AC1EA00D6E430 /* TouchBarSupport.m in Sources */,
|
||||||
B082B253205C7D8000BC04DC /* AppDelegate.swift in Sources */,
|
B082B253205C7D8000BC04DC /* AppDelegate.swift in Sources */,
|
||||||
B059D624205E04F3006E6B86 /* TouchBarItems.swift in Sources */,
|
B059D624205E04F3006E6B86 /* TouchBarItems.swift in Sources */,
|
||||||
B0A7E9AA205D6AA400EEF070 /* KeyPress.swift in Sources */,
|
B0A7E9AA205D6AA400EEF070 /* KeyPress.swift in Sources */,
|
||||||
|
36C2ECD7207B6DAE003CDA33 /* TimeTouchBarItem.swift in Sources */,
|
||||||
);
|
);
|
||||||
runOnlyForDeploymentPostprocessing = 0;
|
runOnlyForDeploymentPostprocessing = 0;
|
||||||
};
|
};
|
||||||
|
|||||||
37
MTMR/AppleScriptTouchBarItem.swift
Normal file
37
MTMR/AppleScriptTouchBarItem.swift
Normal file
@ -0,0 +1,37 @@
|
|||||||
|
import Foundation
|
||||||
|
|
||||||
|
class AppleScriptTouchBarItem: NSCustomTouchBarItem {
|
||||||
|
let script: NSAppleScript
|
||||||
|
private var timer: Timer!
|
||||||
|
private let button = NSButton(title: "", target: nil, action: nil)
|
||||||
|
|
||||||
|
init?(identifier: NSTouchBarItem.Identifier, appleScript: String, interval: TimeInterval) {
|
||||||
|
guard let script = NSAppleScript(source: appleScript) else {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
self.script = script
|
||||||
|
super.init(identifier: identifier)
|
||||||
|
timer = Timer.scheduledTimer(timeInterval: interval, target: self, selector: #selector(refresh), userInfo: nil, repeats: true)
|
||||||
|
self.view = button
|
||||||
|
button.bezelColor = .clear
|
||||||
|
refresh()
|
||||||
|
}
|
||||||
|
|
||||||
|
required init?(coder: NSCoder) {
|
||||||
|
fatalError("init(coder:) has not been implemented")
|
||||||
|
}
|
||||||
|
|
||||||
|
@objc func refresh() {
|
||||||
|
self.button.title = self.execute()
|
||||||
|
}
|
||||||
|
|
||||||
|
func execute() -> String {
|
||||||
|
var error: NSDictionary?
|
||||||
|
let output = script.executeAndReturnError(&error)
|
||||||
|
if let error = error {
|
||||||
|
print(error)
|
||||||
|
return "error"
|
||||||
|
}
|
||||||
|
return output.stringValue ?? "empty value"
|
||||||
|
}
|
||||||
|
}
|
||||||
25
MTMR/TimeTouchBarItem.swift
Normal file
25
MTMR/TimeTouchBarItem.swift
Normal file
@ -0,0 +1,25 @@
|
|||||||
|
import Cocoa
|
||||||
|
|
||||||
|
class TimeTouchBarItem: NSCustomTouchBarItem {
|
||||||
|
private let dateFormatter = DateFormatter()
|
||||||
|
private var timer: Timer!
|
||||||
|
private let button = NSButton(title: "", target: nil, action: nil)
|
||||||
|
|
||||||
|
init(identifier: NSTouchBarItem.Identifier, formatTemplate: String) {
|
||||||
|
dateFormatter.setLocalizedDateFormatFromTemplate(formatTemplate)
|
||||||
|
super.init(identifier: identifier)
|
||||||
|
timer = Timer.scheduledTimer(timeInterval: 1, target: self, selector: #selector(updateTime), userInfo: nil, repeats: true)
|
||||||
|
self.view = button
|
||||||
|
button.bezelColor = .clear
|
||||||
|
updateTime()
|
||||||
|
}
|
||||||
|
|
||||||
|
required init?(coder: NSCoder) {
|
||||||
|
fatalError("init(coder:) has not been implemented")
|
||||||
|
}
|
||||||
|
|
||||||
|
@objc func updateTime() {
|
||||||
|
button.title = self.dateFormatter.string(from: Date())
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@ -14,9 +14,6 @@ class TouchBarController: NSObject, NSTouchBarDelegate {
|
|||||||
|
|
||||||
let touchBar = NSTouchBar()
|
let touchBar = NSTouchBar()
|
||||||
|
|
||||||
var timer = Timer()
|
|
||||||
var timeButton: NSButton = NSButton()
|
|
||||||
|
|
||||||
private override init() {
|
private override init() {
|
||||||
super.init()
|
super.init()
|
||||||
touchBar.delegate = self
|
touchBar.delegate = self
|
||||||
@ -27,6 +24,8 @@ class TouchBarController: NSObject, NSTouchBarDelegate {
|
|||||||
.brightDown,
|
.brightDown,
|
||||||
.brightUp,
|
.brightUp,
|
||||||
|
|
||||||
|
.flexibleSpace,
|
||||||
|
|
||||||
.prev,
|
.prev,
|
||||||
.play,
|
.play,
|
||||||
.next,
|
.next,
|
||||||
@ -48,7 +47,6 @@ class TouchBarController: NSObject, NSTouchBarDelegate {
|
|||||||
item.view = NSButton(image: #imageLiteral(resourceName: "Strip"), target: self, action: #selector(presentTouchBar))
|
item.view = NSButton(image: #imageLiteral(resourceName: "Strip"), target: self, action: #selector(presentTouchBar))
|
||||||
NSTouchBarItem.addSystemTrayItem(item)
|
NSTouchBarItem.addSystemTrayItem(item)
|
||||||
DFRElementSetControlStripPresenceForIdentifier(.controlStripItem, true)
|
DFRElementSetControlStripPresenceForIdentifier(.controlStripItem, true)
|
||||||
timer = Timer.scheduledTimer(timeInterval: 1, target: self, selector: #selector(self.updateTime), userInfo: nil, repeats: true)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func updateControlStripPresence() {
|
func updateControlStripPresence() {
|
||||||
@ -66,117 +64,52 @@ class TouchBarController: NSObject, NSTouchBarDelegate {
|
|||||||
func touchBar(_ touchBar: NSTouchBar, makeItemForIdentifier identifier: NSTouchBarItem.Identifier) -> NSTouchBarItem? {
|
func touchBar(_ touchBar: NSTouchBar, makeItemForIdentifier identifier: NSTouchBarItem.Identifier) -> NSTouchBarItem? {
|
||||||
switch identifier {
|
switch identifier {
|
||||||
case .escButton:
|
case .escButton:
|
||||||
let item = NSCustomTouchBarItem(identifier: identifier)
|
return CustomButtonTouchBarItem(identifier: identifier, title: "esc", key: ESCKeyPress())
|
||||||
item.view = NSButton(title: "esc", target: self, action: #selector(handleEsc))
|
|
||||||
return item
|
|
||||||
case .dismissButton:
|
case .dismissButton:
|
||||||
let item = NSCustomTouchBarItem(identifier: identifier)
|
let item = NSCustomTouchBarItem(identifier: identifier)
|
||||||
item.view = NSButton(title: "exit", target: self, action: #selector(dismissTouchBar))
|
item.view = NSButton(title: "exit", target: self, action: #selector(dismissTouchBar))
|
||||||
return item
|
return item
|
||||||
|
|
||||||
case .brightUp:
|
case .brightUp:
|
||||||
let item = NSCustomTouchBarItem(identifier: identifier)
|
return CustomButtonTouchBarItem(identifier: identifier, title: "🔆", key: BrightnessUpPress())
|
||||||
item.view = NSButton(title: "🔆", target: self, action: #selector(handleBrightUp))
|
|
||||||
return item
|
|
||||||
case .brightDown:
|
case .brightDown:
|
||||||
let item = NSCustomTouchBarItem(identifier: identifier)
|
return CustomButtonTouchBarItem(identifier: identifier, title: "🔅", key: BrightnessDownPress())
|
||||||
item.view = NSButton(title: "🔅", target: self, action: #selector(handleBrightDown))
|
|
||||||
return item
|
|
||||||
|
|
||||||
case .volumeDown:
|
case .volumeDown:
|
||||||
let item = NSCustomTouchBarItem(identifier: identifier)
|
return CustomButtonTouchBarItem(identifier: identifier, title: "🔉", HIDKeycode: NX_KEYTYPE_SOUND_DOWN)
|
||||||
item.view = NSButton(title: "🔉", target: self, action: #selector(handleVolumeDown))
|
|
||||||
return item
|
|
||||||
case .volumeUp:
|
case .volumeUp:
|
||||||
let item = NSCustomTouchBarItem(identifier: identifier)
|
return CustomButtonTouchBarItem(identifier: identifier, title: "🔊", HIDKeycode: NX_KEYTYPE_SOUND_UP)
|
||||||
item.view = NSButton(title: "🔊", target: self, action: #selector(handleVolumeUp))
|
|
||||||
return item
|
|
||||||
|
|
||||||
case .prev:
|
case .prev:
|
||||||
let item = NSCustomTouchBarItem(identifier: identifier)
|
return CustomButtonTouchBarItem(identifier: identifier, title: "⏪", HIDKeycode: NX_KEYTYPE_PREVIOUS)
|
||||||
item.view = NSButton(title: "⏪", target: self, action: #selector(handlePrev))
|
|
||||||
return item
|
|
||||||
case .play:
|
case .play:
|
||||||
let item = NSCustomTouchBarItem(identifier: identifier)
|
return CustomButtonTouchBarItem(identifier: identifier, title: "⏯", HIDKeycode: NX_KEYTYPE_PLAY)
|
||||||
item.view = NSButton(title: "⏯", target: self, action: #selector(handlePlay))
|
|
||||||
return item
|
|
||||||
case .next:
|
case .next:
|
||||||
let item = NSCustomTouchBarItem(identifier: identifier)
|
return CustomButtonTouchBarItem(identifier: identifier, title: "⏩", HIDKeycode: NX_KEYTYPE_NEXT)
|
||||||
item.view = NSButton(title: "⏩", target: self, action: #selector(handleNext))
|
|
||||||
return item
|
|
||||||
|
|
||||||
|
case .battery:
|
||||||
|
let url = Bundle.main.url(forResource: "battery", withExtension: "scpt")!
|
||||||
|
let script = try! String.init(contentsOf: url)
|
||||||
|
return AppleScriptTouchBarItem(identifier: identifier, appleScript: script, interval: 60)
|
||||||
case .time:
|
case .time:
|
||||||
let item = NSCustomTouchBarItem(identifier: identifier)
|
return TimeTouchBarItem(identifier: identifier, formatTemplate: "HH:mm")
|
||||||
timeButton = NSButton(title: self.getCurrentTime(), target: self, action: nil)
|
|
||||||
item.view = timeButton
|
|
||||||
return item
|
|
||||||
|
|
||||||
default:
|
default:
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func getCurrentTime() -> String {
|
|
||||||
let date = Date()
|
|
||||||
let dateFormatter = DateFormatter()
|
|
||||||
dateFormatter.setLocalizedDateFormatFromTemplate("HH:mm")
|
|
||||||
let timestamp = dateFormatter.string(from: date)
|
|
||||||
return timestamp
|
|
||||||
}
|
|
||||||
|
|
||||||
@objc func updateTime() {
|
|
||||||
timeButton.title = getCurrentTime()
|
|
||||||
}
|
|
||||||
|
|
||||||
@objc func handleEsc() {
|
|
||||||
let sender = ESCKeyPress()
|
|
||||||
sender.send()
|
|
||||||
}
|
|
||||||
|
|
||||||
@objc func handleVolumeUp() {
|
|
||||||
HIDPostAuxKey(Int(NX_KEYTYPE_SOUND_UP))
|
|
||||||
}
|
|
||||||
|
|
||||||
@objc func handleVolumeDown() {
|
|
||||||
HIDPostAuxKey(Int(NX_KEYTYPE_SOUND_DOWN))
|
|
||||||
}
|
|
||||||
|
|
||||||
@objc func handleBrightDown() {
|
|
||||||
// HIDPostAuxKey(Int(NX_KEYTYPE_BRIGHTNESS_DOWN))
|
|
||||||
|
|
||||||
let sender = BrightnessUpPress()
|
|
||||||
sender.send()
|
|
||||||
}
|
|
||||||
|
|
||||||
@objc func handleBrightUp() {
|
|
||||||
// HIDPostAuxKey(Int(NX_KEYTYPE_BRIGHTNESS_UP))
|
|
||||||
|
|
||||||
let sender = BrightnessDownPress()
|
|
||||||
sender.send()
|
|
||||||
}
|
|
||||||
|
|
||||||
@objc func handlePrev() {
|
|
||||||
HIDPostAuxKey(Int(NX_KEYTYPE_PREVIOUS))
|
|
||||||
}
|
|
||||||
|
|
||||||
@objc func handlePlay() {
|
|
||||||
HIDPostAuxKey(Int(NX_KEYTYPE_PLAY))
|
|
||||||
}
|
|
||||||
|
|
||||||
@objc func handleNext() {
|
|
||||||
HIDPostAuxKey(Int(NX_KEYTYPE_NEXT))
|
|
||||||
}
|
|
||||||
|
|
||||||
// func getBattery() {
|
|
||||||
// var error: NSDictionary?
|
|
||||||
// if let scriptObject = NSAppleScript(source: <#T##String#>) {
|
|
||||||
// if let output: NSAppleEventDescriptor = scriptObject.executeAndReturnError(
|
|
||||||
// &error) {
|
|
||||||
// print(output.stringValue)
|
|
||||||
// } else if (error != nil) {
|
|
||||||
// print("error: \(error)")
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
extension CustomButtonTouchBarItem {
|
||||||
|
convenience init(identifier: NSTouchBarItem.Identifier, title: String, HIDKeycode: Int) {
|
||||||
|
self.init(identifier: identifier, title: title) { _ in
|
||||||
|
HIDPostAuxKey(HIDKeycode)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
convenience init(identifier: NSTouchBarItem.Identifier, title: String, key: KeyPress) {
|
||||||
|
self.init(identifier: identifier, title: title) { _ in
|
||||||
|
key.send()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@ -34,3 +34,22 @@ extension NSTouchBarItem.Identifier {
|
|||||||
|
|
||||||
static let controlStripItem = NSTouchBarItem.Identifier("com.toxblh.mtmr.controlStrip")
|
static let controlStripItem = NSTouchBarItem.Identifier("com.toxblh.mtmr.controlStrip")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
class CustomButtonTouchBarItem: NSCustomTouchBarItem {
|
||||||
|
let tapClosure: (NSCustomTouchBarItem) -> ()
|
||||||
|
|
||||||
|
init(identifier: NSTouchBarItem.Identifier, title: String, onTap callback: @escaping (NSCustomTouchBarItem) -> ()) {
|
||||||
|
self.tapClosure = callback
|
||||||
|
super.init(identifier: identifier)
|
||||||
|
self.view = NSButton(title: title, target: self, action: #selector(didTapped))
|
||||||
|
}
|
||||||
|
|
||||||
|
required init?(coder: NSCoder) {
|
||||||
|
fatalError("init(coder:) has not been implemented")
|
||||||
|
}
|
||||||
|
|
||||||
|
@objc func didTapped() {
|
||||||
|
self.tapClosure(self)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user