macos.plugin.zsh 7.2 KB

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