osx.plugin.zsh 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. # ------------------------------------------------------------------------------
  2. # FILE: osx.plugin.zsh
  3. # DESCRIPTION: oh-my-zsh plugin file.
  4. # AUTHOR: Sorin Ionescu (sorin.ionescu@gmail.com)
  5. # VERSION: 1.1.0
  6. # ------------------------------------------------------------------------------
  7. function _omz_osx_get_frontmost_app() {
  8. local the_app=$(
  9. osascript 2>/dev/null <<EOF
  10. tell application "System Events"
  11. name of first item of (every process whose frontmost is true)
  12. end tell
  13. EOF
  14. )
  15. echo "$the_app"
  16. }
  17. function tab() {
  18. # Must not have trailing semicolon, for iTerm compatibility
  19. local command="cd \\\"$PWD\\\"; clear"
  20. (( $# > 0 )) && command="${command}; $*"
  21. local the_app=$(_omz_osx_get_frontmost_app)
  22. if [[ "$the_app" == 'Terminal' ]]; then
  23. # Discarding stdout to quash "tab N of window id XXX" output
  24. osascript >/dev/null <<EOF
  25. tell application "System Events"
  26. tell process "Terminal" to keystroke "t" using command down
  27. end tell
  28. tell application "Terminal" to do script "${command}" in front window
  29. EOF
  30. elif [[ "$the_app" == 'iTerm' ]]; then
  31. osascript <<EOF
  32. tell application "iTerm"
  33. set current_terminal to current terminal
  34. tell current_terminal
  35. launch session "Default Session"
  36. set current_session to current session
  37. tell current_session
  38. write text "${command}"
  39. end tell
  40. end tell
  41. end tell
  42. EOF
  43. else
  44. echo "tab: unsupported terminal app: $the_app"
  45. false
  46. fi
  47. }
  48. function vsplit_tab() {
  49. local command="cd \\\"$PWD\\\"; clear"
  50. (( $# > 0 )) && command="${command}; $*"
  51. local the_app=$(_omz_osx_get_frontmost_app)
  52. if [[ "$the_app" == 'iTerm' ]]; then
  53. osascript <<EOF
  54. -- tell application "iTerm" to activate
  55. tell application "System Events"
  56. tell process "iTerm"
  57. tell menu item "Split Vertically With Current Profile" of menu "Shell" of menu bar item "Shell" of menu bar 1
  58. click
  59. end tell
  60. end tell
  61. keystroke "${command} \n"
  62. end tell
  63. EOF
  64. else
  65. echo "$0: unsupported terminal app: $the_app" >&2
  66. false
  67. fi
  68. }
  69. function split_tab() {
  70. local command="cd \\\"$PWD\\\"; clear"
  71. (( $# > 0 )) && command="${command}; $*"
  72. local the_app=$(_omz_osx_get_frontmost_app)
  73. if [[ "$the_app" == 'iTerm' ]]; then
  74. osascript 2>/dev/null <<EOF
  75. tell application "iTerm" to activate
  76. tell application "System Events"
  77. tell process "iTerm"
  78. tell menu item "Split Horizontally With Current Profile" of menu "Shell" of menu bar item "Shell" of menu bar 1
  79. click
  80. end tell
  81. end tell
  82. keystroke "${command} \n"
  83. end tell
  84. EOF
  85. else
  86. echo "$0: unsupported terminal app: $the_app" >&2
  87. false
  88. fi
  89. }
  90. function pfd() {
  91. osascript 2>/dev/null <<EOF
  92. tell application "Finder"
  93. return POSIX path of (target of window 1 as alias)
  94. end tell
  95. EOF
  96. }
  97. function pfs() {
  98. osascript 2>/dev/null <<EOF
  99. set output to ""
  100. tell application "Finder" to set the_selection to selection
  101. set item_count to count the_selection
  102. repeat with item_index from 1 to count the_selection
  103. if item_index is less than item_count then set the_delimiter to "\n"
  104. if item_index is item_count then set the_delimiter to ""
  105. set output to output & ((item item_index of the_selection as alias)'s POSIX path) & the_delimiter
  106. end repeat
  107. EOF
  108. }
  109. function cdf() {
  110. cd "$(pfd)"
  111. }
  112. function pushdf() {
  113. pushd "$(pfd)"
  114. }
  115. function quick-look() {
  116. (( $# > 0 )) && qlmanage -p $* &>/dev/null &
  117. }
  118. function man-preview() {
  119. man -t "$@" | open -f -a Preview
  120. }
  121. function vncviewer() {
  122. open vnc://$@
  123. }
  124. # iTunes control function
  125. function itunes() {
  126. local opt=$1
  127. shift
  128. case "$opt" in
  129. launch|play|pause|stop|rewind|resume|quit)
  130. ;;
  131. mute)
  132. opt="set mute to true"
  133. ;;
  134. unmute)
  135. opt="set mute to false"
  136. ;;
  137. next|previous)
  138. opt="$opt track"
  139. ;;
  140. vol)
  141. opt="set sound volume to $1" #$1 Due to the shift
  142. ;;
  143. playing|status)
  144. local state=`osascript -e 'tell application "iTunes" to player state as string'`
  145. if [[ "$state" = "playing" ]]; then
  146. currenttrack=`osascript -e 'tell application "iTunes" to name of current track as string'`
  147. currentartist=`osascript -e 'tell application "iTunes" to artist of current track as string'`
  148. echo -E "Listening to $fg[yellow]$currenttrack$reset_color by $fg[yellow]$currentartist$reset_color";
  149. else
  150. echo "iTunes is" $state;
  151. fi
  152. return 0
  153. ;;
  154. shuf|shuff|shuffle)
  155. # The shuffle property of current playlist can't be changed in iTunes 12,
  156. # so this workaround uses AppleScript to simulate user input instead.
  157. # Defaults to toggling when no options are given.
  158. # The toggle option depends on the shuffle button being visible in the Now playing area.
  159. # On and off use the menu bar items.
  160. local state=$1
  161. if [[ -n "$state" && ! "$state" =~ "^(on|off|toggle)$" ]]
  162. then
  163. print "Usage: itunes shuffle [on|off|toggle]. Invalid option."
  164. return 1
  165. fi
  166. case "$state" in
  167. on|off)
  168. # Inspired by: http://stackoverflow.com/a/14675583
  169. osascript 1>/dev/null 2>&1 <<-EOF
  170. tell application "System Events" to perform action "AXPress" of (menu item "${state}" of menu "Shuffle" of menu item "Shuffle" of menu "Controls" of menu bar item "Controls" of menu bar 1 of application process "iTunes" )
  171. EOF
  172. return 0
  173. ;;
  174. toggle|*)
  175. osascript 1>/dev/null 2>&1 <<-EOF
  176. tell application "System Events" to perform action "AXPress" of (button 2 of process "iTunes"'s window "iTunes"'s scroll area 1)
  177. EOF
  178. return 0
  179. ;;
  180. esac
  181. ;;
  182. ""|-h|--help)
  183. echo "Usage: itunes <option>"
  184. echo "option:"
  185. echo "\tlaunch|play|pause|stop|rewind|resume|quit"
  186. echo "\tmute|unmute\tcontrol volume set"
  187. echo "\tnext|previous\tplay next or previous track"
  188. echo "\tshuf|shuffle [on|off|toggle]\tSet shuffled playback. Default: toggle. Note: toggle doesn't support the MiniPlayer."
  189. echo "\tvol\tSet the volume, takes an argument from 0 to 100"
  190. echo "\tplaying|status\tShow what song is currently playing in iTunes."
  191. echo "\thelp\tshow this message and exit"
  192. return 0
  193. ;;
  194. *)
  195. print "Unknown option: $opt"
  196. return 1
  197. ;;
  198. esac
  199. osascript -e "tell application \"iTunes\" to $opt"
  200. }