osx.plugin.zsh 5.6 KB

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