-
Notifications
You must be signed in to change notification settings - Fork 0
/
restore.js
executable file
·40 lines (33 loc) · 1.03 KB
/
restore.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#!/usr/bin/osascript -l JavaScript
var app = Application('iTunes');
app.includeStandardAdditions = true;
function run(theId) {
theId = String(theId).trim();
var playlist = app.playlists.whose({persistentID: theId})()[0];
if(!playlist) {
console.log(`Playlist not known: ${theId}`);
return;
}
var contents = String(app.theClipboard()).trim();
if(!contents) {
console.log('Clipboard empty');
}
var ids = contents.split(/[\n\r]+/g);
if(ids[0].indexOf(':') > -1) {
var header = ids.shift();
// This should contain the playlist ID/name
if(header.split(':')[0] !== theId) {
console.log('Playlist to be restored does not match selected playlist. Leave off playlist descriptor to force.');
}
}
// Remove existing tracks
app.delete(playlist.tracks);
// Restore the tracks from the clipboard
ids.forEach(function(id) {
var track = app.tracks.whose({persistentID: id})()[0];
if(!track) {
return console.log(`track ${id} cannot be found and will not be restored`);
}
app.duplicate(track, {to: playlist});
});
}