-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsongstat.applescript
94 lines (77 loc) · 2.76 KB
/
songstat.applescript
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
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
set my currentSong to item 2 of results
set my currentArtist to item 3 of results
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"
if (name of every process) contains appName then return true
end tell
-- 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("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
set my isPlaying to false
end if
end tell
else
continue updateStatus()
end if
end updateStatus
end script
script SpotifyStatus
property parent : PlayerStatus
-- update and return player state
on updateStatus()
if isRunning("Spotify") then
tell application "Spotify"
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
set my isPlaying to false
end if
end tell
else
continue updateStatus()
end if
end updateStatus
end script
set output to ""
repeat with status in {iTunesStatus, SpotifyStatus}
tell status to updateStatus()
if isPlaying of status then return getSerialized() of status as string
end repeat