osx.plugin.zsh 7.4 KB

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