-
Notifications
You must be signed in to change notification settings - Fork 4
/
applescript.js
64 lines (61 loc) · 2.11 KB
/
applescript.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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
var applescript = require("applescript");
var { writeFileSync } = require("fs");
const { Upload } = require("./s3");
// Very basic AppleScript command. Returns the song name of each
// currently selected track in iTunes as an 'Array' of 'String's.
// var script = 'tell application "Music" to get properties of current track';
var script = `tell application "Music" to tell artwork 1 of current track
return data
end tell`;
const ExecuteAppleScript = async (prev) => {
return new Promise((resolve, reject) => {
applescript.execString(script, function (err, artwork) {
if (err) {
// Something went wrong!
console.log(err);
}
applescript.execString(
`tell application "Music" to get properties of current track`,
async (err, song) => {
let songObj = {};
song.forEach((s) => {
if (typeof s === "string") {
let [key, value] = s.split(":");
songObj[key] = value ? value.split('"').join("") : null;
}
});
if (JSON.stringify(song).includes(prev.song)) {
resolve(prev);
} else {
const upload = await Upload({
song: songObj.name,
artist: songObj.artist,
body: artwork,
});
if (upload.$metadata.httpStatusCode !== 200) {
console.log(
"Error uploading image",
upload.$metadata.httpStatusCode
);
} else {
console.log("Uploaded image");
}
resolve({
// s3up: upload,
url:
"https://jack-general.nyc3.digitaloceanspaces.com/apple-music-rich-presence/" +
encodeURI(songObj.name.replaceAll("/", "")).slice(0, 100) +
"-" +
encodeURI(songObj.artist.replaceAll("/", "")).slice(0, 100) +
".jpeg",
song: songObj.name,
artist: songObj.artist,
album: songObj.album,
});
}
}
);
});
});
};
module.exports = { ExecuteAppleScript };