I need help with this script! text manipulation
I'm a complete newbie at this.. and struggling a bit
I'm busy trying to create a script to back up iTunes info that's not stored in the id3 tag to the comments field - namely Rating, Playlist, and some sort of Keywords. Why you ask? Well because it takes hours to put in and if your library gets trashed it's not stored in the original tracks so it's lost for ever.
To start with I've created a script that append "?020? " to the beginning of the comments field where 020 for instance means one-star rating. I've decided to use the delimiters ? either side of the rating because they are pretty unique. Ratings are numerical as iTunes stores them that way internally. This bit works.
Ultimately I'm planning a system where ratings are stored first, keywords are stored after that and any other comments are stored after that.
Keywords will be in the form ?mellow, ?angry, ?loud etc etc etc. (Obviously spaces would be tricky in there so best to use underline)
Playlists can probably just be keywords too (? may with a unique character ascribed, although that is probably redundant) e.g. $?Playlist_One, ??Playlist_2.
Overall the comment will look like this;
?80? ??My_Alltime_Favourites ??BestJazz ?Cool_jazz ?Piano_Jazz $Oscar_Peterson AMG Review: In the album Nightrain Oscar Peterson blah blah blah.
WHAT I NEED HELP WITH IS TEXT MANIPULATION
The next (small) step is to write a bit of logic that will clean up comments so that if they already contain a "rating as text" (? whatnot), it gets moved to the front of the comments field. i.e.:
"A truly great track ?100?" becomes
"?100? A truly great track"
I see a need to do this incase the comment has had text added to the front.
I also see it as a way of getting to understand text manipulation. I've tried using the applescript text delimters and text item functions but something keeps going wrong.
Where can i get guidance?
ta
m
CODE FOLLOWS FOr ANYONE INTERESTED...
tell application "iTunes"
activate
-- first we set the possible list of ratings as text
set rating_delimiter to "?"
set rating_text_list to {rating_delimiter & "000" & rating_delimiter, rating_delimiter & "020" & rating_delimiter, rating_delimiter & "040" & rating_delimiter, rating_delimiter & "060" & rating_delimiter, rating_delimiter & "080" & rating_delimiter, rating_delimiter & "100" & rating_delimiter}
-- logic to determine whether a selection is active or whether we are dealing with a playlist
copy (a reference to (get view of front window)) to thePlaylist
if selection exists then
set using_selection to true
copy (count selection's items) to idx
else -- its the whole playlist
display dialog "it's the whole playlist!! your forgot to select anything... cancel if you selected the whole library!!!"
set using_selection to false
set selectedTracks to (get a reference to thePlaylist)
copy (count thePlaylist's tracks) to idx
end if
-- and now we loop adding the rating to the beginning of the comment for each track
repeat with i from 1 to idx
if using_selection then
copy item i of selection to thisTrack
else
copy track i of selectedTracks to thisTrack
end if
-- thisTrack now set
-- first we clean the comments field up. ratings always go first. So we find all words containing the rating delimiter (? probably), memorize them, delete them and put the first one found at the beginning of the track's comment
if (thisTrack's comment) contains rating_delimiter then
set oldDelimiters to AppleScript's text item delimiters
set AppleScript's text item delimiters to " "
--we need logic here to move ratings to front of comment
set AppleScript's text item delimiters to oldDelimiters
end if
-- if there is no rating in the comment and the track is rated, let's update the comment
if (thisTrack's comment) does not contain rating_delimiter and (thisTrack's rating) is not equal to 0 then
set rating_text_list_index to ((get thisTrack's rating) / 20 + 1)
set thisTrack's comment to (item rating_text_list_index of rating_text_list & " " & (get thisTrack's comment))
end if
end repeat

