Just when I thought iTunes/Music couldn't get any worse. That horrible thing is just full of surprises, isn't it.
I've come up with a kind-of solution. It's a horrible hack. I should be ashamed of myself. Below, please find a script that polls Music every 5 seconds to find out if something is playing. If something is playing, are we within the last 10 seconds of the track? If we are, the script will increment the play count by one, and set the last played date to now.
I couldn't see a way to "listen" to Music for a "track finishing" event or something like that, so that's why I'm polling the app. There may be some weird edge cases that happen if you pause playback within the last 10 seconds of a track, and similarly for tracks that are less than 10 seconds long. But hey - it's a horrible hack. If you've got ideas for improvement, please share here as well.
# how many seconds to repeat (must be less than beforeEndOfTrack)
set repeatTime to 5
# how many seconds before end of track to set played
set beforeEndOfTrack to 10
tell application "System Events"
repeat
delay repeatTime
if exists process "Music" then
tell application "Music"
if player state is playing then
set currentSong to current track
set songDuration to duration of currentSong
set playerPosition to player position
if playerPosition > (songDuration - beforeEndOfTrack) then
set played count of currentSong to (played count of currentSong) + 1
set played date of currentSong to (current date)
delay (songDuration - playerPosition)
end if
end if
end tell
else
exit repeat
end if
end repeat
end tell
Copy/paste into Script Editor, and run the script. I've had it going so far all day, and it's worked great. I hope this is helpful for people.