Embed iTunes Ratings directly into file - backup/restore

Discussion in 'Mac Apps and Mac App Store' started by m021478, Oct 29, 2009.

  1. m021478 macrumors 6502

    Joined:
    Nov 23, 2007
    #1
    I have spent hours and hours tagging my iTunes music with appropriate 'Star-Ratings' based on my listening preferences. The mere thought of accidentally losing all of this time and the detailed ratings I've applied is terrifying.

    Aside from simply exporting my iTunes "Master Library.xml" file on a regular basis, I would like to try and find a way to somehow physically embed the Star-Rating score of each given track directly into the track itself in the Finder (probably by annotating the spotlight comments field for each given track).

    Why would I seek doing something like this?

    As a photographer, I shoot my photographs in RAW format, and I typically wind up converting from my Canon proprietary RAW images (CR2) into Adobe's Digital Negative (DNG) format. One of the big reasons I do this is so that I don't have to deal with the .xmp sidecar file (which contains all of the RAW adjustment and editing information for image). With DNG, what would have normally been a separate, external metadata file containing a files unique adjustment information is actually embedded into the file itself. This prevents cases in which the sidecar files can get lost or corrupt. I like have a self contained entity with all of the relevant information I need for a given image (or music file for that matter)

    Rather than having to rely on exporting my iTunes Library and considering that as my sole backup of my hours and hours of ratings metadata for my music, I'd like to find a way to embed this information directly into the file itself, which in theory could somehow be re-extracted at a later date if need be and re-imported back into a new/different iTunes Library such that certain metadata fields such as the ratings score in my case, would be maintained.

    To simplify, my ideal solution would be an applescript that would gather all of my tracks in iTunes with a 5-star rating, and it would then apply a spotlight comment ~ like "Rating=5" ~ to each of those tracks in the Finder. It would then do the same thing for all tracks rated as 4, 3, 2, and 1-star.

    Not really sure how this spotlight comment could then be used to re-import the tracks into a different/new iTunes library while maintaining this rating info, but I wouldn't put it past some of the scripting gurus on these forums to think up a way...

    Any suggestions would be greatly appreciated... Thanks!
     
  2. wrldwzrd89 macrumors G5

    wrldwzrd89

    Joined:
    Jun 6, 2003
    Location:
    Solon, OH
    #2
    With a little AppleScripting, this can be easily done.
    Essentially, what you're doing is this:
    Code:
    tell application "iTunes"
    for each track in selection
    set theRating to track's rating
    set theFile to track's file
    tell application "Finder"
    set theFile's Spotlight comment to theRating
    end tell
    end tell
    And to do this process in reverse:
    Code:
    tell application "Finder"
    for each file in selection
    set theRating to file's Spotlight comment
    set theFile to file
    tell application "iTunes"
    set theFile's rating to theRating
    end tell
    end tell
    Apologies for my somewhat broken AppleScript code, I'm not at a Mac right now. I know this won't work as is, but at least it'll give a starting point. You can find the correct commands to use by opening an application's dictionary from the Script Editor.
     
  3. m021478 thread starter macrumors 6502

    Joined:
    Nov 23, 2007
    #3
    Can someone please help me properly write this applescript code so that I can copy and paste it into the script editor? I don't have the skills to do so myself, and if my request can indeed be fulfilled by these two simple codes, then I would really like to give these scripts a test run.

    Thanks!
     
  4. applesupergeek macrumors 6502a

    Joined:
    Nov 20, 2009
    #4
    I d love something like this too, I can't believe I cannot find it in Doug's scipts or that more requests for this have not been made. Makes porting songs in libraries a breeze...
     
  5. mysterytramp macrumors 65816

    mysterytramp

    Joined:
    Jul 17, 2008
    Location:
    Maryland
    #5
    This seems to work:

    Code:
    tell application "iTunes"
    	set theSelection to the selection
    	repeat with tr in theSelection
    		set theRating to tr's rating
    		set theFile to location of tr
    		tell application "Finder"
    			set theFile's comment to theRating
    		end tell
    	end repeat
    end tell
    
    I'm not sure the reverse is going to work the way you've mapped out; that is, I don't think you can select a file in Finder and script it's comments into iTunes. But this seems to work:


    Code:
    tell application "iTunes"
    	set theSelection to the selection
    	repeat with tr in theSelection
    		set theFile to location of tr
    		tell application "Finder"
    			set theRating to theFile's comment
    		end tell
    		set tr's rating to theRating
    	end repeat
    end tell
    
    mt
     
  6. applesupergeek macrumors 6502a

    Joined:
    Nov 20, 2009
    #6
    hey mystery tramp and the language that you used, thanks a lot! I 'll give it a go.
     
  7. applesupergeek macrumors 6502a

    Joined:
    Nov 20, 2009
    #7
    Ah, I am amazed, it works so great!!!!

    No more will I be forced to re-rate and be bound by itunes with my ratings. Now I can re-enter rating based on this script. This is so great.

    A couple of requests and one suggestion.

    do you think whenever you get the time you can get it to actually append the rating to the comment field instead of overwriting the rating, and also to include some discernible tag in the spotlight comments with it, i.e. rating=100.

    Since this works so well, maybe one can append the an ip3 tag field for the file, say the comments one, with the rating from itunes, and then have itunes re-incorporate this rating from there with another script, that would be even better.

    Thanks so much!
     
  8. mysterytramp macrumors 65816

    mysterytramp

    Joined:
    Jul 17, 2008
    Location:
    Maryland
    #8
    This saves a string, "rating=" xx to the comment field:

    Code:
    tell application "iTunes"
    	set theSelection to the selection
    	repeat with tr in theSelection
    		set theRating to return & "rating=" & tr's rating & return
    		set tr's comment to tr's comment & theRating
    	end repeat
    end tell
    This extracts the phrase "rating=" and puts that as the rating:

    Code:
    tell application "iTunes"
    	set theSelection to the selection
    	repeat with tr in theSelection
    		set testString to tr's comment
    		set songRatingLoc to (offset of "rating=" in testString) + 7
    		set songRatingNum to character songRatingLoc of testString ¬
    			& character (songRatingLoc + 1) of testString ¬
    			& character (songRatingLoc + 2) of testString
    		set songRatingNum to songRatingNum as integer
    		set tr's rating to songRatingNum
    		-- display dialog songRatingNum -- I'm leaving this behind for future debugging
    	end repeat
    end tell
    I've done very little testing on these scripts but they seem to work on my setup. Please do your own testing, but on songs you have backups for. I'm assuming I'm not doing anything dangerous, or improper, by accessing these fields via AppleScript, but you should still take care.

    mt
     
  9. mysterytramp macrumors 65816

    mysterytramp

    Joined:
    Jul 17, 2008
    Location:
    Maryland
    #9
    This appends "rating=" to a Spotlight comment:

    Code:
    tell application "iTunes"
    	set theSelection to the selection
    	repeat with tr in theSelection
    		set theRating to return & "rating=" & tr's rating & return
    		set theFile to location of tr
    		tell application "Finder"
    			set theFile's comment to theRating
    		end tell
    	end repeat
    end tell
    This reads the Spotlight comment and extracts the rating:

    Code:
    tell application "iTunes"
    	set theSelection to the selection
    	repeat with tr in theSelection
    		set theFile to location of tr
    		tell application "Finder"
    			set testString to theFile's comment
    		end tell
    		set songRatingLoc to (offset of "rating=" in testString) + 7
    		set songRatingNum to character songRatingLoc of testString ¬
    			& character (songRatingLoc + 1) of testString ¬
    			& character (songRatingLoc + 2) of testString
    		set songRatingNum to songRatingNum as integer
    		set tr's rating to songRatingNum
    	end repeat
    end tell
    Again, I've done little testing of this myself. It works on my system. YMMV.

    mt
     
  10. applesupergeek macrumors 6502a

    Joined:
    Nov 20, 2009
    #10
    Ah fantastic, thanks so much, this is such a great script, it means that you can take tracks in and out of your library (maybe for the purposes of sorting them out, or maybe you don't wont a portion of them and you plan to put them in another library) and still keep your ratings, which is great!!!

    Thanks again!!
     
  11. prramesh macrumors newbie

    Joined:
    Jul 16, 2008
    #11
    Doesn't work

    It doesn't work on 10.7.4.

    After I run the script from iTunes, I checked the spotlight comments on Finder. It's doesn't embed any information of the rating.

    Any have some tips for me!!
     

Share This Page