macos.plugin.zsh
8603 bytes
1# Handle $0 according to the standard:
2# https://zdharma-continuum.github.io/Zsh-100-Commits-Club/Zsh-Plugin-Standard.html
30="${${ZERO:-${0:#$ZSH_ARGZERO}}:-${(%):-%N}}"
40="${${(M)0:#/*}:-$PWD/$0}"
5
6# Open in Finder the directories passed as arguments, or the current directory if
7# no directories are passed
8function ofd {
9 if (( ! $# )); then
10 open_command $PWD
11 else
12 open_command $@
13 fi
14}
15
16# Show/hide hidden files in the Finder
17alias showfiles="defaults write com.apple.finder AppleShowAllFiles -bool true && killall Finder"
18alias hidefiles="defaults write com.apple.finder AppleShowAllFiles -bool false && killall Finder"
19
20# Bluetooth restart
21function btrestart() {
22 sudo kextunload -b com.apple.iokit.BroadcomBluetoothHostControllerUSBTransport
23 sudo kextload -b com.apple.iokit.BroadcomBluetoothHostControllerUSBTransport
24}
25
26function _omz_macos_get_frontmost_app() {
27 osascript 2>/dev/null <<EOF
28 tell application "System Events"
29 name of first item of (every process whose frontmost is true)
30 end tell
31EOF
32}
33
34function tab() {
35 # Must not have trailing semicolon, for iTerm compatibility
36 local command="cd \\\"$PWD\\\"; clear"
37 (( $# > 0 )) && command="${command}; $*"
38
39 local the_app=$(_omz_macos_get_frontmost_app)
40
41 if [[ "$the_app" == 'Terminal' ]]; then
42 # Discarding stdout to quash "tab N of window id XXX" output
43 osascript >/dev/null <<EOF
44 tell application "System Events"
45 tell process "Terminal" to keystroke "t" using command down
46 end tell
47 tell application "Terminal" to do script "${command}" in front window
48EOF
49 elif [[ "$the_app" == 'iTerm' ]]; then
50 osascript <<EOF
51 tell application "iTerm"
52 set current_terminal to current terminal
53 tell current_terminal
54 launch session "Default Session"
55 set current_session to current session
56 tell current_session
57 write text "${command}"
58 end tell
59 end tell
60 end tell
61EOF
62 elif [[ "$the_app" == 'iTerm2' ]]; then
63 osascript <<EOF
64 tell application "iTerm2"
65 tell current window
66 create tab with default profile
67 tell current session to write text "${command}"
68 end tell
69 end tell
70EOF
71 elif [[ "$the_app" == 'Hyper' ]]; then
72 osascript >/dev/null <<EOF
73 tell application "System Events"
74 tell process "Hyper" to keystroke "t" using command down
75 end tell
76 delay 1
77 tell application "System Events"
78 keystroke "${command}"
79 key code 36 #(presses enter)
80 end tell
81EOF
82
83 elif [[ "$the_app" == 'Tabby' ]]; then
84 osascript >/dev/null <<EOF
85 tell application "System Events"
86 tell process "Tabby" to keystroke "t" using command down
87 end tell
88EOF
89 elif [[ "$the_app" == 'ghostty' ]]; then
90 osascript >/dev/null <<EOF
91 tell application "System Events"
92 tell process "Ghostty" to keystroke "t" using command down
93 end tell
94EOF
95 else
96 echo "$0: unsupported terminal app: $the_app" >&2
97 return 1
98 fi
99}
100
101function vsplit_tab() {
102 local command="cd \\\"$PWD\\\"; clear"
103 (( $# > 0 )) && command="${command}; $*"
104
105 local the_app=$(_omz_macos_get_frontmost_app)
106
107 if [[ "$the_app" == 'iTerm' ]]; then
108 osascript <<EOF
109 -- tell application "iTerm" to activate
110 tell application "System Events"
111 tell process "iTerm"
112 tell menu item "Split Vertically With Current Profile" of menu "Shell" of menu bar item "Shell" of menu bar 1
113 click
114 end tell
115 end tell
116 keystroke "${command} \n"
117 end tell
118EOF
119 elif [[ "$the_app" == 'iTerm2' ]]; then
120 osascript <<EOF
121 tell application "iTerm2"
122 tell current session of first window
123 set newSession to (split vertically with same profile)
124 tell newSession
125 write text "${command}"
126 select
127 end tell
128 end tell
129 end tell
130EOF
131 elif [[ "$the_app" == 'Hyper' ]]; then
132 osascript >/dev/null <<EOF
133 tell application "System Events"
134 tell process "Hyper"
135 tell menu item "Split Vertically" of menu "Shell" of menu bar 1
136 click
137 end tell
138 end tell
139 delay 1
140 keystroke "${command} \n"
141 end tell
142EOF
143 elif [[ "$the_app" == 'Tabby' ]]; then
144 osascript >/dev/null <<EOF
145 tell application "System Events"
146 tell process "Tabby" to keystroke "D" using command down
147 end tell
148EOF
149 elif [[ "$the_app" == 'ghostty' ]]; then
150 osascript >/dev/null <<EOF
151 tell application "System Events"
152 tell process "Ghostty" to keystroke "D" using command down
153 end tell
154EOF
155 else
156 echo "$0: unsupported terminal app: $the_app" >&2
157 return 1
158 fi
159}
160
161function split_tab() {
162 local command="cd \\\"$PWD\\\"; clear"
163 (( $# > 0 )) && command="${command}; $*"
164
165 local the_app=$(_omz_macos_get_frontmost_app)
166
167 if [[ "$the_app" == 'iTerm' ]]; then
168 osascript 2>/dev/null <<EOF
169 tell application "iTerm" to activate
170
171 tell application "System Events"
172 tell process "iTerm"
173 tell menu item "Split Horizontally With Current Profile" of menu "Shell" of menu bar item "Shell" of menu bar 1
174 click
175 end tell
176 end tell
177 keystroke "${command} \n"
178 end tell
179EOF
180 elif [[ "$the_app" == 'iTerm2' ]]; then
181 osascript <<EOF
182 tell application "iTerm2"
183 tell current session of first window
184 set newSession to (split horizontally with same profile)
185 tell newSession
186 write text "${command}"
187 select
188 end tell
189 end tell
190 end tell
191EOF
192 elif [[ "$the_app" == 'Hyper' ]]; then
193 osascript >/dev/null <<EOF
194 tell application "System Events"
195 tell process "Hyper"
196 tell menu item "Split Horizontally" of menu "Shell" of menu bar 1
197 click
198 end tell
199 end tell
200 delay 1
201 keystroke "${command} \n"
202 end tell
203EOF
204 elif [[ "$the_app" == 'Tabby' ]]; then
205 osascript >/dev/null <<EOF
206 tell application "System Events"
207 tell process "Tabby" to keystroke "d" using command down
208 end tell
209EOF
210 elif [[ "$the_app" == 'ghostty' ]]; then
211 osascript >/dev/null <<EOF
212 tell application "System Events"
213 tell process "Ghostty" to keystroke "d" using command down
214 end tell
215EOF
216 else
217 echo "$0: unsupported terminal app: $the_app" >&2
218 return 1
219 fi
220}
221
222function pfd() {
223 osascript 2>/dev/null <<EOF
224 tell application "Finder"
225 return POSIX path of (insertion location as alias)
226 end tell
227EOF
228}
229
230function pfs() {
231 osascript 2>/dev/null <<EOF
232 set output to ""
233 tell application "Finder" to set the_selection to selection
234 set item_count to count the_selection
235 repeat with item_index from 1 to count the_selection
236 if item_index is less than item_count then set the_delimiter to "\n"
237 if item_index is item_count then set the_delimiter to ""
238 set output to output & ((item item_index of the_selection as alias)'s POSIX path) & the_delimiter
239 end repeat
240EOF
241}
242
243function cdf() {
244 cd "$(pfd)"
245}
246
247function pushdf() {
248 pushd "$(pfd)"
249}
250
251function pxd() {
252 dirname $(osascript 2>/dev/null <<EOF
253 if application "Xcode" is running then
254 tell application "Xcode"
255 return path of active workspace document
256 end tell
257 end if
258EOF
259)
260}
261
262function cdx() {
263 cd "$(pxd)"
264}
265
266function quick-look() {
267 (( $# > 0 )) && qlmanage -p $* &>/dev/null &
268}
269
270function man-preview() {
271 [[ $# -eq 0 ]] && >&2 echo "Usage: $0 command1 [command2 ...]" && return 1
272
273 local page
274 for page in "${(@f)"$(command man -w $@)"}"; do
275 command mandoc -Tpdf $page | open -f -a Preview
276 done
277}
278compdef _man man-preview
279
280function vncviewer() {
281 open vnc://$@
282}
283
284# Remove .DS_Store files recursively in a directory, default .
285function rmdsstore() {
286 find "${@:-.}" -type f -name .DS_Store -delete
287}
288
289# Erases purgeable disk space with 0s on the selected disk
290function freespace(){
291 if [[ -z "$1" ]]; then
292 echo "Usage: $0 <disk>"
293 echo "Example: $0 /dev/disk1s1"
294 echo
295 echo "Possible disks:"
296 df -h | awk 'NR == 1 || /^\/dev\/disk/'
297 return 1
298 fi
299
300 echo "Cleaning purgeable files from disk: $1 ...."
301 diskutil secureErase freespace 0 $1
302}
303
304_freespace() {
305 local -a disks
306 disks=("${(@f)"$(df | awk '/^\/dev\/disk/{ printf $1 ":"; for (i=9; i<=NF; i++) printf $i FS; print "" }')"}")
307 _describe disks disks
308}
309
310compdef _freespace freespace
311
312# Music / iTunes control function
313source "${0:h:A}/music"
314
315# Spotify control function
316source "${0:h:A}/spotify"