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

View File

@ -87,13 +87,6 @@ class TouchBarController: NSObject, NSTouchBarDelegate {
NSTouchBar.minimizeSystemModalFunctionBar(touchBar) 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? { func touchBar(_ touchBar: NSTouchBar, makeItemForIdentifier identifier: NSTouchBarItem.Identifier) -> NSTouchBarItem? {
guard let item = self.items[identifier] else { guard let item = self.items[identifier] else {
return nil return nil
@ -130,6 +123,13 @@ class TouchBarController: NSObject, NSTouchBarDelegate {
print("error \(error) when handling \(item) ") 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): case .custom(closure: let closure):
return closure return closure
case .none: case .none:

View File

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