Skip to content

Instantly share code, notes, and snippets.

@dnicolson
Last active November 22, 2016 21:55
Show Gist options
  • Save dnicolson/cb045773224dfb2af074eaf35d580de5 to your computer and use it in GitHub Desktop.
Save dnicolson/cb045773224dfb2af074eaf35d580de5 to your computer and use it in GitHub Desktop.
/*
An OSA script to replace disc numbers in album names with the disc number attribute.
Accepts album names in any of these formats:
LCD Soundsystem (Disc 1)
Mellon Collie and the Infinite Sadness (Disc 1: Dawn to Dusk)
The Wall: Live in Berlin (Disc 2) [Live]
*/
function getMultiDiscAlbums () {
let albums = {}
const tracks = library.whose({ album: { _contains: '(Disc ' } })
tracks().forEach(function (track) {
const albumNameWithDisc = track.album()
const albumName = albumNameWithDisc.replace(/ \(Disc .*\).*/i, '') // this ignores '[Live]'
albums[albumName] = albums[albumName] || {}
albums[albumName][albumNameWithDisc] = albums[albumName][albumNameWithDisc] || []
albums[albumName][albumNameWithDisc].push(track)
})
return albums
}
function replaceAlbumNames (albums) {
for (const albumName in albums) {
if (albums.hasOwnProperty(albumName)) {
const albumNames = Object.keys(albums[albumName]).sort()
const discCount = albumNames.length
albumNames.forEach(function (albumNameWithDisc, index) {
const discNumber = index + 1
const album = albumNameWithDisc.replace(/ \(Disc .*\)/i, '')
albums[albumName][albumNameWithDisc].forEach(function (track) {
track.discNumber.set(discNumber)
track.discCount.set(discCount)
track.album.set(album)
})
})
}
}
}
const app = Application('iTunes')
const library = app.sources[0].libraryPlaylists[0].fileTracks
const albums = getMultiDiscAlbums()
replaceAlbumNames(albums)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment