📜 ⬆️ ⬇️

Automating iTunes

Adding music to iTunes, I was faced with the fact that a large number of songs do not have properly spelled tags. It looks like this

image

A detailed inspection showed that this is not a problem of encodings. The tags in the tracks are exactly that they were filled with '?' Signs.
')

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

image

To correct the tracks in iTunes, you can simply take the path to the file, select from it the pieces of interest and write them into the appropriate tags. For this problem, it was decided to use AppleScript. Exhaustive documentation on it can be found here AppleScript Guide , and examples of working scripts here Doug's Scripts

Roll up the sleeves, run the Script Editor

image

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

image

This will give us a description of the application objects for scripting.

image

AppleScript, unlike other programming languages, is very similar to English.

image

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

There are two ways to see the results of the script: using the return and Result console,

image

either by using the log in the Event Log panel

image

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

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

image

But if you get a grasp, then everything is clear. English grammar up to possessive declension. Notice that a list of text items of filename returned. To get an element of this list, you just need to specify the number of the desired element (numbering from one)

image

And do not forget about the grammar text item 5 of filename . It is not clear why the developers of AppleScript did not go further and did not replace, say, the numbers with the corresponding verbal notation.
In general, working with strings in AppleScript is somewhat clumsy in a non-English-speaking look, on the other hand you quickly get used to it and sometimes without knowing how to write down this or that construction, you simply write in English. It turns out a working program.

The final parsing of the file name to the parts of interest looks like this

image

Tracks will be processed in iTunes. For this we will work in a loop with a list of selected tracks. We will get the list of selected tracks using selection.

image

The properties (tags) we are interested in can be found in the documentation we opened earlier.

image

The final script looks like this

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 .
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 .
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 .



Note the type conversion (as text, as integer) when setting or getting the properties of an object.

After that, just select the track in iTunes and run the script

image

Voila, as they say.

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

image

Happy Scripting!

Source: https://habr.com/ru/post/63506/


All Articles