Skip to content

Instantly share code, notes, and snippets.

@smileart
Created July 4, 2015 15:25
Show Gist options
  • Save smileart/8117a438424fbc1e08d7 to your computer and use it in GitHub Desktop.
Save smileart/8117a438424fbc1e08d7 to your computer and use it in GitHub Desktop.
JXA script which automates playlists import to the Apple Music using iTunes
// ======= Motivation ========
// http://www.theverge.com/2015/6/30/8871591/spotify-to-apple-music-migrate
// http://i.giphy.com/1zTqgW6bS2jWU.gif
// https://goo.gl/lMTvOj
// All the delays in this script could be adapted
// for your own needs…
// Add this code to the Script Editor (OS X Only!)
// and press Run… http://i.giphy.com/QfGMSZ25v3pGU.gif
// WARNING: Do not use your computer during script execution!
var itunes = Application('iTunes');
itunes.activate();
var system = Application('System Events');
var proc = system.processes.byName('iTunes').windows[0];
// Set the correct context of search
proc.radioGroups.at(1).radioButtons.byName("New").click();
var not_found = [];
// Init your own tracks list (see: Yandex.Music export example)
var tracks = [
"(Exchange) - Massive Attack",
"Please Don't Let Me Be Misunderstood - Nina Simone",
"Ungodly Fruit - Wax Tailor",
"La Ritournelle - Sebastien Tellier",
"Skinny Love - Birdy",
"Demons - Imagine Dragons",
"Jailhouse Rock - Elvis Presley",
"Fish Don't Scream - Serj Tankian"
];
// Add tracks to the My Library
tracks.forEach(function(t){
try {
// Search for the songs not for the albums
proc.textFields.at(0).value.set("song " + t);
proc.textFields.at(0).focused = true;
// Wait a bit for a popup to appear
delay(1);
// Search! (Press Return)
system.keyCode(36);
// Wait for a web page to load
delay(1);
// If the song was found…
if (/^Song by/.test(proc.scrollAreas.at(1).uiElements.at(0).groups.at(0).lists.at(0).groups.at(1).staticTexts.at(0).value()))
{
// Click on the (●●●) button next to the Cover
proc.scrollAreas.at(1).uiElements.at(0).groups.at(0).lists.at(0).groups.at(0).groups.at(1).groups.at(0).click();
proc.scrollAreas.at(1).uiElements.at(0).groups.at(0).lists.at(0).groups.at(0).groups.at(1).groups.at(0).focused = true;
// Put cursor to the popup-menu
system.keyCode(36);
// Go to the very top of the menu
// 10 times is an important constant
// which allows to RErun script again if needed
for (var i = 0; i < 9; i++) {
system.keyCode(126);
}
// Add the song to the My Music
system.keyCode(36);
// Wait a bit for data to be send
delay(2);
} else {
throw 42; // because why not? :)
}
} catch (e) {
// Add the song to the "Not found" list
not_found.push(t);
}
});
// Output all songs which haven't been found
not_found;
// Example of Yandex.Music playlist export using
// https://github.com/itsmepetrov/yandex-music-api
// List of "Song Title - Artist" strings could be parsed
// from any service in any convenient way and then should
// be added to the initial tracks array in JXA script
var YandexMusicApi = require('yandex-music-api');
var api = new YandexMusicApi();
api.init({username: 'user_goes_here', password: 'password_should_be_here'}).then(function(token) {
console.log("Boss, I'm in!");
var options = { 'rich-tracks': true };
// ID 1005 I got using getUserPlaylists method https://goo.gl/ziY5oj
return api.getPlaylists(null, [1005], options).then(function(playlist) {
console.log(playlist[0].tracks.length);
var artist_name = '';
playlist[0].tracks.forEach(function(item) {
if (item.track.artists.length > 0 && item.track.artists[0].hasOwnProperty('name')) {
artist_name = item.track.artists[0].name;
} else {
artist_name = '';
}
console.log('"' + item.track.title + ' - ' + artist_name + '",');
});
return playlist[0];
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment