macos.plugin.zsh 7.8 KB

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