Skip to content

Commit

Permalink
Replace iTunes with Music
Browse files Browse the repository at this point in the history
  • Loading branch information
fredrikhl committed Sep 26, 2020
1 parent 8e92ae0 commit fe63f60
Showing 1 changed file with 15 additions and 17 deletions.
32 changes: 15 additions & 17 deletions songstat.applescript
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
script PlayerStatus

property isPlaying : false
property currentSong : ""
property currentArtist : ""
property currentPos : 0
property currentLength : 1

-- reset object - subclasses should fetch data and call setStatus
on updateStatus()
setStatus({false, "", "", 0, 1})
end updateStatus

-- set script object (playing, song, artist, pos, len)
on setStatus(results)
set my isPlaying to item 1 of results
Expand All @@ -19,7 +19,7 @@ script PlayerStatus
set my currentPos to item 4 of results as number
set my currentLength to item 5 of results as number
end setStatus

-- Test if appName is running
on isRunning(appName)
tell application "System Events"
Expand All @@ -28,30 +28,30 @@ script PlayerStatus
-- if application appName is running then return true
return false
end isRunning

-- Get current track progress in %
on getProgress()
if not isPlaying then return 0
-- else
return (round ((my currentPos) / (my currentLength)) * 100)
end getProgress

-- return player info as string: "<artist>|<song>|<perentage>"
on getSerialized()
if not isPlaying then return ""
return my currentArtist & "|" & my currentSong & "|" & getProgress()
end getSerialized

end script

script iTunesStatus

property parent : PlayerStatus

-- update and return player state
on updateStatus()
if isRunning("iTunes") then
tell application "iTunes"
if isRunning("Music") then
tell application "Music"
if player state is playing then
continue setStatus({true, get name of current track, get artist of current track, get player position, get duration of current track})
else
Expand All @@ -62,13 +62,13 @@ script iTunesStatus
continue updateStatus()
end if
end updateStatus

end script

script SpotifyStatus

property parent : PlayerStatus

-- update and return player state
on updateStatus()
if isRunning("Spotify") then
Expand All @@ -83,7 +83,7 @@ script SpotifyStatus
continue updateStatus()
end if
end updateStatus

end script

set output to ""
Expand All @@ -92,5 +92,3 @@ repeat with status in {iTunesStatus, SpotifyStatus}
tell status to updateStatus()
if isPlaying of status then return getSerialized() of status as string
end repeat

return ""

0 comments on commit fe63f60

Please sign in to comment.