Sunday, January 21, 2018

#1 2013-12-22 01:17:16 pm

Joy
Member
From:: South Tirol
Registered: 2008-07-04
Posts: 547
Website

iTunes is growling

My contribution to Macscripter for Christmas. The script uses Growl to display the current playing song/genre in iTunes.
Would be nice to attach this Script to a Event deamon.


Applescript:

property img_list : {"jp2", "jpg", "jpeg", "tif", "tiff", "png", "gif", "pict", "QuickTime Image", "psd", "bmp", "tga"}

on run
   tell application "iTunes"
       set playing_track to current track
       set {the_artist, the_name, the_album, the_rating} to {artist of playing_track, name of playing_track, album of playing_track, rating of playing_track}
       try
           set {get_artwork, format_of} to {data, format} of artwork 1 of playing_track
           set correct_suffix to my suffix_format(format_of)
           set show_artwork to my gen_pict(get_artwork, the_name, correct_suffix)
           
       on error #no artwork
           set find_artwork to ("genre-" & genre of playing_track & ".jpg" as text)
           set show_artwork to ((path to applications folder as text) & "iTunes.app:Contents:Resources:" & find_artwork as text)
       end try
   end tell
   
   try
       set show_artwork to show_artwork as alias
   on error #no typical genre nor artwork found
       set show_artwork to ((path to applications folder as text) & "iTunes.app:Contents:Resources:iTunes.icns" as text) as alias
   end try
   #nicer ratings
   if the_rating is 0 then
       set the_stars to "-"
   else
       #transform numbers to stars
       repeat the_rating times
           set the_stars to the_stars & "★"
       end repeat
   end if
   
   #output
   my growl_step(the_artist, the_name & return & the_stars, show_artwork)
end run

on suffix_format(format_of)
   set format_of to format_of as text
   set dd to 0
   repeat with a in img_list
       set a to a as text
       set dd to dd + 1
       if a is in format_of then
           set my_format to a
           exit repeat
       end if
   end repeat
   #if a is "jpeg" then set a to "jpg"
   return a
end suffix_format

on gen_pict(artw, the_name, format_of)
   set format_of to format_of as text
   set gen_pt to (path to temporary items folder from user domain as text) & "iTunes:"
   do shell script "mkdir -p '" & POSIX path of gen_pt & "'"
   set inputFile to (gen_pt & the_name & "." & format_of as text)
   my write_note(inputFile, artw, true)
   return inputFile
end gen_pict

on write_note(inputFile, to_write, rewrite)
   #as «class utf8» == for umlauts
   try
       set accessRef to (open for access file the inputFile with write permission)
       if rewrite is true then
           #cancel file contents
           set eof accessRef to 0
           write to_write to accessRef #as «class utf8»
       else
           write to_write to accessRef starting at eof #as «class utf8»
       end if
       close access accessRef
   end try
end write_note

(*
on write_note(inputFile, to_write, rewrite)
#as «class utf8» == for umlauts
try
set accessRef to (open for access file the inputFile with write permission)
if rewrite is true then
#cancel file contents
set eof accessRef to 0
end if
write to_write to accessRef starting at eof as «class utf8»
close access accessRef
on error
try
close access file inputFile
end try
end try
end write_note
*)


on growl_step(proj_title, my_events, icon_pt)
   
   set appName to "iTunes Msg"
   set myAllNotesList to {appName, my_events}
   set theEventDescription to {appName, my_events}
   
   tell application "GrowlHelperApp"
       register as application appName all notifications myAllNotesList ¬
           default notifications myAllNotesList icon of application "iTunes"
       notify with name appName title proj_title ¬
           description my_events application name appName image from location icon_pt
       --with sticky
   end tell
end growl_step

Last edited by Joy (2014-01-20 05:35:27 pm)

Offline

 

#2 2013-12-22 01:30:09 pm

StefanK
Member
From:: St. Gallen, Switzerland
Registered: 2006-10-21
Posts: 11483
Website

Re: iTunes is growling

Hi,

I recommend to use the waterproof version of the write routine to close the open file descriptor reliably
as well as the general use of UTF8

Applescript:


on write_note(inputFile, to_write, rewrite)
   #as «class utf8» == for umlauts
   try
       set accessRef to (open for access file the inputFile with write permission)
       if rewrite is true then
           #cancel file contents
           set eof accessRef to 0
       end if
       write to_write to accessRef starting at eof as «class utf8»
       close access accessRef
   on error
       try
           close access file inputFile
       end try
   end try
end write_note


regards

Stefan

Offline

 

#3 2013-12-23 04:42:23 am

Joy
Member
From:: South Tirol
Registered: 2008-07-04
Posts: 547
Website

Re: iTunes is growling

StefanK wrote:

Hi,

I recommend to use the waterproof version of the write routine to close the open file descriptor reliably
as well as the general use of UTF8


…Ok; i adjusted my original code to reflect the changes you suggested.

Offline

 

#4 2014-01-20 05:34:41 pm

Joy
Member
From:: South Tirol
Registered: 2008-07-04
Posts: 547
Website

Re: iTunes is growling

Hi StefanK,
i tried your suggestion, but i get an error on Lion.
Now i added the handler i used first as an option to my original post. See below:

Applescript:

on write_note(inputFile, to_write, rewrite)
   #as «class utf8» == for umlauts
   try
       set accessRef to (open for access file the inputFile with write permission)
       if rewrite is true then
           #cancel file contents
           set eof accessRef to 0
           write to_write to accessRef #as «class utf8»
       else
           write to_write to accessRef starting at eof #as «class utf8»
       end if
       close access accessRef
   end try
end write_note

Offline

 

Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)