AppleScript: trabajo y movimiento masivo de iTunes

Aquí estoy una vez más con otra pregunta de iTunes AppleScript. Tengo un script de trabajo en el que seleccionas un trabajo (varias "canciones" de iTunes) y le dices cómo configurar los metadatos del trabajo para esa selección. También le dice dónde comienza el nombre del movimiento en el nombre de la canción, y copia todo lo que sigue a esa posición en la etiqueta del movimiento, excluyendo todos los números romanos. Por supuesto, también numera los movimientos.

Aquí está el código para eso:

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

Puede ver mi publicación sobre ese script aquí: Buscar y reemplazar AppleScript para nombres de pistas de iTunes

De todos modos, ese script ahora no se ha vuelto lo suficientemente eficiente para mi uso (proceso muchas pistas clásicas). Usando el script anterior, tengo que seleccionar cada trabajo individual y recortarlo en consecuencia para el trabajo y luego para el movimiento.

Lo que me gustaría crear ahora es un guión que pueda hacer todo el proceso para múltiples trabajos a la vez, digamos, un álbum completo.

Tendría que encontrar cada pista que contenía I.y establecerla como punto de partida para el guión que he descrito anteriormente, y también obtener la posición de eso I.y recortar en consecuencia para las etiquetas de Trabajo y Movimiento para ese trabajo en particular, por ejemplo, todo antes I.y el el espacio que le precede sería fijado como obra, y todo lo que le sigue sería fijado como movimiento.

Puedo ver que esto es lo que tengo que hacer, ¡pero soy demasiado novato en AppleScript para implementarlo! De todos modos, para mí, el verdadero desafío radica en determinar si una cadena se encuentra dentro de otra cadena (por ejemplo, verificar si I.está dentro del nombre de la canción) y encontrar su posición dentro del nombre de la canción. ¡Si supiera cómo hacer esas dos cosas, probablemente podría escribir el resto del guión!

Cualquier sugerencia/idea sería muy útil. Y espero que mi descripción tenga sentido. ¡Gracias!

Nota: aunque obtuve la respuesta a la parte clave y puedo escribir el resto del script yo mismo, agregaré una entrada/salida de muestra.

ingrese la descripción de la imagen aquí

Respuestas (2)

Úselo is inpara verificar si " I. " está dentro del nombre de la canción, así: if " I." is in someString.

Use el offsetcomando para obtener su posición dentro del nombre de la canción


Aquí hay un ejemplo

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

Así que aquí está mi guión completo, basado en la otra respuesta que se ha dado.

Sé que mi respuesta probablemente sea intrincada y no eficiente, ¡pero funciona!

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