Rate the current iTunes song

Sharing and discussing custom actions for LaunchBar
Post Reply
curtismchale
Posts: 5
Joined: Wed May 05, 2010 5:46 pm

Rate the current iTunes song

Post by curtismchale » Fri May 07, 2010 8:50 am

Looked around earlier and didn't see a script for this. Here it is. Just type the number of stars you'd like and it will change the rating of the current track.

Code: Select all

on handle_string(s)
   
   tell application "iTunes"
      set rating of current track to s * 20
   end tell
   
end handle_string

eschlon
Posts: 3
Joined: Fri May 21, 2010 9:42 am

Re: Rate the current iTunes song

Post by eschlon » Fri May 21, 2010 10:00 am

Coming over from QS (or trying to) and put this together for iTunes rating. Works essentially like the OPs but has several checks to ensure that it won't throw up errors and has growl notification support. Hope it helps people.

Code: Select all

on handle_string(rating)
   
   set NewRating to rating as string
   
   (* Check to see if iTunes is running and has a current track, as it'd be pretty stupid to run this script otherwise. Also, doing so would generate an error. *)
   tell application "System Events"
      set iTunesisRunning to ((count of (every process whose name is "iTunes")) > 0)
   end tell
   if iTunesisRunning then
      tell application "iTunes"
         if current track exists then
            set iTunesOk to true
         else
            set iTunesOk to false
         end if
      end tell
   end if
   
   (* Collect information about the current track and set mode to either rate (if a usable rating value between 0 and 5 is supplied) or check (otherwise) *)
   if iTunesOk then
      tell application "iTunes"
         set CurrentRating to (rating of current track) / 20 as integer
         set CurrentTrack to name of current track
         set CurrentAlbum to album of current track
         set CurrentArtist to artist of current track
         if (count of artwork of current track) > 0 then
            set Art to (data of artwork 1) of current track
         else
            set Art to "DEFAULT"
         end if
         if NewRating is in {"0", "1", "2", "3", "4", "5"} then
            set rating of current track to NewRating * 20
            set ScriptMode to "Rate"
         else
            set ScriptMode to "Check"
         end if
      end tell
      
      (* Generate the star strings for the current and new ratings *)
      set CurrentStars to {}
      set NewStars to {}
      repeat with i from 1 to 5
         if i ≤ CurrentRating then
            set CurrentStars to CurrentStars & {"★"}
         else
            set CurrentStars to CurrentStars & {"☆"}
         end if
         if i ≤ NewRating then
            set NewStars to NewStars & {"★"}
         else
            set NewStars to NewStars & {"☆"}
         end if
      end repeat
      set CurrentStars to CurrentStars as string
      set NewStars to NewStars as string
      
      (* Check to see if growl is running... *)
      tell application "System Events"
         set GrowlisRunning to ¬
            (count of (every process whose name is "GrowlHelperApp")) > 0
      end tell
      
      (* ... if it is then send an appropriate notification. Otherwise don't *)
      if GrowlisRunning then
         tell application "GrowlHelperApp"
            set the allNotificationsList to {"Rating Update", "Song Check"}
            set the enabledNotificationsList to {"Rating Update", "Song Check"}
            register as application "_iTunes Rating Update" all notifications allNotificationsList default notifications enabledNotificationsList icon of application "iTunes"
            
            
            if (CurrentStars = NewStars) or (ScriptMode is "Check") then
               if Art = "DEFAULT" then
                  notify with name "Rating Update" title CurrentStars ¬
                     description "Song: " & CurrentTrack & return & "Album: " & CurrentAlbum & return & "Artist: " & CurrentArtist & return application name "_iTunes Rating Update"
               else
                  notify with name "Rating Update" title CurrentStars ¬
                     description "Song: " & CurrentTrack & return & "Album: " & CurrentAlbum & return & "Artist: " & CurrentArtist & return application name "_iTunes Rating Update" pictImage Art
               end if
            else
               if Art = "DEFAULT" then
                  notify with name "Rating Update" title CurrentStars & " ➤➤ " & NewStars ¬
                     description "Song: " & CurrentTrack & return & "Album: " & CurrentAlbum & return & "Artist: " & CurrentArtist & return application name "_iTunes Rating Update"
               else
                  notify with name "Rating Update" title CurrentStars & " ➤➤ " & NewStars ¬
                     description "Song: " & CurrentTrack & return & "Album: " & CurrentAlbum & return & "Artist: " & CurrentArtist & return application name "_iTunes Rating Update" pictImage Art
               end if
            end if
         end tell
      end if
   end if
end handle_string


~~esc]

veke
Rank 2
Rank 2
Posts: 49
Joined: Mon Nov 06, 2006 11:30 am

Re: Rate the current iTunes song

Post by veke » Fri Jun 04, 2010 10:42 am

I actually made a couple of scripts to rate songs and albums and posted them on here a year or so ago, but these are much nicer. Mine were considered for inclusing in LB, but there was an issue with the index not being updated with the star ratings back then, perhaps that has been resolved with never versions of iTunes and we might have these scripts build in now? They are essential.

FiXeR
Posts: 5
Joined: Sat Jan 30, 2010 10:52 am

Re: Rate the current iTunes song

Post by FiXeR » Fri Jun 04, 2010 4:28 pm

Well the second script doesn't work with half stars. So I will stick with the first one.

eschlon
Posts: 3
Joined: Fri May 21, 2010 9:42 am

Re: Rate the current iTunes song

Post by eschlon » Fri Jun 04, 2010 8:09 pm

Wow... I never even realized iTunes could do half stars, that is so useful.

Here's an updated version, handles half stars given as numbers (.5, 0.5, 1.5, 2.5, etc.), thanks for the tip.

Code: Select all

on handle_string(rating)
   
   set NewRating to rating as string
   
   (* Check to see if iTunes is running and has a current track, as it'd be pretty stupid to run this script otherwise. Also, doing so would generate an error. *)
   tell application "System Events"
      set iTunesisRunning to ((count of (every process whose name is "iTunes")) > 0)
   end tell
   if iTunesisRunning then
      tell application "iTunes"
         if current track exists then
            set iTunesOk to true
         else
            set iTunesOk to false
         end if
      end tell
   end if
   
   (* Collect information about the current track and set mode to either rate (if a usable rating value between 0 and 5 is supplied, with .5 values supported) or check (otherwise) *)
   if iTunesOk then
      tell application "iTunes"
         set CurrentRating to (rating of current track) / 20 as string
         set CurrentTrack to name of current track
         set CurrentAlbum to album of current track
         set CurrentArtist to artist of current track
         if (count of artwork of current track) > 0 then
            set Art to (data of artwork 1) of current track
         else
            set Art to "DEFAULT"
         end if
         if NewRating is in {"0", ".5", "0.5", "1", "1.5", "2", "2.5", "3", "3.5", "4", "4.5", "5"} then
            set rating of current track to NewRating * 20
            set ScriptMode to "Rate"
         else
            set ScriptMode to "Check"
         end if
      end tell
      
      (* Generate the star strings for the current and new ratings *)
      set CurrentStars to {}
      set NewStars to {}
      set CurrentHalfStar to false
      set NewHalfStar to false
      if CurrentRating is in {".5", "0.5", "1.5", "2.5", "3.5", "4.5"} then
         set CurrentHalfStar to true
         set CurrentRating to CurrentRating - 0.5
      end if
      if NewRating is in {".5", "0.5", "1.5", "2.5", "3.5", "4.5"} then
         set NewHalfStar to true
         set NewRating to NewRating - 0.5
      end if
      repeat with i from 1 to 5
         if i ≤ CurrentRating then
            set CurrentStars to CurrentStars & {"★"}
         else if CurrentHalfStar and i ≤ CurrentRating + 1 then
            set CurrentStars to CurrentStars & {"½"}
         else
            set CurrentStars to CurrentStars & {"☆"}
         end if
         if i ≤ NewRating then
            set NewStars to NewStars & {"★"}
         else if NewHalfStar and i ≤ NewRating + 1 then
            set NewStars to NewStars & {"½"}
         else
            set NewStars to NewStars & {"☆"}
         end if
      end repeat
      set CurrentStars to CurrentStars as string
      set NewStars to NewStars as string
      
      (* Check to see if growl is running... *)
      tell application "System Events"
         set GrowlisRunning to ¬
            (count of (every process whose name is "GrowlHelperApp")) > 0
      end tell
      
      (* ... if it is then send an appropriate notification. Otherwise don't *)
      if GrowlisRunning then
         tell application "GrowlHelperApp"
            set the allNotificationsList to {"Rating Update", "Song Check"}
            set the enabledNotificationsList to {"Rating Update", "Song Check"}
            register as application "_iTunes Rating Update" all notifications allNotificationsList default notifications enabledNotificationsList icon of application "iTunes"
            
            
            if (CurrentStars = NewStars) or (ScriptMode is "Check") then
               if Art = "DEFAULT" then
                  notify with name "Rating Update" title CurrentStars ¬
                     description "Song: " & CurrentTrack & return & "Album: " & CurrentAlbum & return & "Artist: " & CurrentArtist & return application name "_iTunes Rating Update"
               else
                  notify with name "Rating Update" title CurrentStars ¬
                     description "Song: " & CurrentTrack & return & "Album: " & CurrentAlbum & return & "Artist: " & CurrentArtist & return application name "_iTunes Rating Update" pictImage Art
               end if
            else
               if Art = "DEFAULT" then
                  notify with name "Rating Update" title CurrentStars & " ➤➤ " & NewStars ¬
                     description "Song: " & CurrentTrack & return & "Album: " & CurrentAlbum & return & "Artist: " & CurrentArtist & return application name "_iTunes Rating Update"
               else
                  notify with name "Rating Update" title CurrentStars & " ➤➤ " & NewStars ¬
                     description "Song: " & CurrentTrack & return & "Album: " & CurrentAlbum & return & "Artist: " & CurrentArtist & return application name "_iTunes Rating Update" pictImage Art
               end if
            end if
         end tell
      end if
   end if
end handle_string


~~esc]

FiXeR
Posts: 5
Joined: Sat Jan 30, 2010 10:52 am

Re: Rate the current iTunes song

Post by FiXeR » Sat Jun 05, 2010 9:17 pm

Thanks :) I am going to try it now .. good work

Update: It's not working for me :/

eschlon
Posts: 3
Joined: Fri May 21, 2010 9:42 am

Re: Rate the current iTunes song

Post by eschlon » Sat Jun 05, 2010 11:37 pm

Hrm... weird. Works fine for me when I cut and paste it out of the code box.

It does require that iTunes is running, and that there is a current track (i.e. a track is currently playing or paused). After that you use the 'Enter Text' action and send the text to the script. It accepts any values. If the values are in {.5,0.5,1,1.5, etc} then it should change the rating and give you a growl notification if growl is running.

If the text entered is anything else it should just do a growl notification with the current rating and info.

Also, if you 'change' the rating of a song to its current rating (i.e. rate a 4 star song with 4 stars) it will give you a growl notification but it will not indicate that the rating has been updated (since it hasn't).

veke
Rank 2
Rank 2
Posts: 49
Joined: Mon Nov 06, 2006 11:30 am

Re: Rate the current iTunes song

Post by veke » Mon Jun 07, 2010 4:11 pm

Here's my simplstic solution to the problem, I've made one of these for my most used ratings :

Album ratings:

Code: Select all

tell application "iTunes"
   if player state is playing then
      set album rating of current track to 70
   end if
end tell


and for tracks :

Code: Select all

tell application "iTunes"
   if player state is playing then
      set rating of current track to 100
   end if
end tell


The values are one star 10, two star 40, three stars 60 etc.
Half stars are the ones between those , i.e. 50 for 2 and a half star, 70 for 3 and a half star etc.

Then I have these set to "R1", "AR3" etc for fast rating of the current track.

Walter
Rank 3
Rank 3
Posts: 103
Joined: Fri Nov 24, 2006 6:09 pm

Re: Rate the current iTunes song

Post by Walter » Fri Jun 11, 2010 1:15 pm

Sorry, i got lost in the manual: where do i paste those script commands ?

TomEck
Rank 2
Rank 2
Posts: 55
Joined: Mon Mar 02, 2009 10:41 am

Re: Rate the current iTunes song

Post by TomEck » Tue Jun 15, 2010 10:05 am

Walter wrote:Sorry, i got lost in the manual: where do i paste those script commands ?


AppleScript Editor

Than you have to save the script to an indexed folder (typically the actions folder found in the library: /Library/Application Support/LaunchBar/Actions/).

jenspoder
Posts: 1
Joined: Fri Jun 17, 2011 9:37 am

Re: Rate the current iTunes song

Post by jenspoder » Fri Jun 17, 2011 9:53 am

This script is just the thing I was looking for. Amazing work!

Beautiful growl notification.

Thanks a million.

Jens Poder

hmayes
Posts: 1
Joined: Mon Nov 21, 2011 5:44 pm

Re: Rate the current iTunes song

Post by hmayes » Mon Nov 21, 2011 5:46 pm

Anyone know how to update this since Growl moved to the mac app store? i tried troubleshooting this (got rid of the pictimage error) but can't get it to display the growl notification. Thanks!

Hal

ptujec
Wizard
Wizard
Posts: 321
Joined: Fri Dec 19, 2008 11:36 am
Contact:

Re: Rate the current iTunes song

Post by ptujec » Mon Apr 16, 2012 5:16 pm

This should run with the current Growl (1.3.3) and iTunes (10.6.1) versions:

Code: Select all

-- iTunes - Rate (for current track playing)
-- @Ptujec 2012-04-16

on run
   
   my growlRegister()
   tell application "Growl" to notify with name "Current Song" title "Error" description "Hit spacebar on the selected script to enter your rating!" application name "iTunes - Rate" priority 2
   
end run

on handle_string(s)
   
   tell application "iTunes"
      if player state is playing then
         set okflag to true
      else
         set okflag to false
      end if
   end tell
   
   if okflag is true then
      _rate(s)
      
   else if okflag is false then
      my growlRegister()
      tell application "Growl" to notify with name "Current Song" title "Error" description "iTunes is not playing ..." application name "iTunes - Rate" priority 2
      
   end if
   
end handle_string

on _rate(s)
   
   tell application "iTunes"
      set rating of current track to s * 20
   end tell
   
   tell application "iTunes"
      set aTrack to the current track
      set aName to the name of aTrack
      set aArtist to the artist of aTrack
      if artist of aTrack is not "" then
         set aArtist to the artist of aTrack
      else if artist of aTrack is "" then
         set aArtist to aName
      end if
      
      set rating of aTrack to s * 20
      
      if s is "1" then
         set _rating to "★"
      else if s is "2" then
         set _rating to "★★"
      else if s is "3" then
         set _rating to "★★★"
      else if s is "4" then
         set _rating to "★★★★"
      else if s is "5" then
         set _rating to "★★★★★"
      else
         set _rating to ""
      end if
      
      
      if (count of artwork of aTrack) ≥ 1 then
         set _artist to the artist of aTrack
         set _name to the name of aTrack
         
         set _home to POSIX path of (path to home folder)
         set pathToNewFile to (_home & "TMP/" & _artist & "_" & _name & ".jpg") as string
         tell me to set file_reference to (open for access pathToNewFile with write permission)
         tell application id "com.apple.iTunes" to write (get raw data of artwork 1 of aTrack) to file_reference starting at 0
         tell me to close access file_reference
         -- delay 0.5
         
         my growlRegister()
         tell application "Growl" to notify with name "Current Song" title aArtist description aName & "\n" & _rating application name "iTunes - Rate" image from location pathToNewFile
      else
         my growlRegister()
         tell application "Growl" to notify with name "Current Song" title aArtist description aName & "\n" & _rating application name "iTunes - Rate"
      end if
   end tell
   
   
end _rate


-- additional code for Growl Notification
on growlRegister()
   tell application "Growl"
      register as application "iTunes - Rate" all notifications {"Current Song"} default notifications {"Current Song"} icon of application "iTunes"
   end tell
end growlRegister



BTW: If you are looking for a neat Growl Style (supporting all priorities) check out these which I created/modified: http://quickcalapp.com/goodies.html

ptujec
Wizard
Wizard
Posts: 321
Joined: Fri Dec 19, 2008 11:36 am
Contact:

Re: Rate the current iTunes song

Post by ptujec » Fri Nov 09, 2012 2:26 pm

I updated this script. You can find it among others here:
https://github.com/Ptujec/LaunchBar

(Requires LaunchBar 5.4 because of support for Notification Center)

Post Reply