diff options
Diffstat (limited to 'plugins/osx')
-rw-r--r-- | plugins/osx/README.md | 4 | ||||
-rw-r--r-- | plugins/osx/osx.plugin.zsh | 55 | ||||
-rw-r--r-- | plugins/osx/spotify | 73 |
3 files changed, 110 insertions, 22 deletions
diff --git a/plugins/osx/README.md b/plugins/osx/README.md index 7c75c65f5..4595ee319 100644 --- a/plugins/osx/README.md +++ b/plugins/osx/README.md @@ -18,7 +18,7 @@ This application makes use of the following third party scripts: [shpotify](https://github.com/hnarayanan/shpotify) -Copyright (c) 2012–2017 [Harish Narayanan](https://harishnarayanan.org/). +Copyright (c) 2012–2019 [Harish Narayanan](https://harishnarayanan.org/). Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the @@ -56,6 +56,6 @@ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | `man-preview` | Open a specified man page in Preview app | | `showfiles` | Show hidden files | | `hidefiles` | Hide the hidden files | -| `itunes` | Control iTunes. User `itunes -h` for usage details | +| `itunes` | Control iTunes. Use `itunes -h` for usage details | | `spotify` | Control Spotify and search by artist, album, track… | | `rmdsstore` | Remove .DS\_Store files recursively in a directory | diff --git a/plugins/osx/osx.plugin.zsh b/plugins/osx/osx.plugin.zsh index 6a4b6eec4..eb3c4fb7a 100644 --- a/plugins/osx/osx.plugin.zsh +++ b/plugins/osx/osx.plugin.zsh @@ -51,6 +51,17 @@ EOF end tell end tell EOF + elif [[ "$the_app" == 'Hyper' ]]; then + osascript >/dev/null <<EOF + tell application "System Events" + tell process "Hyper" to keystroke "t" using command down + end tell + delay 1 + tell application "System Events" + keystroke "${command}" + key code 36 #(presses enter) + end tell +EOF else echo "tab: unsupported terminal app: $the_app" @@ -91,6 +102,19 @@ EOF end tell end tell EOF + + elif [[ "$the_app" == 'Hyper' ]]; then + osascript >/dev/null <<EOF + tell application "System Events" + tell process "Hyper" + tell menu item "Split Vertically" of menu "Shell" of menu bar 1 + click + end tell + end tell + delay 1 + keystroke "${command} \n" + end tell +EOF else echo "$0: unsupported terminal app: $the_app" >&2 @@ -132,6 +156,19 @@ EOF end tell EOF + elif [[ "$the_app" == 'Hyper' ]]; then + osascript >/dev/null <<EOF + tell application "System Events" + tell process "Hyper" + tell menu item "Split Horizontally" of menu "Shell" of menu bar 1 + click + end tell + end tell + delay 1 + keystroke "${command} \n" + end tell +EOF + else echo "$0: unsupported terminal app: $the_app" >&2 false @@ -199,7 +236,19 @@ function itunes() { opt="$opt track" ;; vol) - opt="set sound volume to $1" #$1 Due to the shift + local new_volume volume=$(osascript -e 'tell application "iTunes" to get sound volume') + if [[ $# -eq 0 ]]; then + echo "Current volume is ${volume}." + return 0 + fi + case $1 in + up) new_volume=$((volume + 10 < 100 ? volume + 10 : 100)) ;; + down) new_volume=$((volume - 10 > 0 ? volume - 10 : 0)) ;; + <0-100>) new_volume=$1 ;; + *) echo "'$1' is not valid. Expected <0-100>, up or down." + return 1 ;; + esac + opt="set sound volume to ${new_volume}" ;; playlist) # Inspired by: https://gist.github.com/nakajijapan/ac8b45371064ae98ea7f @@ -262,7 +311,7 @@ EOF echo "\tmute|unmute\tcontrol volume set" echo "\tnext|previous\tplay next or previous track" echo "\tshuf|shuffle [on|off|toggle]\tSet shuffled playback. Default: toggle. Note: toggle doesn't support the MiniPlayer." - echo "\tvol\tSet the volume, takes an argument from 0 to 100" + echo "\tvol [0-100|up|down]\tGet or set the volume. 0 to 100 sets the volume. 'up' / 'down' increases / decreases by 10 points. No argument displays current volume." echo "\tplaying|status\tShow what song is currently playing in iTunes." echo "\tplaylist [playlist name]\t Play specific playlist" echo "\thelp\tshow this message and exit" @@ -284,6 +333,6 @@ alias showfiles="defaults write com.apple.finder AppleShowAllFiles -bool true && alias hidefiles="defaults write com.apple.finder AppleShowAllFiles -bool false && killall Finder" # Remove .DS_Store files recursively in a directory, default . -rmdsstore() { +function rmdsstore() { find "${@:-.}" -type f -name .DS_Store -delete } diff --git a/plugins/osx/spotify b/plugins/osx/spotify index 2ab98d3a0..39f8e0437 100644 --- a/plugins/osx/spotify +++ b/plugins/osx/spotify @@ -1,7 +1,7 @@ #!/usr/bin/env bash function spotify() { -# Copyright (c) 2012--2017 Harish Narayanan <mail@harishnarayanan.org> +# Copyright (c) 2012--2019 Harish Narayanan <mail@harishnarayanan.org> # # Contains numerous helpful contributions from Jorge Colindres, Thomas # Pritchard, iLan Epstein, Gabriele Bonetti, Sean Heller, Eric Martin @@ -70,7 +70,7 @@ showHelp () { echo; echo " next # Skips to the next song in a playlist."; echo " prev # Returns to the previous song in a playlist."; - echo " replay # Replays the current track from the begining."; + echo " replay # Replays the current track from the beginning."; echo " pos <time> # Jumps to a time (in secs) in the current song."; echo " pause # Pauses (or resumes) Spotify playback."; echo " stop # Stops playback."; @@ -82,6 +82,9 @@ showHelp () { echo " vol [show] # Shows the current Spotify volume."; echo; echo " status # Shows the current player status."; + echo " status artist # Shows the currently playing artist."; + echo " status album # Shows the currently playing album."; + echo " status track # Shows the currently playing track."; echo; echo " share # Displays the current song's Spotify URL and URI." echo " share url # Displays the current song's Spotify URL and copies it to the clipboard." @@ -99,12 +102,21 @@ cecho(){ echo $bold$green"$1"$reset; } +showArtist() { + echo `osascript -e 'tell application "Spotify" to artist of current track as string'`; +} + +showAlbum() { + echo `osascript -e 'tell application "Spotify" to album of current track as string'`; +} + +showTrack() { + echo `osascript -e 'tell application "Spotify" to name of current track as string'`; +} + showStatus () { state=`osascript -e 'tell application "Spotify" to player state as string'`; cecho "Spotify is currently $state."; - artist=`osascript -e 'tell application "Spotify" to artist of current track as string'`; - album=`osascript -e 'tell application "Spotify" to album of current track as string'`; - track=`osascript -e 'tell application "Spotify" to name of current track as string'`; duration=`osascript -e 'tell application "Spotify" set durSec to (duration of current track / 1000) as text set tM to (round (durSec / 60) rounding down) as text @@ -128,14 +140,19 @@ showStatus () { end tell return nowAt'`; - echo -e $reset"Artist: $artist\nAlbum: $album\nTrack: $track \nPosition: $position / $duration"; + echo -e $reset"Artist: $(showArtist)\nAlbum: $(showAlbum)\nTrack: $(showTrack) \nPosition: $position / $duration"; } if [ $# = 0 ]; then showHelp; else + if [ ! -d /Applications/Spotify.app ] && [ ! -d $HOME/Applications/Spotify.app ]; then + echo "The Spotify application must be installed." + exit 1 + fi + if [ $(osascript -e 'application "Spotify" is running') = "false" ]; then - osascript -e 'tell application "Spotify" to activate' + osascript -e 'tell application "Spotify" to activate' || exit 1 sleep 2 fi fi @@ -160,7 +177,7 @@ while [ $# -gt 0 ]; do showAPIHelp; exit 1; fi - SHPOTIFY_CREDENTIALS=$(printf "${CLIENT_ID}:${CLIENT_SECRET}" | base64 | tr -d "\n"); + SHPOTIFY_CREDENTIALS=$(printf "${CLIENT_ID}:${CLIENT_SECRET}" | base64 | tr -d "\n"|tr -d '\r'); SPOTIFY_PLAY_URI=""; getAccessToken() { @@ -218,18 +235,18 @@ while [ $# -gt 0 ]; do results=$( \ curl -s -G $SPOTIFY_SEARCH_API --data-urlencode "q=$Q" -d "type=playlist&limit=10&offset=0" -H "Accept: application/json" -H "Authorization: Bearer ${SPOTIFY_ACCESS_TOKEN}" \ - | grep -E -o "spotify:user:[a-zA-Z0-9_]+:playlist:[a-zA-Z0-9]+" -m 10 \ + | grep -E -o "spotify:playlist:[a-zA-Z0-9]+" -m 10 \ ) count=$( \ - echo "$results" | grep -c "spotify:user" \ + echo "$results" | grep -c "spotify:playlist" \ ) if [ "$count" -gt 0 ]; then random=$(( $RANDOM % $count)); SPOTIFY_PLAY_URI=$( \ - echo "$results" | awk -v random="$random" '/spotify:user:[a-zA-Z0-9]+:playlist:[a-zA-Z0-9]+/{i++}i==random{print; exit}' \ + echo "$results" | awk -v random="$random" '/spotify:playlist:[a-zA-Z0-9]+/{i++}i==random{print; exit}' \ ) fi;; @@ -290,7 +307,7 @@ while [ $# -gt 0 ]; do "quit" ) cecho "Quitting Spotify."; osascript -e 'tell application "Spotify" to quit'; - exit 1 ;; + exit 0 ;; "next" ) cecho "Going to next track." ; osascript -e 'tell application "Spotify" to next track'; @@ -341,7 +358,7 @@ while [ $# -gt 0 ]; do echo " vol down # Decreases the volume by 10%."; echo " vol [amount] # Sets the volume to an amount between 0 and 100."; echo " vol # Shows the current Spotify volume."; - break + exit 1; fi osascript -e "tell application \"Spotify\" to set sound volume to $newvol"; @@ -360,7 +377,25 @@ while [ $# -gt 0 ]; do break ;; "status" ) - showStatus; + if [ $# != 1 ]; then + # There are additional arguments, a status subcommand + case $2 in + "artist" ) + showArtist; + break ;; + + "album" ) + showAlbum; + break ;; + + "track" ) + showTrack; + break ;; + esac + else + # status is the only param + showStatus; + fi break ;; "info" ) @@ -423,16 +458,20 @@ while [ $# -gt 0 ]; do cecho "Spotify URI: $uri"; echo -n $uri | pbcopy fi - break;; + break ;; "pos" ) cecho "Adjusting Spotify play position." osascript -e "tell application \"Spotify\" to set player position to $2"; - break;; + break ;; - "help" | * ) + "help" ) showHelp; break ;; + * ) + showHelp; + exit 1; + esac done } |