Here I am yet again with another iTunes AppleScript question. I have a working script in which you select a work (several iTunes "songs"), and tell it what to set the work metadata as for that selection. You also tell it where the the movement name starts in the song name, and it copies everything after that position into the movement tag, excluding all roman numerals. It also of course numbers the movements.
Here's the code for that:
tell application "iTunes"
set sel to selection of front browser window
if sel is {} then
try
display dialog "Nothing is selected…" buttons {"Quit"} with icon 0
end try
return
end if
set c to (count of sel)
set songName to (get name of item 1 of sel)
set workName to display dialog "Edit for Work name and then click OK." default answer songName --prompt for work name
set movementLength to display dialog "Edit to everything except the movement name. Do not include the roman numeral if one is present. If an arabic numeral is present, include it." default answer songName --prompt for movement length
repeat with i from 1 to c --set the movement numbers
set thisTrack to item i of sel
set songName to (get name of thisTrack)
set work of thisTrack to text returned of workName
set movement number of thisTrack to i
set movement count of thisTrack to c
set movement of thisTrack to my delRomNum(text ((length of text returned of movementLength) + 1) thru (length of songName) of songName as string) -- copy movement text from song name and delete roman numerals
end repeat
end tell
on delRomNum(t) -- the perl command search and delete any roman numeral (must be a word followed by the period and a space character)
do shell script "/usr/bin/perl -pe 's/\\b[IVXLCDM]+\\b. //g' <<< " & quoted form of t
end delRomNum
You can see my post about that script here: Find-and-replace AppleScript for iTunes Track Names
Anyhow, that script has now not become efficient enough for my use (I process a lot of classical tracks)! Using the above script, I have to select every single work, and trim it accordingly for work, and then for movement.
What I would like to now create is a script that can do the whole process to multiple works at once, say, a whole album.
It would have to find every track that contained I.
and set that as the starting point for the script I have outlined above, and also get the position of that I.
and trim accordingly for the Work and Movement tags for that particular work - e.g everything before I.
and the space preceding it would be set as the work, and everything after it would be set as the movement.
I can see that this is what I have to do, but I'm too much of an AppleScript noob to actually implement it! For me anyway, the real challenge lies in determining whether a string lies inside of another string (e.g. check whether I.
is inside song name) and finding its position inside of the song name. If I knew how to do those two things I could probably write the rest of the script!
Any hints/ideas would be very helpful. And I hope my description makes sense. Thanks!
Note: Even though I got the key part answered, and can write the rest of the script myself, I'll add a sample input/output.