Skip to content

Instantly share code, notes, and snippets.

@typkrft
Last active July 5, 2020 02:32
Show Gist options
  • Save typkrft/c25b8413ca280c618d138645f2b3fc8a to your computer and use it in GitHub Desktop.
Save typkrft/c25b8413ca280c618d138645f2b3fc8a to your computer and use it in GitHub Desktop.
#!usr/bin/env zsh
# TODO
# Comment
# Fix Control Flow
# Button Select for Pywal instead of only automatic. maybe a flag for automatic.
# See if I can get it centered
# Maybe rewrite it in a less useless language
# Crashes when no album art.
# suppress output of some commands with : $()
# add flags for what you want to pywal, maybe detect what you have eg wal-discord, wal-vtop, pywalfox
applscriptFunction() {
osascript <<EOD
on getTrackName()
set theName to ""
try
with timeout of 2 seconds
tell application "Music"
if it is running then
set theName to name of current track
end if
end tell
end timeout
end try
-- log "The current track is " & theName
return theName
end getTrackName
on getAlbumArt()
-- get the raw bytes of the artwork into a var
tell application "Music" to tell artwork 1 of current track
set srcBytes to raw data
-- figure out the proper file extension
if format is «class PNG » then
set ext to ".png"
else
set ext to ".jpg"
end if
end tell
-- get the filename to ~/Desktop/cover.ext
set fileName to (((path to home folder) as text) & ".cache:cover" & ext)
-- write to file
set outFile to open for access file fileName with write permission
-- truncate the file
set eof outFile to 0
-- write the image bytes to the file
write srcBytes to outFile
close access outFile
-- log "Album Artwork Written"
return true
end getAlbumArt
on musicIsRunning()
set isRunning to false
try
with timeout of 2 seconds
tell application "Music"
if it is running then
set isRunning to true
end if
end tell
end timeout
end try
if isRunning is true then
-- log "Music is running"
return true
else
return false
end if
end musicIsRunning
on firstRun()
set forever to false
repeat until forever is true
-- log "First Run"
if musicIsRunning() is true and getTrackName() is not "" then
set firstTrack to getTrackName()
if firstTrack is not null then
getAlbumArt()
exit repeat
else
delay 2
end if
else
delay 2
end if
end repeat
return firstTrack
end firstRun
set currentTrack to firstRun()
-- log "made it"
set newTrack to getTrackName()
-- log newTrack
set forever to false
repeat while forever is false
-- log {"Current Track", currentTrack}
-- log {"New Track", newTrack}
if currentTrack is equal to newTrack then
set newTrack to getTrackName()
delay 2
else if newTrack is not equal to currentTrack and newTrack is not "" then
getAlbumArt()
do shell script "/bin/bash -s <<'EOF'
clear && /Users/brandon/.iterm2/imgcat /Users/brandon/.cache/cover.jpg
EOF"
set currentTrack to newTrack
set newTrack to getTrackName()
else
firstRun()
end if
end repeat
EOD
}
applscriptFunction &
tput civis
oscascriptProcess=$(pgrep -P $!)
# echo $oscascriptProcess
currentBytes=$(wc -c /Users/<USERNAME>/.cache/cover.*)
FILE=$(/bin/ls -t /Users/<USERNAME>/.cache/cover* | head -1)
if [[ -f $(echo $FILE*) ]]; then
clear && /Users/<USERNAME>/.iterm2/imgcat $FILE && : $(/usr/local/bin/wal -ni $FILE --saturate 1.0 && /usr/local/bin/pywalfox update) && osascript -e 'set theName to ""' -e 'set theArtist to ""' -e 'set theAlbum to ""' -e 'try' -e 'with timeout of 2 seconds' -e 'tell application "Music"' -e 'if it is running then' -e 'set theName to name of current track' -e 'set theArtist to artist of current track' -e 'set theAlbum to album of current track' -e 'end if' -e 'end tell' -e 'end timeout' -e 'end try' -e 'if theName is not "" then' -e 'return "Song: " & theName & "\n" & "By: " & theArtist & "\n" & "Album: " & theAlbum' -e 'else if theName is "" then' -e 'return ""' -e 'end if'
fi
while : ; do
FILE=$(/bin/ls -t /Users/<USERNAME>/.cache/cover* | head -1)
read -t 2 -n 1
if [ $? = 0 ] ; then
kill $oscascriptProcess &
clear
tput cnorm
exit ;
else
newBytes=$(wc -c $FILE)
if [ "$currentBytes" = "$newBytes" ]; then
newBytes=$(wc -c $FILE)
elif [ "$currentBytes" != "$newBytes" ]; then
currentBytes=$newBytes
clear && /Users/<USERNAME>/.iterm2/imgcat $FILE && : $(/usr/local/bin/wal -ni $FILE --saturate 1.0 && /usr/local/bin/pywalfox update) && osascript -e 'set theName to ""' -e 'set theArtist to ""' -e 'set theAlbum to ""' -e 'try' -e 'with timeout of 2 seconds' -e 'tell application "Music"' -e 'if it is running then' -e 'set theName to name of current track' -e 'set theArtist to artist of current track' -e 'set theAlbum to album of current track' -e 'end if' -e 'end tell' -e 'end timeout' -e 'end try' -e 'if theName is not "" then' -e 'return "Song: " & theName & "\n" & "By: " & theArtist & "\n" & "Album: " & theAlbum' -e 'else if theName is "" then' -e 'return ""' -e 'end if'
fi
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment