osx.plugin.zsh 7.9 KB

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