macos.plugin.zsh 8.4 KB

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