Skip to content

Commit

Permalink
Merge pull request #3 from ReDetection/master
Browse files Browse the repository at this point in the history
Basic AppleScript support
  • Loading branch information
Toxblh authored Apr 9, 2018
2 parents 2dc1148 + d48d590 commit 74d57cd
Show file tree
Hide file tree
Showing 5 changed files with 115 additions and 93 deletions.
8 changes: 8 additions & 0 deletions MTMR.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
objects = {

/* 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 */; };
B059D624205E04F3006E6B86 /* TouchBarItems.swift in Sources */ = {isa = PBXBuildFile; fileRef = B059D623205E04F3006E6B86 /* TouchBarItems.swift */; };
B059D62D205F11E8006E6B86 /* DFRFoundation.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = B059D62C205F11E8006E6B86 /* DFRFoundation.framework */; };
Expand Down Expand Up @@ -41,6 +43,8 @@

/* Begin PBXFileReference section */
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>"; };
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>"; };
Expand Down Expand Up @@ -132,6 +136,8 @@
B082B256205C7D8000BC04DC /* Assets.xcassets */,
B0A7E9A9205D6AA400EEF070 /* KeyPress.swift */,
B059D623205E04F3006E6B86 /* TouchBarItems.swift */,
36C2ECD8207B74B4003CDA33 /* AppleScriptTouchBarItem.swift */,
36C2ECD6207B6DAE003CDA33 /* TimeTouchBarItem.swift */,
B059D621205E03F5006E6B86 /* TouchBarController.swift */,
B0C1CFC9205C97D30021C862 /* WindowController.swift */,
B082B258205C7D8000BC04DC /* Main.storyboard */,
Expand Down Expand Up @@ -302,12 +308,14 @@
buildActionMask = 2147483647;
files = (
B059D622205E03F5006E6B86 /* TouchBarController.swift in Sources */,
36C2ECD9207B74B4003CDA33 /* AppleScriptTouchBarItem.swift in Sources */,
B082B255205C7D8000BC04DC /* ViewController.swift in Sources */,
B0C1CFCA205C97D30021C862 /* WindowController.swift in Sources */,
B0F8771A207AC1EA00D6E430 /* TouchBarSupport.m in Sources */,
B082B253205C7D8000BC04DC /* AppDelegate.swift in Sources */,
B059D624205E04F3006E6B86 /* TouchBarItems.swift in Sources */,
B0A7E9AA205D6AA400EEF070 /* KeyPress.swift in Sources */,
36C2ECD7207B6DAE003CDA33 /* TimeTouchBarItem.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down
37 changes: 37 additions & 0 deletions MTMR/AppleScriptTouchBarItem.swift
Original file line number Diff line number Diff line change
@@ -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 changes: 25 additions & 0 deletions MTMR/TimeTouchBarItem.swift
Original file line number Diff line number Diff line change
@@ -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())
}

}
119 changes: 26 additions & 93 deletions MTMR/TouchBarController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,6 @@ class TouchBarController: NSObject, NSTouchBarDelegate {

let touchBar = NSTouchBar()

var timer = Timer()
var timeButton: NSButton = NSButton()

private override init() {
super.init()
touchBar.delegate = self
Expand All @@ -27,6 +24,8 @@ class TouchBarController: NSObject, NSTouchBarDelegate {
.brightDown,
.brightUp,

.flexibleSpace,

.prev,
.play,
.next,
Expand All @@ -48,7 +47,6 @@ class TouchBarController: NSObject, NSTouchBarDelegate {
item.view = NSButton(image: #imageLiteral(resourceName: "Strip"), target: self, action: #selector(presentTouchBar))
NSTouchBarItem.addSystemTrayItem(item)
DFRElementSetControlStripPresenceForIdentifier(.controlStripItem, true)
timer = Timer.scheduledTimer(timeInterval: 1, target: self, selector: #selector(self.updateTime), userInfo: nil, repeats: true)
}

func updateControlStripPresence() {
Expand All @@ -66,117 +64,52 @@ class TouchBarController: NSObject, NSTouchBarDelegate {
func touchBar(_ touchBar: NSTouchBar, makeItemForIdentifier identifier: NSTouchBarItem.Identifier) -> NSTouchBarItem? {
switch identifier {
case .escButton:
let item = NSCustomTouchBarItem(identifier: identifier)
item.view = NSButton(title: "esc", target: self, action: #selector(handleEsc))
return item
return CustomButtonTouchBarItem(identifier: identifier, title: "esc", key: ESCKeyPress())
case .dismissButton:
let item = NSCustomTouchBarItem(identifier: identifier)
item.view = NSButton(title: "exit", target: self, action: #selector(dismissTouchBar))
return item

case .brightUp:
let item = NSCustomTouchBarItem(identifier: identifier)
item.view = NSButton(title: "🔆", target: self, action: #selector(handleBrightUp))
return item
return CustomButtonTouchBarItem(identifier: identifier, title: "🔆", key: BrightnessUpPress())
case .brightDown:
let item = NSCustomTouchBarItem(identifier: identifier)
item.view = NSButton(title: "🔅", target: self, action: #selector(handleBrightDown))
return item
return CustomButtonTouchBarItem(identifier: identifier, title: "🔅", key: BrightnessDownPress())

case .volumeDown:
let item = NSCustomTouchBarItem(identifier: identifier)
item.view = NSButton(title: "🔉", target: self, action: #selector(handleVolumeDown))
return item
return CustomButtonTouchBarItem(identifier: identifier, title: "🔉", HIDKeycode: NX_KEYTYPE_SOUND_DOWN)
case .volumeUp:
let item = NSCustomTouchBarItem(identifier: identifier)
item.view = NSButton(title: "🔊", target: self, action: #selector(handleVolumeUp))
return item
return CustomButtonTouchBarItem(identifier: identifier, title: "🔊", HIDKeycode: NX_KEYTYPE_SOUND_UP)

case .prev:
let item = NSCustomTouchBarItem(identifier: identifier)
item.view = NSButton(title: "", target: self, action: #selector(handlePrev))
return item
return CustomButtonTouchBarItem(identifier: identifier, title: "", HIDKeycode: NX_KEYTYPE_PREVIOUS)
case .play:
let item = NSCustomTouchBarItem(identifier: identifier)
item.view = NSButton(title: "", target: self, action: #selector(handlePlay))
return item
return CustomButtonTouchBarItem(identifier: identifier, title: "", HIDKeycode: NX_KEYTYPE_PLAY)
case .next:
let item = NSCustomTouchBarItem(identifier: identifier)
item.view = NSButton(title: "", target: self, action: #selector(handleNext))
return item
return CustomButtonTouchBarItem(identifier: identifier, title: "", HIDKeycode: NX_KEYTYPE_NEXT)

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:
let item = NSCustomTouchBarItem(identifier: identifier)
timeButton = NSButton(title: self.getCurrentTime(), target: self, action: nil)
item.view = timeButton
return item
return TimeTouchBarItem(identifier: identifier, formatTemplate: "HH:mm")

default:
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))
}

extension CustomButtonTouchBarItem {
convenience init(identifier: NSTouchBarItem.Identifier, title: String, HIDKeycode: Int) {
self.init(identifier: identifier, title: title) { _ in
HIDPostAuxKey(HIDKeycode)
}
}

@objc func handleNext() {
HIDPostAuxKey(Int(NX_KEYTYPE_NEXT))
convenience init(identifier: NSTouchBarItem.Identifier, title: String, key: KeyPress) {
self.init(identifier: identifier, title: title) { _ in
key.send()
}
}

// 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)")
// }
// }
// }

}
19 changes: 19 additions & 0 deletions MTMR/TouchBarItems.swift
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,22 @@ extension NSTouchBarItem.Identifier {

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)
}
}

0 comments on commit 74d57cd

Please sign in to comment.