Pill
Aktives Mitglied
Thread Starter
- Dabei seit
- 21.02.2011
- Beiträge
- 3.279
- Reaktionspunkte
- 463
Seit Erscheinen von OS X Mountain Lion und dem Notification Center sind einige Tools (viele davon basieren auf AppleScript) erschienen, die den aktuellen Titel von iTunes anzeigen. Alle scheitern aber bisher daran, das Album Cover anzuzeigen. Das hat einen einfachen Grund: Es ist nicht vorgesehen, dass beliebige Bilder angezeigt werden, sondern es wird immer das Icon des jeweiligen Programms angezeigt. Die einzige Möglichkeit das Album Cover anzuzeigen ist es also das Icon zu ändern. Das ist in AppleScript relativ einfach zu lösen, schnell taucht jedoch ein weiteres Problem auf: Ist ein Programm einmal im Notification Center registriert, wird das Programmicon nicht mehr aktualisiert. Da es keine Möglichkeit gibt per AppleScript ein Programm aus dem Notification Center zu löschen, muss dies manuell in einer SQL-Datenbank geschehen, die sich im Ordner ~/Library/Application Support/NotificationCenter/ befindet.
Wie ihr schon seht, ist das ganze keine sehr saubere Lösung, eine andere habe ich bisher allerdings nicht gefunden. Die Meldung wird übrigens über Cocoa-AppleScript abgesetzt, vielen Dank an dieser Stelle an kermitd, der das ganze hier beschrieben hat.
Kommen wir zum Eingemachten, dem Quellcode:
Code:
[FONT=Verdana][size=2][color=#4C4D4D]-- main.scpt[/color]
[color=#4C4D4D]-- Cocoa-AppleScript Applet[/color]
[color=#4C4D4D]--[/color]
[color=#4C4D4D]-- Copyright 2011 {Your Company}. All rights reserved.[/color]
[color=#4C4D4D]-- This is the main script for a Cocoa-AppleScript Applet.[/color]
[color=#4C4D4D]-- You can put the usual script applet handlers here.[/color]
[b]set[/b] [color=#3F7F00]dnc[/color] [b]to[/b] [color=#4A1E7F]current application[/color]'s [color=#3F7F00]NSDistributedNotificationCenter[/color]'s [color=#3F7F00]defaultCenter[/color]
[b]tell[/b] [color=#3F7F00]dnc[/color] [b]to[/b] [color=#3F7F00]addObserver_selector_name_object_[/color]([b]me[/b], "updateTrackInfo", "com.apple.iTunes.playerInfo", [color=#0000FF][i]missing value[/i][/color])
[b]global[/b] [color=#3F7F00]currentTrack[/color], [color=#3F7F00]currentTrackName[/color], [color=#3F7F00]currentTrackAlbum[/color], [color=#3F7F00]currentTrackArtist[/color][color=#7F007F]
[/color]
[b]on[/b] [color=#3F7F00]updateTrackInfo[/color]()
[b]tell[/b] [color=#0000FF][i]application[/i][/color] "iTunes"
[b]if[/b] [color=#6C04D4]player state[/color] [b]is[/b] [b]not[/b] [color=#4A1E7F]playing[/color] [b]then[/b]
[b]return[/b]
[b]end[/b] [b]if[/b]
[b]end[/b] [b]tell[/b]
[color=#3F7F00]fetchiTunesTrackInfo[/color]()
[color=#3F7F00]adjustIconForAlbumArtworkOfCurrentTrack[/color]()
[color=#3F7F00]sendMessage[/color]([color=#3F7F00]currentTrackName[/color], [color=#3F7F00]currentTrackArtist[/color], [color=#3F7F00]currentTrackAlbum[/color])
[b]end[/b] [color=#3F7F00]updateTrackInfo[/color]
[b]on[/b] [color=#3F7F00]fetchiTunesTrackInfo[/color]()
[b]try[/b]
[b]tell[/b] [color=#0000FF][i]application[/i][/color] "iTunes"
[b]set[/b] [color=#3F7F00]currentTrack[/color] [b]to[/b] [b]the[/b] [color=#6C04D4]current track[/color]
[b]set[/b] [color=#3F7F00]currentTrackName[/color] [b]to[/b] [b]the[/b] [color=#6C04D4]name[/color] [b]of[/b] [b]the[/b] [color=#3F7F00]currentTrack[/color]
[b]set[/b] [color=#3F7F00]currentTrackAlbum[/color] [b]to[/b] [b]the[/b] [color=#6C04D4]album[/color] [b]of[/b] [b]the[/b] [color=#3F7F00]currentTrack[/color]
[b]set[/b] [color=#3F7F00]currentTrackArtist[/color] [b]to[/b] [b]the[/b] [color=#6C04D4]artist[/color] [b]of[/b] [b]the[/b] [color=#3F7F00]currentTrack[/color]
[b]end[/b] [b]tell[/b]
[b]end[/b] [b]try[/b]
[b]end[/b] [color=#3F7F00]fetchiTunesTrackInfo[/color]
[b]on[/b] [color=#3F7F00]adjustIconForAlbumArtworkOfCurrentTrack[/color]()
[b]tell[/b] [color=#0000FF][i]application[/i][/color] "iTunes"
[color=#0016B0][b]do shell script[/b][/color] "sqlite3 ~/Library/Application\\ Support/NotificationCenter/*.db \"DELETE from app_info where bundleid is 'com.apple.ScriptEditor.id.Now-Playing'\" && killAll NotificationCenter;"
[b]set[/b] [color=#3F7F00]thePath[/color] [b]to[/b] (([color=#0016B0][b]path to[/b][/color] [color=#4A1E7F]current application[/color]) [b]as[/b] [color=#0000FF][i]text[/i][/color])
[b]set[/b] [color=#3F7F00]thefile[/color] [b]to[/b] ([color=#3F7F00]thePath[/color] & "Contents:Resources:Icon.png" [b]as[/b] [color=#0000FF][i]alias[/i][/color])
[b]try[/b]
[b]set[/b] [color=#3F7F00]thefilehandle[/color] [b]to[/b] [color=#0016B0][b]open for access[/b][/color] [color=#3F7F00]thefile[/color] [b]with[/b] [color=#0016B0]write permission[/color]
[color=#0016B0][b]write[/b][/color] ([b]get[/b] [color=#6C04D4]raw data[/color] [b]of[/b] [color=#0000FF][i]artwork[/i][/color] 1 [b]of[/b] [color=#6C04D4]current track[/color]) [color=#0016B0]to[/color] [color=#3F7F00]thefilehandle[/color] [color=#0016B0]starting at[/color] 0
[color=#0016B0][b]close access[/b][/color] [color=#3F7F00]thefilehandle[/color]
[b]set[/b] [color=#3F7F00]theCommand[/color] [b]to[/b] "sips --resampleHeightWidth 128 128 " & [color=#6C04D4]quoted form[/color] [b]of[/b] [color=#6C04D4]POSIX path[/color] [b]of[/b] [color=#3F7F00]thefile[/color] & " -o " & [color=#6C04D4]quoted form[/color] [b]of[/b] [color=#6C04D4]POSIX path[/color] [b]of[/b] [color=#3F7F00]thefile[/color] & "; sips -s format icns " & [color=#6C04D4]quoted form[/color] [b]of[/b] [color=#6C04D4]POSIX path[/color] [b]of[/b] [color=#3F7F00]thefile[/color] & " -o '" & [color=#6C04D4]POSIX path[/color] [b]of[/b] [color=#3F7F00]thePath[/color] & "Contents/Resources/Applet.icns';"
[color=#0016B0][b]do shell script[/b][/color] [color=#3F7F00]theCommand[/color]
[b]on[/b] [b]error[/b] [color=#3F7F00]err[/color]
[color=#0016B0][b]close access[/b][/color] [color=#3F7F00]thefilehandle[/color]
[b]set[/b] [color=#3F7F00]cmd[/color] [b]to[/b] "rm '" & [color=#6C04D4]POSIX path[/color] [b]of[/b] [color=#3F7F00]thePath[/color] & "Contents/Resources/Applet.icns'; cp '" & [color=#6C04D4]POSIX path[/color] [b]of[/b] [color=#3F7F00]thePath[/color] & "Contents/Resources/Applet2.icns' '" & [color=#6C04D4]POSIX path[/color] [b]of[/b] [color=#3F7F00]thePath[/color] & "Contents/Resources/Applet.icns'"
[color=#0016B0][b]do shell script[/b][/color] [color=#3F7F00]cmd[/color]
[color=#4C4D4D]--display dialog cmd[/color]
[color=#4C4D4D]--tell current application to quit[/color]
[b]end[/b] [b]try[/b]
[b]end[/b] [b]tell[/b]
[b]tell[/b] [color=#0000FF][i]application[/i][/color] "System Events"
[b]repeat[/b] [b]until[/b] ([color=#0000FF][b]exists[/b][/color] [color=#0000FF][i]process[/i][/color] "rm") & ([color=#0000FF][b]exists[/b][/color] [color=#0000FF][i]process[/i][/color] "cp") [b]is[/b] {[color=#4A1E7F]false[/color], [color=#4A1E7F]false[/color]}
[color=#0016B0][b]delay[/b][/color] 0.1
[b]end[/b] [b]repeat[/b]
[b]end[/b] [b]tell[/b]
[b]end[/b] [color=#3F7F00]adjustIconForAlbumArtworkOfCurrentTrack[/color]
[b]on[/b] [color=#3F7F00]sendMessage[/color]([color=#3F7F00]myTitle[/color], [color=#3F7F00]mySubtitle[/color], [color=#3F7F00]myContent[/color])
[b]tell[/b] [color=#4A1E7F]current application[/color]'s [color=#3F7F00]NSUserNotification[/color] [b]to[/b] [b]set[/b] [color=#3F7F00]thisNote[/color] [b]to[/b] [color=#3F7F00]alloc[/color]()'s [color=#3F7F00]init[/color]()
[b]set[/b] [color=#3F7F00]thisNote[/color]'s [color=#3F7F00]title[/color] [b]to[/b] [color=#3F7F00]myTitle[/color]
[b]set[/b] [color=#3F7F00]thisNote[/color]'s [color=#3F7F00]subtitle[/color] [b]to[/b] [color=#3F7F00]mySubtitle[/color]
[b]set[/b] [color=#3F7F00]thisNote[/color]'s [color=#3F7F00]informativeText[/color] [b]to[/b] [color=#3F7F00]myContent[/color]
[b]tell[/b] [color=#4A1E7F]current application[/color]'s [color=#3F7F00]NSUserNotificationCenter[/color]'s [color=#3F7F00]defaultUserNotificationCenter[/color] [b]to[/b] [color=#3F7F00]deliverNotification_[/color]([color=#3F7F00]thisNote[/color])
[b]end[/b] [color=#3F7F00]sendMessage[/color][/size][/FONT]
Da es mit dem Quellcode allein nicht getan ist, gibt es das Programm als ganzes noch hier zum Download. Bedenkt aber, dass sich das Programm noch im Alphastatus befindet. Im Moment kann ich nur garantieren, dass es bei mir funktioniert.
Natürlich bin ich gespannt auf eure Verbesserungsvorschläge und Anregungen.
Zuletzt bearbeitet: