osx.plugin.zsh 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  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. elif [[ "$the_app" == 'iTerm2' ]]; then
  44. osascript <<EOF
  45. tell application "iTerm"
  46. tell current window
  47. create tab with default profile
  48. tell current session to write text "${command}"
  49. end tell
  50. end tell
  51. EOF
  52. else
  53. echo "tab: unsupported terminal app: $the_app"
  54. false
  55. fi
  56. }
  57. function vsplit_tab() {
  58. local command="cd \\\"$PWD\\\"; clear"
  59. (( $# > 0 )) && command="${command}; $*"
  60. local the_app=$(_omz_osx_get_frontmost_app)
  61. if [[ "$the_app" == 'iTerm' ]]; then
  62. osascript <<EOF
  63. -- tell application "iTerm" to activate
  64. tell application "System Events"
  65. tell process "iTerm"
  66. tell menu item "Split Vertically With Current Profile" of menu "Shell" of menu bar item "Shell" of menu bar 1
  67. click
  68. end tell
  69. end tell
  70. keystroke "${command} \n"
  71. end tell
  72. EOF
  73. elif [[ "$the_app" == 'iTerm2' ]]; then
  74. osascript <<EOF
  75. tell application "iTerm"
  76. tell current session of first window
  77. set newSession to (split vertically with same profile)
  78. tell newSession
  79. write text "${command}"
  80. select
  81. end tell
  82. end tell
  83. end tell
  84. EOF
  85. else
  86. echo "$0: unsupported terminal app: $the_app" >&2
  87. false
  88. fi
  89. }
  90. function split_tab() {
  91. local command="cd \\\"$PWD\\\"; clear"
  92. (( $# > 0 )) && command="${command}; $*"
  93. local the_app=$(_omz_osx_get_frontmost_app)
  94. if [[ "$the_app" == 'iTerm' ]]; then
  95. osascript 2>/dev/null <<EOF
  96. tell application "iTerm" to activate
  97. tell application "System Events"
  98. tell process "iTerm"
  99. tell menu item "Split Horizontally With Current Profile" of menu "Shell" of menu bar item "Shell" of menu bar 1
  100. click
  101. end tell
  102. end tell
  103. keystroke "${command} \n"
  104. end tell
  105. EOF
  106. elif [[ "$the_app" == 'iTerm2' ]]; then
  107. osascript <<EOF
  108. tell application "iTerm"
  109. tell current session of first window
  110. set newSession to (split horizontally with same profile)
  111. tell newSession
  112. write text "${command}"
  113. select
  114. end tell
  115. end tell
  116. end tell
  117. EOF
  118. else
  119. echo "$0: unsupported terminal app: $the_app" >&2
  120. false
  121. fi
  122. }
  123. function pfd() {
  124. osascript 2>/dev/null <<EOF
  125. tell application "Finder"
  126. return POSIX path of (target of window 1 as alias)
  127. end tell
  128. EOF
  129. }
  130. function pfs() {
  131. osascript 2>/dev/null <<EOF
  132. set output to ""
  133. tell application "Finder" to set the_selection to selection
  134. set item_count to count the_selection
  135. repeat with item_index from 1 to count the_selection
  136. if item_index is less than item_count then set the_delimiter to "\n"
  137. if item_index is item_count then set the_delimiter to ""
  138. set output to output & ((item item_index of the_selection as alias)'s POSIX path) & the_delimiter
  139. end repeat
  140. EOF
  141. }
  142. function cdf() {
  143. cd "$(pfd)"
  144. }
  145. function pushdf() {
  146. pushd "$(pfd)"
  147. }
  148. function quick-look() {
  149. (( $# > 0 )) && qlmanage -p $* &>/dev/null &
  150. }
  151. function man-preview() {
  152. man -t "$@" | open -f -a Preview
  153. }
  154. function vncviewer() {
  155. open vnc://$@
  156. }
  157. # iTunes control function
  158. function itunes() {
  159. local opt=$1
  160. shift
  161. case "$opt" in
  162. launch|play|pause|stop|rewind|resume|quit)
  163. ;;
  164. mute)
  165. opt="set mute to true"
  166. ;;
  167. unmute)
  168. opt="set mute to false"
  169. ;;
  170. next|previous)
  171. opt="$opt track"
  172. ;;
  173. vol)
  174. opt="set sound volume to $1" #$1 Due to the shift
  175. ;;
  176. playing|status)
  177. local state=`osascript -e 'tell application "iTunes" to player state as string'`
  178. if [[ "$state" = "playing" ]]; then
  179. currenttrack=`osascript -e 'tell application "iTunes" to name of current track as string'`
  180. currentartist=`osascript -e 'tell application "iTunes" to artist of current track as string'`
  181. echo -E "Listening to $fg[yellow]$currenttrack$reset_color by $fg[yellow]$currentartist$reset_color";
  182. else
  183. echo "iTunes is" $state;
  184. fi
  185. return 0
  186. ;;
  187. shuf|shuff|shuffle)
  188. # The shuffle property of current playlist can't be changed in iTunes 12,
  189. # so this workaround uses AppleScript to simulate user input instead.
  190. # Defaults to toggling when no options are given.
  191. # The toggle option depends on the shuffle button being visible in the Now playing area.
  192. # On and off use the menu bar items.
  193. local state=$1
  194. if [[ -n "$state" && ! "$state" =~ "^(on|off|toggle)$" ]]
  195. then
  196. print "Usage: itunes shuffle [on|off|toggle]. Invalid option."
  197. return 1
  198. fi
  199. case "$state" in
  200. on|off)
  201. # Inspired by: http://stackoverflow.com/a/14675583
  202. osascript 1>/dev/null 2>&1 <<-EOF
  203. 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" )
  204. EOF
  205. return 0
  206. ;;
  207. toggle|*)
  208. osascript 1>/dev/null 2>&1 <<-EOF
  209. tell application "System Events" to perform action "AXPress" of (button 2 of process "iTunes"'s window "iTunes"'s scroll area 1)
  210. EOF
  211. return 0
  212. ;;
  213. esac
  214. ;;
  215. ""|-h|--help)
  216. echo "Usage: itunes <option>"
  217. echo "option:"
  218. echo "\tlaunch|play|pause|stop|rewind|resume|quit"
  219. echo "\tmute|unmute\tcontrol volume set"
  220. echo "\tnext|previous\tplay next or previous track"
  221. echo "\tshuf|shuffle [on|off|toggle]\tSet shuffled playback. Default: toggle. Note: toggle doesn't support the MiniPlayer."
  222. echo "\tvol\tSet the volume, takes an argument from 0 to 100"
  223. echo "\tplaying|status\tShow what song is currently playing in iTunes."
  224. echo "\thelp\tshow this message and exit"
  225. return 0
  226. ;;
  227. *)
  228. print "Unknown option: $opt"
  229. return 1
  230. ;;
  231. esac
  232. osascript -e "tell application \"iTunes\" to $opt"
  233. }