d47447a5e63715ae6ab6c2f46924dc8766c8e746

Author
Marc Cornellà <marc.cornella@live.com>
Committer
GitHub <noreply@github.com>
Date

Message

osx: refactor plugin and add a few features (#9026)

Co-authored-by: Capybara <git.capybara@gmail.com>
Co-authored-by: drootang <drootang@users.noreply.github.com>
Co-authored-by: Augusto Souza <augustorsouza@gmail.com>
Co-authored-by: PatrBal <44707588+PatrBal@users.noreply.github.com>

Diff

This diff is truncated to protect this page.

  1diff --git a/plugins/osx/README.md b/plugins/osx/README.md
  2index 50e9e7f21d17f9dcaa9a6d09b3011e7d82140bbc..ecc9327d0abf33038929fcd423ad28a4a4108dc2 100644
  3--- a/plugins/osx/README.md
  4+++ b/plugins/osx/README.md
  5@@ -22,14 +22,17 @@ Original author: [Sorin Ionescu](https://github.com/sorin-ionescu)
  6 | `pfs`           | Return the current Finder selection                   |
  7 | `cdf`           | `cd` to the current Finder directory                  |
  8 | `pushdf`        | `pushd` to the current Finder directory               |
  9+| `pxd`           | Return the current Xcode project directory            |
 10+| `cdx`           | `cd` to the current Xcode project directory           |
 11 | `quick-look`    | Quick-Look a specified file                           |
 12 | `man-preview`   | Open a specified man page in Preview app              |
 13-| `showfiles`     | Show hidden files                                     |
 14-| `hidefiles`     | Hide the hidden files                                 |
 15-| `itunes`        | DEPRECATED. Use `music` from macOS Catalina on        |
 16+| `showfiles`     | Show hidden files in Finder                           |
 17+| `hidefiles`     | Hide the hidden files in Finder                       |
 18+| `itunes`        | _DEPRECATED_. Use `music` from macOS Catalina on      |
 19 | `music`         | Control Apple Music. Use `music -h` for usage details |
 20 | `spotify`       | Control Spotify and search by artist, album, track…   |
 21 | `rmdsstore`     | Remove .DS\_Store files recursively in a directory    |
 22+| `btrestart`     | Restart the Bluetooth daemon                          |
 23 
 24 ## Acknowledgements
 25 
 26diff --git a/plugins/osx/_security b/plugins/osx/_security
 27new file mode 100644
 28index 0000000000000000000000000000000000000000..e4ed585ac3017980d848da22332a826126403f3a
 29--- /dev/null
 30+++ b/plugins/osx/_security
 31@@ -0,0 +1,90 @@
 32+#compdef security
 33+
 34+local -a _1st_arguments
 35+_1st_arguments=(
 36+  'help:Show all commands, or show usage for a command'
 37+  'list-keychains:Display or manipulate the keychain search list'
 38+  'default-keychain:Display or set the default keychain'
 39+  'login-keychain:Display or set the login keychain'
 40+  'create-keychain:Create keychains and add them to the search list'
 41+  'delete-keychain:Delete keychains and remove them from the search list'
 42+  'lock-keychain:Lock the specified keychain'
 43+  'lock-keychain:Unlock the specified keychain'
 44+  'set-keychain-settings:Set settings for a keychain'
 45+  'set-keychain-password:Set password for a keychain'
 46+  'show-keychain-info:Show the settings for keychain'
 47+  'dump-keychain:Dump the contents of one or more keychains'
 48+  'create-keypair:Create an asymmetric key pair'
 49+  'add-generic-password:Add a generic password item'
 50+  'add-internet-password:Add an internet password item'
 51+  'add-certificates:Add certificates to a keychain'
 52+  'find-generic-password:Find a generic password item'
 53+  'delete-generic-password:Delete a generic password item'
 54+  'find-internet-password:Find an internet password item'
 55+  'delete-internet-password:Delete an internet password item'
 56+  'find-certificate:Find a certificate item'
 57+  'find-identity:Find an identity certificate + private key'
 58+  'delete-certificate:Delete a certificate from a keychain'
 59+  'set-identity-preference:Set the preferred identity to use for a service'
 60+  'get-identity-preference:Get the preferred identity to use for a service'
 61+  'create-db:Create a db using the DL'
 62+  'export:Export items from a keychain'
 63+  'import:Import items into a keychain'
 64+  'cms:Encode or decode CMS messages'
 65+  'install-mds:MDS database'
 66+  'add-trusted-cert:Add trusted certificates:'
 67+  'remove-trusted-cert:Remove trusted certificates:'
 68+  'dump-trust-settings:Display contents of trust settings'
 69+  'user-trust-settings-enable:Display or manipulate user-level trust settings'
 70+  'trust-settings-export:Export trust settings'
 71+  'trust-settings-import:Import trust settings'
 72+  'verify-cert:Verify certificates:'
 73+  'authorize:Perform authorization operations'
 74+  'authorizationdb:Make changes to the authorization policy database'
 75+  'execute-with-privileges:Execute tool with privileges'
 76+  'leaks:Run /usr/bin/leaks on this process'
 77+  'error:Display a descriptive message for the given error codes:'
 78+  'create-filevaultmaster-keychain:"Create a keychain containing a key pair for FileVault recovery use'
 79+)
 80+_arguments '*:: :->command'
 81+
 82+if (( CURRENT == 1 )); then
 83+  _describe -t commands "security command" _1st_arguments
 84+  return
 85+fi
 86+
 87+case "$words[1]" in
 88+  find-(generic|internet)-password)
 89+    _values \
 90+      'Usage: find-[internet/generic]-password [-a account] [-s server] [options...] [-g] [keychain...]' \
 91+      '-a[Match "account" string]' \
 92+      '-c[Match "creator" (four-character code)]' \
 93+      '-C[Match "type" (four-character code)]' \
 94+      '-D[Match "kind" string]' \
 95+      '-G[Match "value" string (generic attribute)]' \
 96+      '-j[Match "comment" string]' \
 97+      '-l[Match "label" string]' \
 98+      '-s[Match "service" string]' \
 99+      '-g[Display the password for the item found]' \
100+      '-w[Display only the password on stdout]' ;;
101+  add-(generic|internet)-password)
102+    _values \
103+      'Usage: add-[internet/generic]-password [-a account] [-s server] [-w password] [options...] [-A|-T appPath] [keychain]]' \
104+      '-a[Specify account name (required)]' \
105+      '-c[Specify item creator (optional four-character code)]' \
106+      '-C[Specify item type (optional four-character code)]' \
107+      '-d[Specify security domain string (optional)]' \
108+      '-D[Specify kind (default is "Internet password")]' \
109+      '-j[Specify comment string (optional)]' \
110+      '-l[Specify label (if omitted, server name is used as default label)]' \
111+      '-p[Specify path string (optional)]' \
112+      '-P[Specify port number (optional)]' \
113+      '-r[Specify protocol (optional four-character SecProtocolType, e.g. "http", "ftp ")]' \
114+      '-s[Specify server name (required)]' \
115+      '-t[Specify authentication type (as a four-character SecAuthenticationType, default is "dflt")]' \
116+      '-w[Specify password to be added]' \
117+      '-A[Allow any application to access this item without warning (insecure, not recommended!)]' \
118+      '-T[Specify an application which may access this item (multiple -T options are allowed)]' \
119+      '-U[Update item if it already exists (if omitted, the item cannot already exist) ]' \
120+      'utils)]' ;;
121+esac
122diff --git a/plugins/osx/music b/plugins/osx/music
123new file mode 100644
124index 0000000000000000000000000000000000000000..50566797b4425daec3304f314e52fbbb30fe363b
125--- /dev/null
126+++ b/plugins/osx/music
127@@ -0,0 +1,170 @@
128+#!/usr/bin/env zsh
129+
130+function music itunes() {
131+  local APP_NAME=Music sw_vers=$(sw_vers -productVersion 2>/dev/null)
132+
133+  autoload is-at-least
134+  if [[ -z "$sw_vers" ]] || is-at-least 10.15 $sw_vers; then
135+    if [[ $0 = itunes ]]; then
136+      echo >&2 The itunes function name is deprecated. Use \'music\' instead.
137+      return 1
138+    fi
139+  else
140+    APP_NAME=iTunes
141+  fi
142+
143+  local opt=$1 playlist=$2
144+  (( $# > 0 )) && shift
145+  case "$opt" in
146+    launch|play|pause|stop|rewind|resume|quit)
147+      ;;
148+    mute)
149+      opt="set mute to true"
150+      ;;
151+    unmute)
152+      opt="set mute to false"
153+      ;;
154+    next|previous)
155+      opt="$opt track"
156+      ;;
157+    vol)
158+      local new_volume volume=$(osascript -e "tell application \"$APP_NAME\" to get sound volume")
159+      if [[ $# -eq 0 ]]; then
160+        echo "Current volume is ${volume}."
161+        return 0
162+      fi
163+      case $1 in
164+        up) new_volume=$((volume + 10 < 100 ? volume + 10 : 100)) ;;
165+        down) new_volume=$((volume - 10 > 0 ? volume - 10 : 0)) ;;
166+        <0-100>) new_volume=$1 ;;
167+        *) echo "'$1' is not valid. Expected <0-100>, up or down."
168+           return 1 ;;
169+      esac
170+      opt="set sound volume to ${new_volume}"
171+      ;;
172+    playlist)
173+      # Inspired by: https://gist.github.com/nakajijapan/ac8b45371064ae98ea7f
174+      if [[ -n "$playlist" ]]; then
175+        osascript 2>/dev/null <<EOF
176+          tell application "$APP_NAME"
177+            set new_playlist to "$playlist" as string
178+            play playlist new_playlist
179+          end tell
180+EOF
181+        if [[ $? -eq 0 ]]; then
182+          opt="play"
183+        else
184+          opt="stop"
185+        fi
186+      else
187+        opt="set allPlaylists to (get name of every playlist)"
188+      fi
189+      ;;
190+    playing|status)
191+      local currenttrack currentartist state=$(osascript -e "tell application \"$APP_NAME\" to player state as string")
192+      if [[ "$state" = "playing" ]]; then
193+        currenttrack=$(osascript -e "tell application \"$APP_NAME\" to name of current track as string")
194+        currentartist=$(osascript -e "tell application \"$APP_NAME\" to artist of current track as string")
195+        echo -E "Listening to ${fg[yellow]}${currenttrack}${reset_color} by ${fg[yellow]}${currentartist}${reset_color}"
196+      else
197+        echo "$APP_NAME is $state"
198+      fi
199+      return 0
200+      ;;
201+    shuf|shuff|shuffle)
202+      # The shuffle property of current playlist can't be changed in iTunes 12,
203+      # so this workaround uses AppleScript to simulate user input instead.
204+      # Defaults to toggling when no options are given.
205+      # The toggle option depends on the shuffle button being visible in the Now playing area.
206+      # On and off use the menu bar items.
207+      local state=$1
208+
209+      if [[ -n "$state" && "$state" != (on|off|toggle) ]]; then
210+        print "Usage: $0 shuffle [on|off|toggle]. Invalid option."
211+        return 1
212+      fi
213+
214+      case "$state" in
215+        on|off)
216+          # Inspired by: https://stackoverflow.com/a/14675583
217+          osascript >/dev/null 2>&1 <<EOF
218+            tell application "System Events" to perform action "AXPress" of (menu item "${state}" of menu "Shuffle" of menu item "Shuffle" of menu "Controls" of menu bar item "Controls" of menu bar 1 of application process "iTunes" )
219+EOF
220+          return 0
221+          ;;
222+        toggle|*)
223+          osascript >/dev/null 2>&1 <<EOF
224+            tell application "System Events" to perform action "AXPress" of (button 2 of process "iTunes"'s window "iTunes"'s scroll area 1)
225+EOF
226+          return 0
227diff --git a/plugins/osx/osx.plugin.zsh b/plugins/osx/osx.plugin.zsh
228index 03e9c1c8c3a8be8d0576207280c5c543893460ed..7842e9f733fa8ddd479bdff58352b5db27ff2917 100644
229--- a/plugins/osx/osx.plugin.zsh
230+++ b/plugins/osx/osx.plugin.zsh
231@@ -1,15 +1,22 @@
232 # Open the current directory in a Finder window
233 alias ofd='open_command $PWD'
234 
235+# Show/hide hidden files in the Finder
236+alias showfiles="defaults write com.apple.finder AppleShowAllFiles -bool true && killall Finder"
237+alias hidefiles="defaults write com.apple.finder AppleShowAllFiles -bool false && killall Finder"
238+
239+# Bluetooth restart
240+function btrestart() {
241+  sudo kextunload -b com.apple.iokit.BroadcomBluetoothHostControllerUSBTransport
242+  sudo kextload -b com.apple.iokit.BroadcomBluetoothHostControllerUSBTransport
243+}
244+
245 function _omz_osx_get_frontmost_app() {
246-  local the_app=$(
247-    osascript 2>/dev/null <<EOF
248-      tell application "System Events"
249-        name of first item of (every process whose frontmost is true)
250-      end tell
251+  osascript 2>/dev/null <<EOF
252+    tell application "System Events"
253+      name of first item of (every process whose frontmost is true)
254+    end tell
255 EOF
256-  )
257-  echo "$the_app"
258 }
259 
260 function tab() {
261@@ -27,7 +34,6 @@ function tab() {
262       end tell
263       tell application "Terminal" to do script "${command}" in front window
264 EOF
265-
266   elif [[ "$the_app" == 'iTerm' ]]; then
267     osascript <<EOF
268       tell application "iTerm"
269@@ -41,32 +47,29 @@ EOF
270         end tell
271       end tell
272 EOF
273-
274   elif [[ "$the_app" == 'iTerm2' ]]; then
275-      osascript <<EOF
276-        tell application "iTerm2"
277-          tell current window
278-            create tab with default profile
279-            tell current session to write text "${command}"
280-          end tell
281+    osascript <<EOF
282+      tell application "iTerm2"
283+        tell current window
284+          create tab with default profile
285+          tell current session to write text "${command}"
286         end tell
287+      end tell
288 EOF
289   elif [[ "$the_app" == 'Hyper' ]]; then
290     osascript >/dev/null <<EOF
291-          tell application "System Events"
292-            tell process "Hyper" to keystroke "t" using command down
293-          end tell
294-          delay 1
295-          tell application "System Events"
296-              keystroke "${command}"
297-              key code 36  #(presses enter)
298-            end tell
299+      tell application "System Events"
300+        tell process "Hyper" to keystroke "t" using command down
301+      end tell
302+      delay 1
303+      tell application "System Events"
304+        keystroke "${command}"
305+        key code 36  #(presses enter)
306+      end tell
307 EOF
308-
309   else
310-    echo "tab: unsupported terminal app: $the_app"
311-    false
312-
313+    echo "$0: unsupported terminal app: $the_app" >&2
314+    return 1
315   fi
316 }
317 
318@@ -79,7 +82,6 @@ function vsplit_tab() {
319   if [[ "$the_app" == 'iTerm' ]]; then
320     osascript <<EOF
321       -- tell application "iTerm" to activate
322-
323       tell application "System Events"
324         tell process "iTerm"
325           tell menu item "Split Vertically With Current Profile" of menu "Shell" of menu bar item "Shell" of menu bar 1
326@@ -89,37 +91,33 @@ function vsplit_tab() {
327         keystroke "${command} \n"
328       end tell
329 EOF
330-