osx.plugin.zsh 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  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. opt="set sound volume to $1" #$1 Due to the shift
  208. ;;
  209. playlist)
  210. # Inspired by: https://gist.github.com/nakajijapan/ac8b45371064ae98ea7f
  211. if [[ ! -z "$playlist" ]]; then
  212. osascript -e 'tell application "iTunes"' -e "set new_playlist to \"$playlist\" as string" -e "play playlist new_playlist" -e "end tell" 2>/dev/null;
  213. if [[ $? -eq 0 ]]; then
  214. opt="play"
  215. else
  216. opt="stop"
  217. fi
  218. else
  219. opt="set allPlaylists to (get name of every playlist)"
  220. fi
  221. ;;
  222. playing|status)
  223. local state=`osascript -e 'tell application "iTunes" to player state as string'`
  224. if [[ "$state" = "playing" ]]; then
  225. currenttrack=`osascript -e 'tell application "iTunes" to name of current track as string'`
  226. currentartist=`osascript -e 'tell application "iTunes" to artist of current track as string'`
  227. echo -E "Listening to $fg[yellow]$currenttrack$reset_color by $fg[yellow]$currentartist$reset_color";
  228. else
  229. echo "iTunes is" $state;
  230. fi
  231. return 0
  232. ;;
  233. shuf|shuff|shuffle)
  234. # The shuffle property of current playlist can't be changed in iTunes 12,
  235. # so this workaround uses AppleScript to simulate user input instead.
  236. # Defaults to toggling when no options are given.
  237. # The toggle option depends on the shuffle button being visible in the Now playing area.
  238. # On and off use the menu bar items.
  239. local state=$1
  240. if [[ -n "$state" && ! "$state" =~ "^(on|off|toggle)$" ]]
  241. then
  242. print "Usage: itunes shuffle [on|off|toggle]. Invalid option."
  243. return 1
  244. fi
  245. case "$state" in
  246. on|off)
  247. # Inspired by: https://stackoverflow.com/a/14675583
  248. osascript 1>/dev/null 2>&1 <<-EOF
  249. 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" )
  250. EOF
  251. return 0
  252. ;;
  253. toggle|*)
  254. osascript 1>/dev/null 2>&1 <<-EOF
  255. tell application "System Events" to perform action "AXPress" of (button 2 of process "iTunes"'s window "iTunes"'s scroll area 1)
  256. EOF
  257. return 0
  258. ;;
  259. esac
  260. ;;
  261. ""|-h|--help)
  262. echo "Usage: itunes <option>"
  263. echo "option:"
  264. echo "\tlaunch|play|pause|stop|rewind|resume|quit"
  265. echo "\tmute|unmute\tcontrol volume set"
  266. echo "\tnext|previous\tplay next or previous track"
  267. echo "\tshuf|shuffle [on|off|toggle]\tSet shuffled playback. Default: toggle. Note: toggle doesn't support the MiniPlayer."
  268. echo "\tvol\tSet the volume, takes an argument from 0 to 100"
  269. echo "\tplaying|status\tShow what song is currently playing in iTunes."
  270. echo "\tplaylist [playlist name]\t Play specific playlist"
  271. echo "\thelp\tshow this message and exit"
  272. return 0
  273. ;;
  274. *)
  275. print "Unknown option: $opt"
  276. return 1
  277. ;;
  278. esac
  279. osascript -e "tell application \"iTunes\" to $opt"
  280. }
  281. # Spotify control function
  282. source ${ZSH}/plugins/osx/spotify
  283. # Show/hide hidden files in the Finder
  284. alias showfiles="defaults write com.apple.finder AppleShowAllFiles -bool true && killall Finder"
  285. alias hidefiles="defaults write com.apple.finder AppleShowAllFiles -bool false && killall Finder"
  286. # Remove .DS_Store files recursively in a directory, default .
  287. function rmdsstore() {
  288. find "${@:-.}" -type f -name .DS_Store -delete
  289. }