macos.plugin.zsh 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  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. else
  76. echo "$0: unsupported terminal app: $the_app" >&2
  77. return 1
  78. fi
  79. }
  80. function vsplit_tab() {
  81. local command="cd \\\"$PWD\\\"; clear"
  82. (( $# > 0 )) && command="${command}; $*"
  83. local the_app=$(_omz_macos_get_frontmost_app)
  84. if [[ "$the_app" == 'iTerm' ]]; then
  85. osascript <<EOF
  86. -- tell application "iTerm" to activate
  87. tell application "System Events"
  88. tell process "iTerm"
  89. tell menu item "Split Vertically With Current Profile" of menu "Shell" of menu bar item "Shell" of menu bar 1
  90. click
  91. end tell
  92. end tell
  93. keystroke "${command} \n"
  94. end tell
  95. EOF
  96. elif [[ "$the_app" == 'iTerm2' ]]; then
  97. osascript <<EOF
  98. tell application "iTerm2"
  99. tell current session of first window
  100. set newSession to (split vertically with same profile)
  101. tell newSession
  102. write text "${command}"
  103. select
  104. end tell
  105. end tell
  106. end tell
  107. EOF
  108. elif [[ "$the_app" == 'Hyper' ]]; then
  109. osascript >/dev/null <<EOF
  110. tell application "System Events"
  111. tell process "Hyper"
  112. tell menu item "Split Vertically" of menu "Shell" of menu bar 1
  113. click
  114. end tell
  115. end tell
  116. delay 1
  117. keystroke "${command} \n"
  118. end tell
  119. EOF
  120. else
  121. echo "$0: unsupported terminal app: $the_app" >&2
  122. return 1
  123. fi
  124. }
  125. function split_tab() {
  126. local command="cd \\\"$PWD\\\"; clear"
  127. (( $# > 0 )) && command="${command}; $*"
  128. local the_app=$(_omz_macos_get_frontmost_app)
  129. if [[ "$the_app" == 'iTerm' ]]; then
  130. osascript 2>/dev/null <<EOF
  131. tell application "iTerm" to activate
  132. tell application "System Events"
  133. tell process "iTerm"
  134. tell menu item "Split Horizontally With Current Profile" of menu "Shell" of menu bar item "Shell" of menu bar 1
  135. click
  136. end tell
  137. end tell
  138. keystroke "${command} \n"
  139. end tell
  140. EOF
  141. elif [[ "$the_app" == 'iTerm2' ]]; then
  142. osascript <<EOF
  143. tell application "iTerm2"
  144. tell current session of first window
  145. set newSession to (split horizontally with same profile)
  146. tell newSession
  147. write text "${command}"
  148. select
  149. end tell
  150. end tell
  151. end tell
  152. EOF
  153. elif [[ "$the_app" == 'Hyper' ]]; then
  154. osascript >/dev/null <<EOF
  155. tell application "System Events"
  156. tell process "Hyper"
  157. tell menu item "Split Horizontally" of menu "Shell" of menu bar 1
  158. click
  159. end tell
  160. end tell
  161. delay 1
  162. keystroke "${command} \n"
  163. end tell
  164. EOF
  165. else
  166. echo "$0: unsupported terminal app: $the_app" >&2
  167. return 1
  168. fi
  169. }
  170. function pfd() {
  171. osascript 2>/dev/null <<EOF
  172. tell application "Finder"
  173. return POSIX path of (insertion location as alias)
  174. end tell
  175. EOF
  176. }
  177. function pfs() {
  178. osascript 2>/dev/null <<EOF
  179. set output to ""
  180. tell application "Finder" to set the_selection to selection
  181. set item_count to count the_selection
  182. repeat with item_index from 1 to count the_selection
  183. if item_index is less than item_count then set the_delimiter to "\n"
  184. if item_index is item_count then set the_delimiter to ""
  185. set output to output & ((item item_index of the_selection as alias)'s POSIX path) & the_delimiter
  186. end repeat
  187. EOF
  188. }
  189. function cdf() {
  190. cd "$(pfd)"
  191. }
  192. function pushdf() {
  193. pushd "$(pfd)"
  194. }
  195. function pxd() {
  196. dirname $(osascript 2>/dev/null <<EOF
  197. if application "Xcode" is running then
  198. tell application "Xcode"
  199. return path of active workspace document
  200. end tell
  201. end if
  202. EOF
  203. )
  204. }
  205. function cdx() {
  206. cd "$(pxd)"
  207. }
  208. function quick-look() {
  209. (( $# > 0 )) && qlmanage -p $* &>/dev/null &
  210. }
  211. function man-preview() {
  212. [[ $# -eq 0 ]] && >&2 echo "Usage: $0 command1 [command2 ...]" && return 1
  213. local page
  214. for page in "${(@f)"$(man -w $@)"}"; do
  215. command mandoc -Tpdf $page | open -f -a Preview
  216. done
  217. }
  218. compdef _man man-preview
  219. function vncviewer() {
  220. open vnc://$@
  221. }
  222. # Remove .DS_Store files recursively in a directory, default .
  223. function rmdsstore() {
  224. find "${@:-.}" -type f -name .DS_Store -delete
  225. }
  226. # Erases purgeable disk space with 0s on the selected disk
  227. function freespace(){
  228. if [[ -z "$1" ]]; then
  229. echo "Usage: $0 <disk>"
  230. echo "Example: $0 /dev/disk1s1"
  231. echo
  232. echo "Possible disks:"
  233. df -h | awk 'NR == 1 || /^\/dev\/disk/'
  234. return 1
  235. fi
  236. echo "Cleaning purgeable files from disk: $1 ...."
  237. diskutil secureErase freespace 0 $1
  238. }
  239. _freespace() {
  240. local -a disks
  241. disks=("${(@f)"$(df | awk '/^\/dev\/disk/{ printf $1 ":"; for (i=9; i<=NF; i++) printf $i FS; print "" }')"}")
  242. _describe disks disks
  243. }
  244. compdef _freespace freespace
  245. # Music / iTunes control function
  246. source "${0:h:A}/music"
  247. # Spotify control function
  248. source "${0:h:A}/spotify"