mirror of
https://github.com/Toxblh/MTMR.git
synced 2026-01-11 01:18:39 +00:00
* Updated README Added explanation for missing parameters (background, title and image) * Implemented changable icons for AppleScriptTouchBarItem AppleScriptTouchBarItem now allow to specify any number of icons which can be changed from the script. You cannot change icon from touch event. To change icon, you need to return array from your script with 2 values - title and icn name. More info in readme * Implemented custom swipe actions Co-authored-by: Fedor Zaitsev <lobster@Fedors-MacBook-Pro.local>
67 lines
2.1 KiB
Swift
67 lines
2.1 KiB
Swift
//
|
|
// SwipeItem.swift
|
|
// MTMR
|
|
//
|
|
// Created by Fedor Zaitsev on 3/29/20.
|
|
// Copyright © 2020 Anton Palgunov. All rights reserved.
|
|
//
|
|
|
|
import Foundation
|
|
import Foundation
|
|
|
|
class SwipeItem: NSCustomTouchBarItem {
|
|
private var scriptApple: NSAppleScript?
|
|
private var scriptBash: String?
|
|
private var direction: String
|
|
private var fingers: Int
|
|
private var minOffset: Float
|
|
init?(identifier: NSTouchBarItem.Identifier, direction: String, fingers: Int, minOffset: Float, sourceApple: SourceProtocol?, sourceBash: SourceProtocol?) {
|
|
self.direction = direction
|
|
self.fingers = fingers
|
|
self.scriptBash = sourceBash?.string
|
|
self.scriptApple = sourceApple?.appleScript
|
|
self.minOffset = minOffset
|
|
super.init(identifier: identifier)
|
|
}
|
|
|
|
required init?(coder _: NSCoder) {
|
|
fatalError("init(coder:) has not been implemented")
|
|
}
|
|
|
|
func processEvent(offset: CGFloat, fingers: Int) {
|
|
if direction == "right" && Float(offset) > self.minOffset && self.fingers == fingers {
|
|
self.execute()
|
|
}
|
|
if direction == "left" && Float(offset) < -self.minOffset && self.fingers == fingers {
|
|
self.execute()
|
|
}
|
|
}
|
|
|
|
func execute() {
|
|
if scriptApple != nil {
|
|
DispatchQueue.appleScriptQueue.async {
|
|
var error: NSDictionary?
|
|
self.scriptApple?.executeAndReturnError(&error)
|
|
if let error = error {
|
|
print("SwipeItem apple script error: \(error)")
|
|
return
|
|
}
|
|
}
|
|
}
|
|
if scriptBash != nil {
|
|
DispatchQueue.shellScriptQueue.async {
|
|
let task = Process()
|
|
task.launchPath = "/bin/bash"
|
|
task.arguments = ["-c", self.scriptBash!]
|
|
task.launch()
|
|
task.waitUntilExit()
|
|
|
|
|
|
if (task.terminationStatus != 0) {
|
|
print("SwipeItem bash script error. Status: \(task.terminationStatus)")
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|