osx.plugin.zsh 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  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 trash() {
  123. local trash_dir="${HOME}/.Trash"
  124. local temp_ifs="$IFS"
  125. IFS=$'\n'
  126. for item in "$@"; do
  127. if [[ -e "$item" ]]; then
  128. item_name="$(basename $item)"
  129. if [[ -e "${trash_dir}/${item_name}" ]]; then
  130. mv -f "$item" "${trash_dir}/${item_name} $(date "+%H-%M-%S")"
  131. else
  132. mv -f "$item" "${trash_dir}/"
  133. fi
  134. fi
  135. done
  136. IFS=$temp_ifs
  137. }
  138. function vncviewer() {
  139. open vnc://$@
  140. }
  141. # iTunes control function
  142. function itunes() {
  143. local opt=$1
  144. shift
  145. case "$opt" in
  146. launch|play|pause|stop|rewind|resume|quit)
  147. ;;
  148. mute)
  149. opt="set mute to true"
  150. ;;
  151. unmute)
  152. opt="set mute to false"
  153. ;;
  154. next|previous)
  155. opt="$opt track"
  156. ;;
  157. vol)
  158. opt="set sound volume to $1" #$1 Due to the shift
  159. ;;
  160. shuf|shuff|shuffle)
  161. # The shuffle property of current playlist can't be changed in iTunes 12,
  162. # so this workaround uses AppleScript to simulate user input instead.
  163. # Defaults to toggling when no options are given.
  164. # The toggle option depends on the shuffle button being visible in the Now playing area.
  165. # On and off use the menu bar items.
  166. local state=$1
  167. if [[ -n "$state" && ! "$state" =~ "^(on|off|toggle)$" ]]
  168. then
  169. print "Usage: itunes shuffle [on|off|toggle]. Invalid option."
  170. return 1
  171. fi
  172. case "$state" in
  173. on|off)
  174. # Inspired by: http://stackoverflow.com/a/14675583
  175. osascript 1>/dev/null 2>&1 <<-EOF
  176. 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" )
  177. EOF
  178. return 0
  179. ;;
  180. toggle|*)
  181. osascript 1>/dev/null 2>&1 <<-EOF
  182. tell application "System Events" to perform action "AXPress" of (button 2 of process "iTunes"'s window "iTunes"'s scroll area 1)
  183. EOF
  184. return 0
  185. ;;
  186. esac
  187. ;;
  188. ""|-h|--help)
  189. echo "Usage: itunes <option>"
  190. echo "option:"
  191. echo "\tlaunch|play|pause|stop|rewind|resume|quit"
  192. echo "\tmute|unmute\tcontrol volume set"
  193. echo "\tnext|previous\tplay next or previous track"
  194. echo "\tshuf|shuffle [on|off|toggle]\tSet shuffled playback. Default: toggle. Note: toggle doesn't support the MiniPlayer."
  195. echo "\tvol\tSet the volume, takes an argument from 0 to 100"
  196. echo "\thelp\tshow this message and exit"
  197. return 0
  198. ;;
  199. *)
  200. print "Unknown option: $opt"
  201. return 1
  202. ;;
  203. esac
  204. osascript -e "tell application \"iTunes\" to $opt"
  205. }