Guest User

Untitled

a guest
Feb 18th, 2018
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.06 KB | None | 0 0
  1. #!/usr/bin/env bash
  2.  
  3. ME=$(basename $0)
  4. MUTEFILE=~/.channels.mute
  5.  
  6. usage() {
  7. cat <<END
  8. $ME COMMAND [ARGS...]: Volume control / muting
  9.  
  10. Commands:
  11.  
  12. -t [CHANNEL] Toggle muting of CHANNEL
  13. (Default input channel)
  14. -l Show current volumes of all channels
  15. -s CHANNEL [VALUE] Set the volume of CHANNEL to VALUE
  16. or show the current volume of CHANNEL
  17. (Default input channel)
  18. -h This help
  19.  
  20. END
  21. }
  22.  
  23. osa() {
  24. osascript <<< "$@"
  25. return $?
  26. }
  27.  
  28. getvol() {
  29. local which="$1"
  30. osa $which volume of \(get volume settings\)
  31. return $?
  32. }
  33.  
  34. setvol() {
  35. local which="$1"
  36. local volume="$2"
  37. osa set volume "$which" volume "$volume"
  38. return $?
  39. }
  40.  
  41. show() {
  42. local which="$1"
  43. echo -n "$which: "
  44. getvol "$which"
  45. return $?
  46. }
  47.  
  48. toggle() {
  49. local which="$1"
  50. local ret=
  51. local current="$(getvol "$which")"
  52.  
  53. if [ "$current" -gt 0 ]; then
  54. echo "$which $current" > "$MUTEFILE"
  55. setvol "$which" 0
  56. ret=$?
  57. else
  58. setvol "$which" "$(getlast "$which")"
  59. ret=$?
  60. fi
  61.  
  62. return $ret
  63. }
  64.  
  65. getlast() {
  66. local which="$1"
  67. local last=
  68. local ret=0
  69. local CHANNEL=
  70. local VOLUME=
  71. local TEMPFILE="$(mktemp mute.XXXX)"
  72.  
  73. while read CHANNEL VOLUME; do
  74. if [ "$CHANNEL" = "$which" ]; then
  75. last="$VOLUME"
  76. break
  77. else
  78. echo "$CHANNEL $VOLUME" >> "$TEMPFILE"
  79. fi
  80. done < "$MUTEFILE"
  81.  
  82. if [ -n "$last" ]; then
  83. ret=1
  84. last=64
  85. fi
  86.  
  87. mv "$TEMPFILE" "$MUTEFILE"
  88.  
  89. echo "$last"
  90. return $ret
  91. }
  92.  
  93. main() {
  94. local cmd="$1"
  95. shift
  96. local which="${1:-input}"
  97. [ -n "$1" ] && shift
  98.  
  99. case $cmd in
  100. -t)
  101. toggle "$which"
  102. show "$which"
  103. ;;
  104. -s)
  105. [ -n "$1" ] && setvol "$which" "$1"
  106. show "$which"
  107. ;;
  108. -l)
  109. osa get volume settings
  110. ;;
  111. *)
  112. usage
  113. exit 1
  114. ;;
  115. esac
  116. }
  117.  
  118. main "$@"
Add Comment
Please, Sign In to add comment