Skip to content

Instantly share code, notes, and snippets.

@dnicolson
Last active February 18, 2018 18:48
Show Gist options
  • Save dnicolson/d89e2916c06c1c4e37263f011afe6b90 to your computer and use it in GitHub Desktop.
Save dnicolson/d89e2916c06c1c4e37263f011afe6b90 to your computer and use it in GitHub Desktop.
JXA script to find songs with incorrect sentence case
const lowercaseWords = ['a', 'an', 'and', 'at', 'but', 'by', 'for', 'in', 'nor', 'of', 'on', 'or', 'so', 'the', 'to', 'with']
.map(word => ` ${word.toLowerCase().replace(/(^| )(\w)/g, s => s.toUpperCase())} `).join('|')
const iTunes = Application('iTunes'),
selection = iTunes.selection()
let songsToFix = []
selection.forEach((song) => {
const newName = song.name(),
newAlbum = song.album(),
newArtist = song.artist()
if (`${newName}${newAlbum}`.match(new RegExp(`^[0-9A-Z].*(${lowercaseWords})`))) {
songsToFix.push([newArtist, newAlbum])
}
})
const albumsToFix = [...new Set(songsToFix.map(o => JSON.stringify(o)))].map(s => JSON.parse(s))
const destinationPlaylist = iTunes.sources[0].userPlaylists.whose({name: {_equals: 'Playlist'}})
albumsToFix.forEach((album) => {
const songs = iTunes.playlists.whose({name: 'Library'})[0].tracks
.whose({artist: {_equals: album[0]}})
.whose({album: {_equals: album[1]}})
// songs.forEach((song) => {
// song.duplicate({to: destinationPlaylist[0]})
// })
for (let i=0; i<songs.length; i++) {
songs[i].duplicate({to: destinationPlaylist[0]})
}
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment