Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/bash
- # Quick bash script that allows you to grab the currently playing song from VLC/Spotify Web
- # Nothing special, use it, modify it or distribute it
- # This program uses sed make sure it's installed
- # Arguments: [Source(VLC|SpotifyWeb)]
- # Config vars
- prefix="Song: " # This is prefixed in the output file
- updateInterval=2 # In seconds
- outputFile="./nowPlaying"
- webBrowser="Google Chrome"
- # Uncomment this line if you use FireFox
- #webBrowser="Mozilla Firefox"
- if [ "$#" -ne 1 ]; then # Missing or too many arguments
- echo "Arguments: [Source(VLC|SpotifyWeb)]"
- echo "Spotify Web assumes you're using chrome, modify the script for FireFox etc."
- exit
- fi
- # Functions
- xwinlength=21 # The length of this string "xwininfo: Window id: "
- function grabResourceID() {
- xwinoutput="$(xwininfo | grep 'Window id:')" # Grab the line containing the resource id from xwininfo
- cutoutput="${xwinoutput:xwinlength}" # Format it
- resourceID="${cutoutput%% *}"
- }
- function grabXOutput() {
- xOutput="$(xwininfo -tree -root | grep $resourceID)" # Get all open windows and output only the one with the resource id of the chosen player
- }
- function removeFileEndings() {
- # Get rid of file endings
- # Those are the common ones I could think of
- songClean="$(sed 's/\.mp3//g' <<< $songName)"
- songClean="$(sed 's/\.aac//g' <<< $songClean)"
- songClean="$(sed 's/\.wav//g' <<< $songClean)"
- songClean="$(sed 's/\.ogg//g' <<< $songClean)"
- songClean="$(sed 's/\.flac//g' <<< $songClean)"
- songClean="$(sed 's/\.ogg//g' <<< $songClean)"
- songClean="$(sed 's/\.aiff//g' <<< $songClean)"
- songClean="$(sed 's/\.alac//g' <<< $songClean)"
- }
- echo "Click in the window of your chosen player..."
- grabResourceID
- echo "Resource ID is $resourceID"
- # Defaults
- playerSuffix=" - VLC media player\"" # That '"' at the end is from xwininfo
- sourceType="$1" # Available: VLC | Spotify web, For Spotify web you have to have the Spotify web player tab selected!
- # Leave these
- resIDLength=${#resourceID}
- totalLength=$((resIDLength + 7))
- playerSuffixLength=${#playerSuffix}
- oldOutput="NULL"
- if [ "$sourceType" == "VLC" ]; then
- echo "Using formatting for VLC..."
- while true; do
- grabXOutput
- if [ "$xOutput" == "$oldOutput" ]; then # Song hasn't changed
- sleep $updateInterval
- continue
- fi
- oldOutput=$xOutput
- cutColon="${xOutput%%:*}" # Cut everything after the colon off
- cutResourceId="${cutColon:totalLength}" # Cut off ResourceID
- songName="${cutResourceId:0:${#cutResourceId}-$playerSuffixLength}" # Cut off the Suffix eg. ' - VLC Media Player'
- removeFileEndings
- echo "Now Playing: $songClean"
- echo "$prefix$songClean">$outputFile
- sleep $updateInterval
- done
- else
- echo "Using formatting for Spotify Web..."
- playerSuffix=" - Spotify - $webBrowser\""
- playerSuffixLength=${#playerSuffix}
- while true; do
- grabXOutput
- if [ "$xOutput" == "$oldOutput" ]; then # Song hasn't changed
- sleep $updateInterval
- continue
- fi
- if [[ $xOutput == *"▶"* ]]; then # When Spotify is playing cut off the ▶
- totalLength=$((resIDLength + 9)) # Spotify prefixes a '▶' and a whitespace
- else
- totalLength=$((resIDLength + 7)) # Otherwise don't cut if off
- fi
- oldOutput=$xOutput
- cutColon="${xOutput%%:*}"
- cutResourceId="${cutColon:totalLength}"
- songName="${cutResourceId:0:${#cutResourceId}-$playerSuffixLength}"
- echo "Now Playing: $songName"
- echo "$prefix$songName">$outputFile
- sleep $updateInterval
- done
- fi
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement