Created
July 3, 2020 20:56
-
-
Save typkrft/4af28d5b5e115421f9c0198e301adf95 to your computer and use it in GitHub Desktop.
Apple Music Song Names to Text File
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
on getMusicInfo() | |
set theName to "" | |
set theState to "" | |
try | |
with timeout of 2 seconds | |
tell application "Music" | |
if it is running then | |
set theName to name of current track as text | |
set theState to player state as text | |
end if | |
end tell | |
end timeout | |
end try | |
set theList to {theName, theState} | |
return theList | |
end getMusicInfo | |
on writeNameToFile(theName) | |
do shell script "echo " & quoted form of theName & " >> ~/songNames.txt && sed -i '' '/^$/d' ~/songNames.txt" | |
log "Wrote " & theName & " to ~/songNames.txt" | |
end writeNameToFile | |
on main() | |
set theState to item 2 of getMusicInfo() | |
set currentTrack to item 1 of getMusicInfo() | |
set newTrack to "" | |
if theState is "playing" then | |
writeNameToFile(currentTrack) | |
end if | |
repeat while true | |
set theState to item 2 of getMusicInfo() | |
log {currentTrack, newTrack} | |
if theState is not "playing" then | |
delay 2 | |
else if theState is "playing" and currentTrack is not newTrack then | |
writeNameToFile(newTrack) | |
set currentTrack to item 1 of getMusicInfo() | |
set newTrack to currentTrack | |
delay 2 | |
else if theState is "playing" and currentTrack is newTrack then | |
delay 2 | |
set newTrack to item 1 of getMusicInfo() | |
end if | |
end repeat | |
end main | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment