kermitd
Aktives Mitglied
- Dabei seit
- 16.02.2006
- Beiträge
- 7.184
- Reaktionspunkte
- 2.617
Ich bin mal so frei gewesen und hab Spotify-Support hinzugefügt. Den letzten Abschnitt aus adjustIconForAlbumArtworkOfCurrentTrack habe ich in updateTrackInfo_ verschoben. Womit latestTrackFlag entfällt.
NC wird nicht mehr ständig neugestartet solange sich das Album nicht ändert. Außerdem überprüft das Skript zu Beginn, ob iTunes bzw. Spotify bereits im Vordergrund sind, und verzichtet in diesem Fall auf eine Benachrichtigung.
NC wird nicht mehr ständig neugestartet solange sich das Album nicht ändert. Außerdem überprüft das Skript zu Beginn, ob iTunes bzw. Spotify bereits im Vordergrund sind, und verzichtet in diesem Fall auf eine Benachrichtigung.
Code:
-- main.scpt
-- Cocoa-AppleScript Applet
--
-- ----------------------------------------------------------------------
--
-- OS X Mountain Lion song notifications with album cover art support
--
-- ----------------------------------------------------------------------
--
-- v.pre-2012082501
-- Latest changes:
-- - Adds support for Spotify
-- - Notifications are silenced if the music player is currently the frontmost app
-- - NotificationCenter process is not restarted anymore when tracks from the same album are played
-- - solves issues that occur when the user is rapidly changing tracks
--
--
global currentTrackName, currentTrackAlbum, currentTrackArtist, currentTrackArtworkCount
property latestAlbum : ""
set dnc to current application's NSDistributedNotificationCenter's defaultCenter
tell dnc to addObserver_selector_name_object_(me, "updateTrackInfo:", "com.apple.iTunes.playerInfo", missing value)
tell dnc to addObserver_selector_name_object_(me, "updateTrackInfo:", "com.spotify.client.PlaybackStateChanged", missing value)
on updateTrackInfo_(aNotification)
tell current application's NSDictionary to set trackInfo to alloc()'s initWithDictionary_(aNotification's userInfo)
tell application "System Events" to set frontmostApp to name of the first process whose frontmost is true
if aNotification's |name| as text = "com.apple.iTunes.playerInfo" then
set notifier to "iTunes"
else if aNotification's |name| as text = "com.spotify.client.PlaybackStateChanged" then
set notifier to "Spotify"
end if
if (the frontmostApp is the notifier) or (trackInfo's valueForKey_("Player State") as text) is not "playing" then return
set currentTrackAlbum to trackInfo's valueForKey_("Album") as text
set currentTrackArtist to trackInfo's valueForKey_("Artist") as text
set currentTrackName to trackInfo's valueForKey_("Name") as text
set albumHasChanged to false
-- adjust app icon if the album has changed
if currentTrackAlbum ≠ latestAlbum then
set albumHasChanged to true
if notifier = "iTunes" then
set currentTrackArtworkCount to trackInfo's valueForKey_("Artwork Count") as integer
else if notifier = "Spotify" then
set currentTrackArtworkCount to trackInfo's valueForKey_("Has Artwork") as integer
end if
adjustIconForAlbumArtworkOfCurrentTrackPlayingOn(notifier)
-- only continue if the track that triggered the notification is still the latest one
if notifier = "iTunes" then
tell application "iTunes" to set actualCurrentTrackName to (the name of the current track)
else if notifier = "Spotify" then
tell application "Spotify" to set actualCurrentTrackName to (the name of the current track)
end if
if actualCurrentTrackName ≠ currentTrackName then return
set latestAlbum to currentTrackAlbum
end if
-- (have to) restart NotificationCenter for the changed Icon to be registered
if (albumHasChanged) then
do shell script "killAll NotificationCenter;"
delay 0.5
repeat while (appIsRunning("NotificationCenter") is false)
delay 0.2
end repeat
end if
sendMessage(currentTrackName, currentTrackArtist, currentTrackAlbum)
end updateTrackInfo_
on adjustIconForAlbumArtworkOfCurrentTrackPlayingOn(appName)
set thePath to current application's NSBundle's mainBundle's resourcePath as text
set thefile to thePath & "/Icon.png"
if currentTrackArtworkCount ≠ 0 then
if (appName = "iTunes") then
tell application "iTunes" to set currentTrackArtwork to raw data of artwork 1 of current track
else if (appName = "Spotify") then
tell application "Spotify" to set currentTrackArtwork to artwork of current track
end if
try
set thefilehandle to open for access (thefile) with write permission
write currentTrackArtwork to thefilehandle starting at 0
close access thefilehandle
on error err
close access thefilehandle
end try
do shell script "sips --resampleHeightWidth 128 128 " & quoted form of thefile & " -o " & quoted form of thefile & "; sips -s format icns " & quoted form of thefile & " -o '" & thePath & "/Applet.icns';"
else
do shell script "rm '" & thePath & "/Applet.icns'; cp '" & thePath & "/Applet2.icns' '" & thePath & "/Applet.icns'"
end if
end adjustIconForAlbumArtworkOfCurrentTrackPlayingOn
-- dispatch a message to ML's NC
on sendMessage(myTitle, mySubtitle, myContent)
tell current application's NSUserNotification to set thisNote to alloc()'s init()
set thisNote's title to myTitle
set thisNote's subtitle to mySubtitle
set thisNote's informativeText to myContent
tell current application's NSUserNotificationCenter's defaultUserNotificationCenter to deliverNotification_(thisNote)
end sendMessage
-- check whether a process exists
on appIsRunning(appName)
tell application "System Events" to (name of processes) contains appName
end appIsRunning
Zuletzt bearbeitet: