2

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.

enter image description here

0

2 Answers 2

3

Use is in to check whether " I." is inside song name, like this: if " I." is in someString.

Use the offset command to get its position inside of the song name


Here's is an example

set songName to (get name of thisTrack)
if " I." is in songName then -- " I." is inside this song name
    set {theWork, theMovement} to my splitText(songName, " I.") -- split the string to get the Work and the Movement
end if


on splitText(t, theSearchString)
    set x to the offset of theSearchString in t
    set a to text 1 thru (x - 1) of t -- everything before theSearchString would be set as the work
    set b to text x thru -1 of t -- this part would be set as the movement
    return {a, b}
end splitText
1

So here's my complete script, based on the other answer that has been given.

I know that my answer is most likely convoluted, and not efficient, but it works!

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 theSearchString to text returned of (display dialog "Enter the characters between the work name and movement name, for the first movement. Include spaces:" default answer ": I. ")
    set c to (count of sel)
    set songName to (get name of item 1 of sel)
    set movementNumber to 0
    set movementOffset to 0

    set theSearchString_no_rn to my delRomNum(theSearchString)

    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)
        if theSearchString is in songName then -- " I. " is inside this song name
            set movementOffset to the offset of theSearchString in songName
            set movementNumber to 0
        end if

        set theMovement to text (movementOffset + (length of theSearchString_no_rn)) thru -1 of songName -- this part would be set as the movement

        set theWork to text 1 thru (movementOffset - 1) of songName -- everything before theSearchString would be set as the work

        set movementNumber to movementNumber + 1
        set movement number of thisTrack to movementNumber
        set movement of thisTrack to my delRomNum(theMovement)
        set work of thisTrack to theWork

    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 must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .