Last active
January 12, 2018 10:35
-
-
Save bduisenov/a23c682daf8f1c7a73af93b178633110 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Foundation | |
private func bash(_ commandName: String, _ arguments: [String]) -> String? { | |
guard var whichPathForCommand = executeShell(command: "/bin/bash" , arguments:[ "-l", "-c", "which \(commandName)" ]) else { | |
return "\(commandName) not found" | |
} | |
whichPathForCommand = whichPathForCommand.trimmingCharacters(in: NSCharacterSet.whitespacesAndNewlines) | |
return executeShell(command: whichPathForCommand, arguments: arguments) | |
} | |
private func executeShell(command: String, arguments: [String] = []) -> String? { | |
let task = Process() | |
task.launchPath = command | |
task.arguments = arguments | |
let pipe = Pipe() | |
task.standardOutput = pipe | |
task.launch() | |
let data = pipe.fileHandleForReading.readDataToEndOfFile() | |
let output: String? = String(data: data, encoding: String.Encoding.utf8) | |
return output | |
} | |
let _ = bash("open", ["-a", "AnyBar"]) | |
while true { | |
let result = bash("ping", ["-c 1", "ya.ru"]) | |
if (result!.contains("ttl")) { | |
let _ = bash("anybar", ["purple"]) | |
} else { | |
let _ = bash("anybar", ["red"]) | |
} | |
sleep(2) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment