osx.plugin.zsh 9.8 KB

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