1
0
mirror of https://github.com/Toxblh/MTMR.git synced 2026-01-10 17:08:39 +00:00

shell scripts and sleep implemented

This commit is contained in:
Serg 2018-04-10 23:37:20 +07:00
parent 1584cf979c
commit 0ba5984217
3 changed files with 20 additions and 7 deletions

View File

@ -62,6 +62,7 @@ class SupportedTypesHolder {
let item = ItemType.appleScriptTitledButton(source: try! String(contentsOf: scriptURL), refreshInterval: interval ?? 1800.0)
return (item: item, action: .none)
},
"sleep": { _ in return (item: .staticButton(title: "☕️"), action: .shellScript(executable: "/usr/bin/pmset", parameters: ["sleepnow"]) ) },
]
static let sharedInstance = SupportedTypesHolder()
@ -125,18 +126,22 @@ enum ActionType: Decodable {
case hidKey(keycode: Int)
case keyPress(keycode: Int)
case appleSctipt(source: String)
case shellScript(executable: String, parameters: [String])
case custom(closure: ()->())
private enum CodingKeys: String, CodingKey {
case action
case keycode
case actionAppleScript
case executablePath
case shellArguments
}
private enum ActionTypeRaw: String, Decodable {
case hidKey
case keyPress
case appleScript
case shellScript
}
init(from decoder: Decoder) throws {
@ -152,6 +157,10 @@ enum ActionType: Decodable {
case .some(.appleScript):
let source = try container.decode(String.self, forKey: .actionAppleScript)
self = .appleSctipt(source: source)
case .some(.shellScript):
let executable = try container.decode(String.self, forKey: .executablePath)
let parameters = try container.decodeIfPresent([String].self, forKey: .shellArguments) ?? []
self = .shellScript(executable: executable, parameters: parameters)
case .none:
self = .none
}
@ -181,6 +190,8 @@ func ==(lhs: ActionType, rhs: ActionType) -> Bool {
return a == b
case let (.appleSctipt(a), .appleSctipt(b)):
return a == b
case let (.shellScript(a, b), .shellScript(c, d)):
return a == c && b == d
default:
return false
}

View File

@ -87,13 +87,6 @@ class TouchBarController: NSObject, NSTouchBarDelegate {
NSTouchBar.minimizeSystemModalFunctionBar(touchBar)
}
@objc func goToSleep() {
let task = Process()
task.launchPath = "/usr/bin/pmset"
task.arguments = ["sleepnow"]
task.launch()
}
func touchBar(_ touchBar: NSTouchBar, makeItemForIdentifier identifier: NSTouchBarItem.Identifier) -> NSTouchBarItem? {
guard let item = self.items[identifier] else {
return nil
@ -130,6 +123,13 @@ class TouchBarController: NSObject, NSTouchBarDelegate {
print("error \(error) when handling \(item) ")
}
}
case .shellScript(executable: let executable, parameters: let parameters):
return {
let task = Process()
task.launchPath = executable
task.arguments = parameters
task.launch()
}
case .custom(closure: let closure):
return closure
case .none:

View File

@ -9,5 +9,7 @@
{ "type": "play" },
{ "type": "next" },
{ "type": "weather", "refreshInterval": 1800 },
{ "type": "sleep" },
{ "type": "battery" },
{ "type": "timeButton" },
]