gpt4 book ai didi

applescript - 在 Applescript 中监控 Spotify 轨道变化?

转载 作者:行者123 更新时间:2023-12-04 03:07:40 26 4
gpt4 key购买 nike

我正在尝试通过 Spotify 的 Applescript 库找出检测音轨变化的最佳方法。到目前为止,我已经尝试检查玩家位置——如果它等于 0,则它是一个新轨道,并且再次出现 Growl 通知。 (如果有人重新开始歌曲等,这通常不起作用)

我想知道是否更合理的方法是运行一个空闲的 iTunes 脚本并每隔几秒钟检查当前轨道名称的变化。我担心这可能有点像 pig 的内存。我也无法使此代码起作用。

tell application "System Events"
-- checks for instance of Growl and Spotify, does not run if both programs are not active
set isRunning to ¬
(count of (every process whose name is "Growl")) > 0
(count of (every process whose name is "Spotify")) > 0
end tell

--establish empty variable to be filled by song title later
global latest_song
set latest_song to ""

on idle
tell application "Spotify"
if player state is playing then
copy name of current track to current_tracks_name
-- runs match between last and current song titles
if current_tracks_name is not latest_song then
copy current_tracks_name to latest_song
set who to artist of current track
set onwhat to album of current track
tell application "Growl"
-- Make a list of all the notification types
-- that this script will ever send:
set the allNotificationsList to ¬
{"SpotifyNewTrack"}

-- Make a list of the notifications
-- that will be enabled by default.
-- Those not enabled by default can be enabled later
-- in the 'Applications' tab of the growl prefpane.
set the enabledNotificationsList to ¬
{"SpotifyNewTrack"}

-- Register our script with growl.
-- You can optionally (as here) set a default icon
-- for this script's notifications.
register as application ¬
"Spotify" all notifications allNotificationsList ¬
default notifications enabledNotificationsList ¬
icon of application "Spotify"

-- Send a Notification...
notify with name ¬
"SpotifyNewTrack" title ¬
current_tracks_name description ¬
who application name "Spotify"
end tell
end if
return 5
end if
end tell
end idle

这可能有点复杂,但任何帮助表示赞赏。

最佳答案

我已经编写了一个小脚本,因为 Spotify 不支持 Growl 1.3。

编程不是很好,但我认为,通知很花哨 ;)

set delay_time to 1

-- register growl
tell application "Growl"
set the allNotificationsList to {"SpotifyNewTrack"}
set the enabledNotificationsList to {"SpotifyNewTrack"}
register as application ¬
"Spotify" all notifications allNotificationsList ¬
default notifications enabledNotificationsList ¬
icon of application "Spotify"
end tell

set myspace to " "
set myspace2 to myspace & myspace
set myspace3 to myspace & myspace & myspace

set old_track_id to ""
set old_state to ""

repeat
try

set boolStarted to false
set boolPaused to false

-- main code
tell application "Spotify" to set track_id to id of current track
tell application "Spotify" to set theState to player state as string


if ((old_state is "stopped") or (old_state is "paused")) and theState is "playing" then set boolStarted to true
if (old_state is "playing") and theState is "paused" then set boolPaused to true

set old_state to theState

if (old_track_id is not track_id) or boolStarted or boolPaused then

tell application "Spotify"
set theTitle to name of the current track
set theArtist to artist of the current track
set theAlbum to album of the current track
set theDuration to duration of the current track
set theCount to played count of the current track
set theStarred to starred of the current track
set thePopularity to popularity of the current track
set theCover to artwork of the current track
set thePosition to player position
end tell


if theStarred then
set stringStarred to "♥" & myspace
else
set stringStarred to ""
end if

set stringPopularity to ""
repeat (thePopularity / 20 as integer) times
set stringPopularity to stringPopularity & "✭"
end repeat
repeat (5 - (thePopularity / 20 as integer)) times
set stringPopularity to stringPopularity & "✩"
end repeat

set theMinutes to round (theDuration / 60) rounding down
set theSeconds to round (theDuration - (theMinutes * 60))
if theSeconds < 10 then set theSeconds to "0" & theSeconds

set thePosMinutes to round (thePosition / 60) rounding down
set thePosSeconds to thePosition - (thePosMinutes * 60) as integer
if thePosSeconds < 10 then set thePosSeconds to "0" & thePosSeconds
set stringPos to thePosMinutes & ":" & thePosSeconds



if boolStarted or boolPaused then
set thePosMinutes to round (thePosition / 60) rounding down
set thePosSeconds to thePosition - (thePosMinutes * 60) as integer
if thePosSeconds < 10 then set thePosSeconds to "0" & thePosSeconds
set stringPos to thePosMinutes & ":" & thePosSeconds
end if


set growlTitle to theArtist & ":" & myspace2 & theTitle
set growlMessage to theAlbum & myspace & "||" & myspace & theMinutes & ":" & theSeconds & ¬
myspace & stringStarred & stringPopularity & " (" & thePopularity & ")" & myspace & theCount & "x"

if boolStarted then set growlTitle to growlTitle & myspace3 & "▶"
if boolPaused then set growlTitle to growlTitle & myspace3 & "❚❚"


-- save cover art
set f to open for access "Macintosh HD:tmp:spotify-artwork.tiff" with write permission
set eof of f to 0
write theCover to f
close access f


set thePercentage to (20 * thePosition / theDuration) as integer

set stringPercentage to ""

repeat (thePercentage - 1) times
set stringPercentage to stringPercentage & "·"
end repeat

set stringPercentage to stringPercentage & "◆"

if thePercentage is 0 then
repeat (19 - thePercentage) times
set stringPercentage to stringPercentage & "·"
end repeat
else
repeat (20 - thePercentage) times
set stringPercentage to stringPercentage & "·"
end repeat
end if

if boolStarted or boolPaused then
set growlTitle to growlTitle & myspace3 & stringPos & myspace3 & stringPercentage
end if


tell application "Growl" to notify with name ¬
"SpotifyNewTrack" title growlTitle description growlMessage ¬
application name ¬
"Spotify" image from location "/tmp/spotify-artwork.tiff"


end if

set old_track_id to track_id


end try
delay delay_time
end repeat

关于applescript - 在 Applescript 中监控 Spotify 轨道变化?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7642467/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com