macos.plugin.zsh 6.9 KB

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