osx.plugin.zsh 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  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. elif [[ "$the_app" == 'Hyper' ]]; then
  49. osascript >/dev/null <<EOF
  50. tell application "System Events"
  51. tell process "Hyper" to keystroke "t" using command down
  52. end tell
  53. delay 1
  54. tell application "System Events"
  55. keystroke "${command}"
  56. key code 36 #(presses enter)
  57. end tell
  58. EOF
  59. else
  60. echo "tab: unsupported terminal app: $the_app"
  61. false
  62. fi
  63. }
  64. function vsplit_tab() {
  65. local command="cd \\\"$PWD\\\"; clear"
  66. (( $# > 0 )) && command="${command}; $*"
  67. local the_app=$(_omz_osx_get_frontmost_app)
  68. if [[ "$the_app" == 'iTerm' ]]; then
  69. osascript <<EOF
  70. -- tell application "iTerm" to activate
  71. tell application "System Events"
  72. tell process "iTerm"
  73. tell menu item "Split Vertically With Current Profile" of menu "Shell" of menu bar item "Shell" of menu bar 1
  74. click
  75. end tell
  76. end tell
  77. keystroke "${command} \n"
  78. end tell
  79. EOF
  80. elif [[ "$the_app" == 'iTerm2' ]]; then
  81. osascript <<EOF
  82. tell application "iTerm2"
  83. tell current session of first window
  84. set newSession to (split vertically with same profile)
  85. tell newSession
  86. write text "${command}"
  87. select
  88. end tell
  89. end tell
  90. end tell
  91. EOF
  92. elif [[ "$the_app" == 'Hyper' ]]; then
  93. osascript >/dev/null <<EOF
  94. tell application "System Events"
  95. tell process "Hyper"
  96. tell menu item "Split Vertically" of menu "Shell" of menu bar 1
  97. click
  98. end tell
  99. end tell
  100. delay 1
  101. keystroke "${command} \n"
  102. end tell
  103. EOF
  104. else
  105. echo "$0: unsupported terminal app: $the_app" >&2
  106. false
  107. fi
  108. }
  109. function split_tab() {
  110. local command="cd \\\"$PWD\\\"; clear"
  111. (( $# > 0 )) && command="${command}; $*"
  112. local the_app=$(_omz_osx_get_frontmost_app)
  113. if [[ "$the_app" == 'iTerm' ]]; then
  114. osascript 2>/dev/null <<EOF
  115. tell application "iTerm" to activate
  116. tell application "System Events"
  117. tell process "iTerm"
  118. tell menu item "Split Horizontally With Current Profile" of menu "Shell" of menu bar item "Shell" of menu bar 1
  119. click
  120. end tell
  121. end tell
  122. keystroke "${command} \n"
  123. end tell
  124. EOF
  125. elif [[ "$the_app" == 'iTerm2' ]]; then
  126. osascript <<EOF
  127. tell application "iTerm2"
  128. tell current session of first window
  129. set newSession to (split horizontally with same profile)
  130. tell newSession
  131. write text "${command}"
  132. select
  133. end tell
  134. end tell
  135. end tell
  136. EOF
  137. elif [[ "$the_app" == 'Hyper' ]]; then
  138. osascript >/dev/null <<EOF
  139. tell application "System Events"
  140. tell process "Hyper"
  141. tell menu item "Split Horizontally" of menu "Shell" of menu bar 1
  142. click
  143. end tell
  144. end tell
  145. delay 1
  146. keystroke "${command} \n"
  147. end tell
  148. EOF
  149. else
  150. echo "$0: unsupported terminal app: $the_app" >&2
  151. false
  152. fi
  153. }
  154. function pfd() {
  155. osascript 2>/dev/null <<EOF
  156. tell application "Finder"
  157. return POSIX path of (target of window 1 as alias)
  158. end tell
  159. EOF
  160. }
  161. function pfs() {
  162. osascript 2>/dev/null <<EOF
  163. set output to ""
  164. tell application "Finder" to set the_selection to selection
  165. set item_count to count the_selection
  166. repeat with item_index from 1 to count the_selection
  167. if item_index is less than item_count then set the_delimiter to "\n"
  168. if item_index is item_count then set the_delimiter to ""
  169. set output to output & ((item item_index of the_selection as alias)'s POSIX path) & the_delimiter
  170. end repeat
  171. EOF
  172. }
  173. function cdf() {
  174. cd "$(pfd)"
  175. }
  176. function pushdf() {
  177. pushd "$(pfd)"
  178. }
  179. function quick-look() {
  180. (( $# > 0 )) && qlmanage -p $* &>/dev/null &
  181. }
  182. function man-preview() {
  183. man -t "$@" | open -f -a Preview
  184. }
  185. compdef _man man-preview
  186. function vncviewer() {
  187. open vnc://$@
  188. }
  189. # iTunes control function
  190. function itunes() {
  191. local opt=$1
  192. local playlist=$2
  193. shift
  194. case "$opt" in
  195. launch|play|pause|stop|rewind|resume|quit)
  196. ;;
  197. mute)
  198. opt="set mute to true"
  199. ;;
  200. unmute)
  201. opt="set mute to false"
  202. ;;
  203. next|previous)
  204. opt="$opt track"
  205. ;;
  206. vol)
  207. local new_volume volume=$(osascript -e 'tell application "iTunes" to get sound volume')
  208. if [[ $# -eq 0 ]]; then
  209. echo "Current volume is ${volume}."
  210. return 0
  211. fi
  212. case $1 in
  213. up) new_volume=$((volume + 10 < 100 ? volume + 10 : 100)) ;;
  214. down) new_volume=$((volume - 10 > 0 ? volume - 10 : 0)) ;;
  215. <0-100>) new_volume=$1 ;;
  216. *) echo "'$1' is not valid. Expected <0-100>, up or down."
  217. return 1 ;;
  218. esac
  219. opt="set sound volume to ${new_volume}"
  220. ;;
  221. playlist)
  222. # Inspired by: https://gist.github.com/nakajijapan/ac8b45371064ae98ea7f
  223. if [[ ! -z "$playlist" ]]; then
  224. osascript -e 'tell application "iTunes"' -e "set new_playlist to \"$playlist\" as string" -e "play playlist new_playlist" -e "end tell" 2>/dev/null;
  225. if [[ $? -eq 0 ]]; then
  226. opt="play"
  227. else
  228. opt="stop"
  229. fi
  230. else
  231. opt="set allPlaylists to (get name of every playlist)"
  232. fi
  233. ;;
  234. playing|status)
  235. local state=`osascript -e 'tell application "iTunes" to player state as string'`
  236. if [[ "$state" = "playing" ]]; then
  237. currenttrack=`osascript -e 'tell application "iTunes" to name of current track as string'`
  238. currentartist=`osascript -e 'tell application "iTunes" to artist of current track as string'`
  239. echo -E "Listening to $fg[yellow]$currenttrack$reset_color by $fg[yellow]$currentartist$reset_color";
  240. else
  241. echo "iTunes is" $state;
  242. fi
  243. return 0
  244. ;;
  245. shuf|shuff|shuffle)
  246. # The shuffle property of current playlist can't be changed in iTunes 12,
  247. # so this workaround uses AppleScript to simulate user input instead.
  248. # Defaults to toggling when no options are given.
  249. # The toggle option depends on the shuffle button being visible in the Now playing area.
  250. # On and off use the menu bar items.
  251. local state=$1
  252. if [[ -n "$state" && ! "$state" =~ "^(on|off|toggle)$" ]]
  253. then
  254. print "Usage: itunes shuffle [on|off|toggle]. Invalid option."
  255. return 1
  256. fi
  257. case "$state" in
  258. on|off)
  259. # Inspired by: https://stackoverflow.com/a/14675583
  260. osascript 1>/dev/null 2>&1 <<-EOF
  261. 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" )
  262. EOF
  263. return 0
  264. ;;
  265. toggle|*)
  266. osascript 1>/dev/null 2>&1 <<-EOF
  267. tell application "System Events" to perform action "AXPress" of (button 2 of process "iTunes"'s window "iTunes"'s scroll area 1)
  268. EOF
  269. return 0
  270. ;;
  271. esac
  272. ;;
  273. ""|-h|--help)
  274. echo "Usage: itunes <option>"
  275. echo "option:"
  276. echo "\tlaunch|play|pause|stop|rewind|resume|quit"
  277. echo "\tmute|unmute\tcontrol volume set"
  278. echo "\tnext|previous\tplay next or previous track"
  279. echo "\tshuf|shuffle [on|off|toggle]\tSet shuffled playback. Default: toggle. Note: toggle doesn't support the MiniPlayer."
  280. echo "\tvol [0-100|up|down]\tGet or set the volume. 0 to 100 sets the volume. 'up' / 'down' increases / decreases by 10 points. No argument displays current volume."
  281. echo "\tplaying|status\tShow what song is currently playing in iTunes."
  282. echo "\tplaylist [playlist name]\t Play specific playlist"
  283. echo "\thelp\tshow this message and exit"
  284. return 0
  285. ;;
  286. *)
  287. print "Unknown option: $opt"
  288. return 1
  289. ;;
  290. esac
  291. osascript -e "tell application \"iTunes\" to $opt"
  292. }
  293. # Spotify control function
  294. source ${ZSH}/plugins/osx/spotify
  295. # Show/hide hidden files in the Finder
  296. alias showfiles="defaults write com.apple.finder AppleShowAllFiles -bool true && killall Finder"
  297. alias hidefiles="defaults write com.apple.finder AppleShowAllFiles -bool false && killall Finder"
  298. # Remove .DS_Store files recursively in a directory, default .
  299. function rmdsstore() {
  300. find "${@:-.}" -type f -name .DS_Store -delete
  301. }