|
property lastTrack : "" -- Last known track (name & artist) |
|
property lastState : "stopped" -- Last known playback state |
|
property lastRunning : false -- Last known running state of Apple Music |
|
|
|
|
|
on clearAdiumStatus() |
|
tell application "Adium" |
|
if it is running then |
|
set status message of every account to "" |
|
end if |
|
end tell |
|
end clearAdiumStatus |
|
|
|
on quit |
|
clearAdiumStatus() |
|
display dialog "Goodbye!" |
|
continue quit |
|
end quit |
|
|
|
|
|
on run |
|
display notification "Script is running. Enjoy your music." with title "AdiumMusicStatus" |
|
idle -- triggers the idle handler |
|
end run |
|
|
|
on idle |
|
-- Core loop logic |
|
set statusMessage to "" -- Default to empty status |
|
set currentTrack to "" |
|
set currentState to "stopped" |
|
set currentRunning to false |
|
|
|
-- Check Apple Music for current track and state |
|
tell application "Music" |
|
if it is running then |
|
set currentRunning to true |
|
try |
|
set playerStateRaw to player state |
|
if playerStateRaw is playing then |
|
set currentState to "playing" |
|
else if playerStateRaw is paused then |
|
set currentState to "paused" |
|
else if playerStateRaw is stopped then |
|
set currentState to "stopped" |
|
else |
|
set currentState to "unknown" |
|
end if |
|
|
|
if currentState is "playing" then |
|
set songName to name of current track |
|
set songArtist to artist of current track |
|
set currentTrack to songName & " by " & songArtist |
|
set statusMessage to "Listening to: " & currentTrack |
|
end if |
|
on error errMsg |
|
display dialog "Error accessing Music: " & errMsg |
|
set currentTrack to "" |
|
set statusMessage to "" |
|
end try |
|
end if |
|
end tell |
|
|
|
-- Check for changes |
|
set hasChanged to false |
|
if currentRunning is not lastRunning then |
|
set hasChanged to true |
|
else if currentState is not lastState then |
|
set hasChanged to true |
|
else if currentTrack is not lastTrack then |
|
set hasChanged to true |
|
end if |
|
|
|
-- Update Adium only if something changed |
|
if hasChanged then |
|
tell application "Adium" |
|
if it is running then |
|
repeat with a in accounts |
|
set status message of a to statusMessage |
|
end repeat |
|
end if |
|
end tell |
|
|
|
set lastTrack to currentTrack |
|
set lastState to currentState |
|
set lastRunning to currentRunning |
|
end if |
|
|
|
return 5 -- Wait 5 seconds before next idle cycle |
|
end idle |