Automate iTunes

    When adding music to iTunes, I was faced with the fact that a large number of songs do not have correctly registered tags. It looks something like this

    image

    A detailed inspection showed that this is not an encoding problem. The tags in the tracks are exactly what were clogged with '?' Marks.


    As it turned out, not everything was lost, since the name of the file, as well as the directory in which this file is located, contains all the necessary information, such as artist name, album, year, name and track number. You can

    image

    fix tracks in iTunes simply by taking the path to the file, select from it pieces of interest to us and write them in the appropriate tags. For this task, it was decided to use AppleScript. Comprehensive documentation on it can be found here AppleScript Guide , and examples of working scripts here Doug's Scripts

    Having rolled up our sleeves, we start the Script Editor

    image

    From the Windows> Library menu, select the iTunes application that interests us.

    image

    This will open us a description of the application objects available for scripting

    image

    AppleScript, unlike other programming languages ​​it is very similar to English

    image

    Some AppleScript programs are indistinguishable from notes of a high school student. Minus - those who programmed in other languages ​​will have to be quite tight at first. However, there is a plus - in addition to the ability to automate applications in Mac OS X, programming in AppleScript provides good practice in English.

    There are two ways to see the results of the script:

    image

    either using return and the Result console, or using log in the Event Log panel

    image

    There is such a thing as a display dialog, but this is for advanced users.

    Returning to our problem of parsing the file name. This is done somewhat unobviously,

    image

    however, if you read carefully, then everything is clear. English grammar up to possessive declension. Note that a list of strings is returned text items of filename. To get an element of this list, you just need to indicate the number of the desired element (numbering from one)

    image

    and do not forget about the grammar text item 5 of filename. It is unclear why the developers of AppleScript did not go further and did not replace, say, numbers with the corresponding verbal designations.
    In general, working with strings in AppleScript is somewhat awkward at the non-English look, on the other hand, you quickly get used to it and sometimes don’t know how to write this or that construction, you just write in English. It turns out a working program.

    The final analysis of the file name into parts of interest to us looks something like this

    image

    Tracks will be processed in iTunes. To do this, we will work in a cycle with a list of selected tracks. The list of selected tracks is obtained using selection.

    image

    Properties (tags) of interest to us can be found in the documentation that we opened earlier.

    image

    The final script looks like this. Pay attention to the type conversion (as text, as integer) during setting or obtaining object properties. After that, just select the track in iTunes and run the script

    image

    tell application "iTunes"
        if selection is not {} then -- there ARE tracks selected...
            set sel to a reference to selection
            repeat with aTrack in sel
                set filename to location of aTrack as text
                set AppleScript's text item delimiters to ":"
                set artst to text item 5 of filename as text
                set albm to text item 6 of filename as text
                set title to text item 7 of filename as text
                set AppleScript's text item delimiters to ""
                -- work on album and year
                set dashIdx to offset of "-" in albm
                set yr to items 1 thru (dashIdx - 1) of albm as text
                set albm to items (dashIdx + 1) thru -1 of albm as text
                -- work on title
                set dashIdx to offset of "-" in title
                set trackNo to items 1 thru (dashIdx - 1) of title as text
                set title to items (dashIdx + 1) thru -4 of title as text
                set year of aTrack to (yr as integer)
                set artist of aTrack to artst
                set name of aTrack to title
                set track number of aTrack to (trackNo as integer)
                set album of aTrack to albm
            end repeat
        end if
    end tell

    * This source code was highlighted with Source Code Highlighter.







    image

    Voila, as they say.

    It remains only to save the script in ~/Library/iTunes/Scriptsand it will be available under the name under which you saved it in the iTunes

    image

    Happy Scripting menu !


    Also popular now: