]> src.twobees.de Git - dotfiles.git/blob - stow/oh-my-zsh/.oh-my-zsh/plugins/macos/macos.plugin.zsh
b540eecb5ff8573c82b0d2287cbfd85d272678a4
[dotfiles.git] / stow / oh-my-zsh / .oh-my-zsh / plugins / macos / macos.plugin.zsh
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
6 # Open the current directory in a Finder window
7 alias ofd='open_command $PWD'
8
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"
12
13 # Bluetooth restart
14 function btrestart() {
15   sudo kextunload -b com.apple.iokit.BroadcomBluetoothHostControllerUSBTransport
16   sudo kextload -b com.apple.iokit.BroadcomBluetoothHostControllerUSBTransport
17 }
18
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)
23     end tell
24 EOF
25 }
26
27 function tab() {
28   # Must not have trailing semicolon, for iTerm compatibility
29   local command="cd \\\"$PWD\\\"; clear"
30   (( $# > 0 )) && command="${command}; $*"
31
32   local the_app=$(_omz_macos_get_frontmost_app)
33
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   else
76     echo "$0: unsupported terminal app: $the_app" >&2
77     return 1
78   fi
79 }
80
81 function vsplit_tab() {
82   local command="cd \\\"$PWD\\\"; clear"
83   (( $# > 0 )) && command="${command}; $*"
84
85   local the_app=$(_omz_macos_get_frontmost_app)
86
87   if [[ "$the_app" == 'iTerm' ]]; then
88     osascript <<EOF
89       -- tell application "iTerm" to activate
90       tell application "System Events"
91         tell process "iTerm"
92           tell menu item "Split Vertically With Current Profile" of menu "Shell" of menu bar item "Shell" of menu bar 1
93             click
94           end tell
95         end tell
96         keystroke "${command} \n"
97       end tell
98 EOF
99   elif [[ "$the_app" == 'iTerm2' ]]; then
100     osascript <<EOF
101       tell application "iTerm2"
102         tell current session of first window
103           set newSession to (split vertically with same profile)
104           tell newSession
105             write text "${command}"
106             select
107           end tell
108         end tell
109       end tell
110 EOF
111   elif [[ "$the_app" == 'Hyper' ]]; then
112     osascript >/dev/null <<EOF
113     tell application "System Events"
114       tell process "Hyper"
115         tell menu item "Split Vertically" of menu "Shell" of menu bar 1
116           click
117         end tell
118       end tell
119       delay 1
120       keystroke "${command} \n"
121     end tell
122 EOF
123   else
124     echo "$0: unsupported terminal app: $the_app" >&2
125     return 1
126   fi
127 }
128
129 function split_tab() {
130   local command="cd \\\"$PWD\\\"; clear"
131   (( $# > 0 )) && command="${command}; $*"
132
133   local the_app=$(_omz_macos_get_frontmost_app)
134
135   if [[ "$the_app" == 'iTerm' ]]; then
136     osascript 2>/dev/null <<EOF
137       tell application "iTerm" to activate
138
139       tell application "System Events"
140         tell process "iTerm"
141           tell menu item "Split Horizontally With Current Profile" of menu "Shell" of menu bar item "Shell" of menu bar 1
142             click
143           end tell
144         end tell
145         keystroke "${command} \n"
146       end tell
147 EOF
148   elif [[ "$the_app" == 'iTerm2' ]]; then
149     osascript <<EOF
150       tell application "iTerm2"
151         tell current session of first window
152           set newSession to (split horizontally with same profile)
153           tell newSession
154             write text "${command}"
155             select
156           end tell
157         end tell
158       end tell
159 EOF
160   elif [[ "$the_app" == 'Hyper' ]]; then
161     osascript >/dev/null <<EOF
162     tell application "System Events"
163       tell process "Hyper"
164         tell menu item "Split Horizontally" of menu "Shell" of menu bar 1
165           click
166         end tell
167       end tell
168       delay 1
169       keystroke "${command} \n"
170     end tell
171 EOF
172   else
173     echo "$0: unsupported terminal app: $the_app" >&2
174     return 1
175   fi
176 }
177
178 function pfd() {
179   osascript 2>/dev/null <<EOF
180     tell application "Finder"
181       return POSIX path of (insertion location as alias)
182     end tell
183 EOF
184 }
185
186 function pfs() {
187   osascript 2>/dev/null <<EOF
188     set output to ""
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
195     end repeat
196 EOF
197 }
198
199 function cdf() {
200   cd "$(pfd)"
201 }
202
203 function pushdf() {
204   pushd "$(pfd)"
205 }
206
207 function pxd() {
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
212       end tell
213     end if
214 EOF
215 )
216 }
217
218 function cdx() {
219   cd "$(pxd)"
220 }
221
222 function quick-look() {
223   (( $# > 0 )) && qlmanage -p $* &>/dev/null &
224 }
225
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 "$@"
229 }
230 compdef _man man-preview
231
232 function vncviewer() {
233   open vnc://$@
234 }
235
236 # Remove .DS_Store files recursively in a directory, default .
237 function rmdsstore() {
238   find "${@:-.}" -type f -name .DS_Store -delete
239 }
240
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"
246     echo
247     echo "Possible disks:"
248     df -h | awk 'NR == 1 || /^\/dev\/disk/'
249     return 1
250   fi
251
252   echo "Cleaning purgeable files from disk: $1 ...."
253   diskutil secureErase freespace 0 $1
254 }
255
256 _freespace() {
257   local -a disks
258   disks=("${(@f)"$(df | awk '/^\/dev\/disk/{ printf $1 ":"; for (i=9; i<=NF; i++) printf $i FS; print "" }')"}")
259   _describe disks disks
260 }
261
262 compdef _freespace freespace
263
264 # Music / iTunes control function
265 source "${0:h:A}/music"
266
267 # Spotify control function
268 source "${0:h:A}/spotify"