Categories: Misc / DotNet / Java / Coder / Linux / PHP Ask - La ask - La Answer

I need help with this script! text manipulation

Hi there,

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
[4621 byte] By [mollw] at [2007-11-9 14:20:14]
# 1 Re: I need help with this script! text manipulation
Note: If you think this thread will get longer, please edit the posted script so the lines aren't as long (it ends up being about three browsers wide which makes it difficult to read any of the comments).

The parsing, text item delimiters (set to "?") should be the right way to go. What "keeps going wrong"?

I would suggest wrapping you entire block in the tag (instead of just the rating) which will make putting and getting your data from the tag much easier.

By using a block tag then separating your entires with commas and having a leading field specification character, things could be much easier.

?80,?My Alltime Favourites,?BestJazz,?Cool Jazz,?Piano Jazz?Oscar_Peterson AMG Review: In the album Nightrain Oscar Peterson blah blah blah

And allow you to keep spaces.
DeltaTee at 2007-11-15 17:25:08 >
# 2 Re: I need help with this script! text manipulation
depending on if your rating symbol is going to occur more than twice in the existing string then something like this...

set the_comments to "some before text///80///someafter text"
set the_rating_text to "///"
if the_comments contains the_rating_text then
set old_tids to AppleScript's text item delimiters
set AppleScript's text item delimiters to the_rating_text
if text item 1 of the_comments is real then
set new_comments to (the_rating_text & text item 1 of the_comments & the_rating_text and text item 2 of the_comments) as string
else
try
set new_comments to (the_rating_text & text item 2 of the_comments & the_rating_text & text item 1 of the_comments & text item 3 of the_comments) as string
on error
set new_comments to (the_rating_text & text item 2 of the_comments & the_rating_text & text item 1 of the_comments) as string
end try
end if
end if

might work.. it checks first to see if the string which indicates a a rating exists in the varaible containg the comments.. if it does it splits the string using the rating symbol as the tid, this would make either text item 1 or text item 2 be the rating..
so check if text item 1 is a number .. if so then just add whats left of the string after the number..
if text item 1 is not a number then the rating is text item 2.. so attempt to build the new comment with text item 2 at the front, then text item 1 (what came before the rating orginally) and see if you can add text item 3 (what came after the rating originally) to the new comment.. if unable to add text item 3 then there was nothing after the rating in the original comments...
deeg at 2007-11-15 17:26:08 >
[an error occurred while processing this directive]
[an error occurred while processing this directive]