macos.plugin.zsh 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. # Handle $0 according to the standard:
  2. # https://zdharma-continuum.github.io/Zsh-100-Commits-Club/Zsh-Plugin-Standard.html
  3. 0="${${ZERO:-${0:#$ZSH_ARGZERO}}:-${(%):-%N}}"
  4. 0="${${(M)0:#/*}:-$PWD/$0}"
  5. # Open the current directory in a Finder window
  6. alias ofd='open_command $PWD'
  7. # Show/hide hidden files in the Finder
  8. alias showfiles="defaults write com.apple.finder AppleShowAllFiles -bool true && killall Finder"
  9. alias hidefiles="defaults write com.apple.finder AppleShowAllFiles -bool false && killall Finder"
  10. # Bluetooth restart
  11. function btrestart() {
  12. sudo kextunload -b com.apple.iokit.BroadcomBluetoothHostControllerUSBTransport
  13. sudo kextload -b com.apple.iokit.BroadcomBluetoothHostControllerUSBTransport
  14. }
  15. function _omz_macos_get_frontmost_app() {
  16. osascript 2>/dev/null <<EOF
  17. tell application "System Events"
  18. name of first item of (every process whose frontmost is true)
  19. end tell
  20. EOF
  21. }
  22. function tab() {
  23. # Must not have trailing semicolon, for iTerm compatibility
  24. local command="cd \\\"$PWD\\\"; clear"
  25. (( $# > 0 )) && command="${command}; $*"
  26. local the_app=$(_omz_macos_get_frontmost_app)
  27. if [[ "$the_app" == 'Terminal' ]]; then
  28. # Discarding stdout to quash "tab N of window id XXX" output
  29. osascript >/dev/null <<EOF
  30. tell application "System Events"
  31. tell process "Terminal" to keystroke "t" using command down
  32. end tell
  33. tell application "Terminal" to do script "${command}" in front window
  34. EOF
  35. elif [[ "$the_app" == 'iTerm' ]]; then
  36. osascript <<EOF
  37. tell application "iTerm"
  38. set current_terminal to current terminal
  39. tell current_terminal
  40. launch session "Default Session"
  41. set current_session to current session
  42. tell current_session
  43. write text "${command}"
  44. end tell
  45. end tell
  46. end tell
  47. EOF
  48. elif [[ "$the_app" == 'iTerm2' ]]; then
  49. osascript <<EOF
  50. tell application "iTerm2"
  51. tell current window
  52. create tab with default profile
  53. tell current session to write text "${command}"
  54. end tell
  55. end tell
  56. EOF
  57. elif [[ "$the_app" == 'Hyper' ]]; then
  58. osascript >/dev/null <<EOF
  59. tell application "System Events"
  60. tell process "Hyper" to keystroke "t" using command down
  61. end tell
  62. delay 1
  63. tell application "System Events"
  64. keystroke "${command}"
  65. key code 36 #(presses enter)
  66. end tell
  67. EOF
  68. else
  69. echo "$0: unsupported terminal app: $the_app" >&2
  70. return 1
  71. fi
  72. }
  73. function vsplit_tab() {
  74. local command="cd \\\"$PWD\\\"; clear"
  75. (( $# > 0 )) && command="${command}; $*"
  76. local the_app=$(_omz_macos_get_frontmost_app)
  77. if [[ "$the_app" == 'iTerm' ]]; then
  78. osascript <<EOF
  79. -- tell application "iTerm" to activate
  80. tell application "System Events"
  81. tell process "iTerm"
  82. tell menu item "Split Vertically With Current Profile" of menu "Shell" of menu bar item "Shell" of menu bar 1
  83. click
  84. end tell
  85. end tell
  86. keystroke "${command} \n"
  87. end tell
  88. EOF
  89. elif [[ "$the_app" == 'iTerm2' ]]; then
  90. osascript <<EOF
  91. tell application "iTerm2"
  92. tell current session of first window
  93. set newSession to (split vertically with same profile)
  94. tell newSession
  95. write text "${command}"
  96. select
  97. end tell
  98. end tell
  99. end tell
  100. EOF
  101. elif [[ "$the_app" == 'Hyper' ]]; then
  102. osascript >/dev/null <<EOF
  103. tell application "System Events"
  104. tell process "Hyper"
  105. tell menu item "Split Vertically" of menu "Shell" of menu bar 1
  106. click
  107. end tell
  108. end tell
  109. delay 1
  110. keystroke "${command} \n"
  111. end tell
  112. EOF
  113. else
  114. echo "$0: unsupported terminal app: $the_app" >&2
  115. return 1
  116. fi
  117. }
  118. function split_tab() {
  119. local command="cd \\\"$PWD\\\"; clear"
  120. (( $# > 0 )) && command="${command}; $*"
  121. local the_app=$(_omz_macos_get_frontmost_app)
  122. if [[ "$the_app" == 'iTerm' ]]; then
  123. osascript 2>/dev/null <<EOF
  124. tell application "iTerm" to activate
  125. tell application "System Events"
  126. tell process "iTerm"
  127. tell menu item "Split Horizontally With Current Profile" of menu "Shell" of menu bar item "Shell" of menu bar 1
  128. click
  129. end tell
  130. end tell
  131. keystroke "${command} \n"
  132. end tell
  133. EOF
  134. elif [[ "$the_app" == 'iTerm2' ]]; then
  135. osascript <<EOF
  136. tell application "iTerm2"
  137. tell current session of first window
  138. set newSession to (split horizontally with same profile)
  139. tell newSession
  140. write text "${command}"
  141. select
  142. end tell
  143. end tell
  144. end tell
  145. EOF
  146. elif [[ "$the_app" == 'Hyper' ]]; then
  147. osascript >/dev/null <<EOF
  148. tell application "System Events"
  149. tell process "Hyper"
  150. tell menu item "Split Horizontally" of menu "Shell" of menu bar 1
  151. click
  152. end tell
  153. end tell
  154. delay 1
  155. keystroke "${command} \n"
  156. end tell
  157. EOF
  158. else
  159. echo "$0: unsupported terminal app: $the_app" >&2
  160. return 1
  161. fi
  162. }
  163. function pfd() {
  164. osascript 2>/dev/null <<EOF
  165. tell application "Finder"
  166. return POSIX path of (insertion location as alias)
  167. end tell
  168. EOF
  169. }
  170. function pfs() {
  171. osascript 2>/dev/null <<EOF
  172. set output to ""
  173. tell application "Finder" to set the_selection to selection
  174. set item_count to count the_selection
  175. repeat with item_index from 1 to count the_selection
  176. if item_index is less than item_count then set the_delimiter to "\n"
  177. if item_index is item_count then set the_delimiter to ""
  178. set output to output & ((item item_index of the_selection as alias)'s POSIX path) & the_delimiter
  179. end repeat
  180. EOF
  181. }
  182. function cdf() {
  183. cd "$(pfd)"
  184. }
  185. function pushdf() {
  186. pushd "$(pfd)"
  187. }
  188. function pxd() {
  189. dirname $(osascript 2>/dev/null <<EOF
  190. if application "Xcode" is running then
  191. tell application "Xcode"
  192. return path of active workspace document
  193. end tell
  194. end if
  195. EOF
  196. )
  197. }
  198. function cdx() {
  199. cd "$(pxd)"
  200. }
  201. function quick-look() {
  202. (( $# > 0 )) && qlmanage -p $* &>/dev/null &
  203. }
  204. function man-preview() {
  205. # Don't let Preview.app steal focus if the man page doesn't exist
  206. man -w "$@" &>/dev/null && man -t "$@" | open -f -a Preview || man "$@"
  207. }
  208. compdef _man man-preview
  209. function vncviewer() {
  210. open vnc://$@
  211. }
  212. # Remove .DS_Store files recursively in a directory, default .
  213. function rmdsstore() {
  214. find "${@:-.}" -type f -name .DS_Store -delete
  215. }
  216. # Erases purgeable disk space with 0s on the selected disk
  217. function freespace(){
  218. if [[ -z "$1" ]]; then
  219. echo "Usage: $0 <disk>"
  220. echo "Example: $0 /dev/disk1s1"
  221. echo
  222. echo "Possible disks:"
  223. df -h | awk 'NR == 1 || /^\/dev\/disk/'
  224. return 1
  225. fi
  226. echo "Cleaning purgeable files from disk: $1 ...."
  227. diskutil secureErase freespace 0 $1
  228. }
  229. _freespace() {
  230. local -a disks
  231. disks=("${(@f)"$(df | awk '/^\/dev\/disk/{ printf $1 ":"; for (i=9; i<=NF; i++) printf $i FS; print "" }')"}")
  232. _describe disks disks
  233. }
  234. compdef _freespace freespace
  235. # Music / iTunes control function
  236. source "${0:h:A}/music"
  237. # Spotify control function
  238. source "${0:h:A}/spotify"