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}"
6 # Open the current directory in a Finder window
7 alias ofd='open_command $PWD'
9 # Show/hide hidden files in the Finder
10 alias showfiles="defaults write com.apple.finder AppleShowAllFiles -bool true && killall Finder"
11 alias hidefiles="defaults write com.apple.finder AppleShowAllFiles -bool false && killall Finder"
14 function btrestart() {
15 sudo kextunload -b com.apple.iokit.BroadcomBluetoothHostControllerUSBTransport
16 sudo kextload -b com.apple.iokit.BroadcomBluetoothHostControllerUSBTransport
19 function _omz_macos_get_frontmost_app() {
20 osascript 2>/dev/null <<EOF
21 tell application "System Events"
22 name of first item of (every process whose frontmost is true)
28 # Must not have trailing semicolon, for iTerm compatibility
29 local command="cd \\\"$PWD\\\"; clear"
30 (( $# > 0 )) && command="${command}; $*"
32 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
40 tell application "Terminal" to do script "${command}" in front window
42 elif [[ "$the_app" == 'iTerm' ]]; then
44 tell application "iTerm"
45 set current_terminal to current terminal
47 launch session "Default Session"
48 set current_session to current session
50 write text "${command}"
55 elif [[ "$the_app" == 'iTerm2' ]]; then
57 tell application "iTerm2"
59 create tab with default profile
60 tell current session to write text "${command}"
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
70 tell application "System Events"
71 keystroke "${command}"
72 key code 36 #(presses enter)
76 echo "$0: unsupported terminal app: $the_app" >&2
81 function vsplit_tab() {
82 local command="cd \\\"$PWD\\\"; clear"
83 (( $# > 0 )) && command="${command}; $*"
85 local the_app=$(_omz_macos_get_frontmost_app)
87 if [[ "$the_app" == 'iTerm' ]]; then
89 -- tell application "iTerm" to activate
90 tell application "System Events"
92 tell menu item "Split Vertically With Current Profile" of menu "Shell" of menu bar item "Shell" of menu bar 1
96 keystroke "${command} \n"
99 elif [[ "$the_app" == 'iTerm2' ]]; then
101 tell application "iTerm2"
102 tell current session of first window
103 set newSession to (split vertically with same profile)
105 write text "${command}"
111 elif [[ "$the_app" == 'Hyper' ]]; then
112 osascript >/dev/null <<EOF
113 tell application "System Events"
115 tell menu item "Split Vertically" of menu "Shell" of menu bar 1
120 keystroke "${command} \n"
124 echo "$0: unsupported terminal app: $the_app" >&2
129 function split_tab() {
130 local command="cd \\\"$PWD\\\"; clear"
131 (( $# > 0 )) && command="${command}; $*"
133 local the_app=$(_omz_macos_get_frontmost_app)
135 if [[ "$the_app" == 'iTerm' ]]; then
136 osascript 2>/dev/null <<EOF
137 tell application "iTerm" to activate
139 tell application "System Events"
141 tell menu item "Split Horizontally With Current Profile" of menu "Shell" of menu bar item "Shell" of menu bar 1
145 keystroke "${command} \n"
148 elif [[ "$the_app" == 'iTerm2' ]]; then
150 tell application "iTerm2"
151 tell current session of first window
152 set newSession to (split horizontally with same profile)
154 write text "${command}"
160 elif [[ "$the_app" == 'Hyper' ]]; then
161 osascript >/dev/null <<EOF
162 tell application "System Events"
164 tell menu item "Split Horizontally" of menu "Shell" of menu bar 1
169 keystroke "${command} \n"
173 echo "$0: unsupported terminal app: $the_app" >&2
179 osascript 2>/dev/null <<EOF
180 tell application "Finder"
181 return POSIX path of (insertion location as alias)
187 osascript 2>/dev/null <<EOF
189 tell application "Finder" to set the_selection to selection
190 set item_count to count the_selection
191 repeat with item_index from 1 to count the_selection
192 if item_index is less than item_count then set the_delimiter to "\n"
193 if item_index is item_count then set the_delimiter to ""
194 set output to output & ((item item_index of the_selection as alias)'s POSIX path) & the_delimiter
208 dirname $(osascript 2>/dev/null <<EOF
209 if application "Xcode" is running then
210 tell application "Xcode"
211 return path of active workspace document
222 function quick-look() {
223 (( $# > 0 )) && qlmanage -p $* &>/dev/null &
226 function man-preview() {
227 # Don't let Preview.app steal focus if the man page doesn't exist
228 man -w "$@" &>/dev/null && man -t "$@" | open -f -a Preview || man "$@"
230 compdef _man man-preview
232 function vncviewer() {
236 # Remove .DS_Store files recursively in a directory, default .
237 function rmdsstore() {
238 find "${@:-.}" -type f -name .DS_Store -delete
241 # Erases purgeable disk space with 0s on the selected disk
242 function freespace(){
243 if [[ -z "$1" ]]; then
244 echo "Usage: $0 <disk>"
245 echo "Example: $0 /dev/disk1s1"
247 echo "Possible disks:"
248 df -h | awk 'NR == 1 || /^\/dev\/disk/'
252 echo "Cleaning purgeable files from disk: $1 ...."
253 diskutil secureErase freespace 0 $1
258 disks=("${(@f)"$(df | awk '/^\/dev\/disk/{ printf $1 ":"; for (i=9; i<=NF; i++) printf $i FS; print "" }')"}")
259 _describe disks disks
262 compdef _freespace freespace
264 # Music / iTunes control function
265 source "${0:h:A}/music"
267 # Spotify control function
268 source "${0:h:A}/spotify"