From 48a263e770239c117146cddf5c08376d74952ccc Mon Sep 17 00:00:00 2001 From: Daniel Machin Date: Tue, 28 Jul 2020 13:00:58 -0400 Subject: [PATCH 1/3] Add macOS Catalina compatibility --- .gitignore | 1 + README.md | 2 +- app.js | 42 +++++++++++++++++++++++++++++++++++------- lib/airplay.js | 6 +++++- lib/art.applescript | 31 ++++++++++++++++++++++++++++++- package.json | 2 +- 6 files changed, 73 insertions(+), 11 deletions(-) diff --git a/.gitignore b/.gitignore index eb03e3e..740a0a4 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ node_modules *.log +package-lock.json diff --git a/README.md b/README.md index 9457934..652c0a9 100755 --- a/README.md +++ b/README.md @@ -7,7 +7,7 @@ you can integrate its control into any of your own software. This is especially convenient for integrating into any sort of Home Automation you might have. -Last confirmed working version of iTunes: `12.2.1`. +Last confirmed working version of Apple Music: `1.0.6.10`. ## Features diff --git a/app.js b/app.js index e7b49a4..258a57d 100755 --- a/app.js +++ b/app.js @@ -18,7 +18,11 @@ var logFormat = "'[:date[iso]] - :remote-addr - :method :url :status :response-t app.use(morgan(logFormat)) function getCurrentState(){ - itunes = Application('iTunes'); + try { + itunes = Application('Music'); + } catch (error) { + itunes = Application('iTunes'); + } playerState = itunes.playerState(); currentState = {}; @@ -63,7 +67,11 @@ function sendResponse(error, res){ } function playPlaylist(nameOrId){ - itunes = Application('iTunes'); + try { + itunes = Application('Music'); + } catch (error) { + itunes = Application('iTunes'); + } if ((nameOrId - 0) == nameOrId && ('' + nameOrId).trim().length > 0) { id = parseInt(nameOrId); @@ -76,7 +84,11 @@ function playPlaylist(nameOrId){ } function setVolume(level){ - itunes = Application('iTunes'); + try { + itunes = Application('Music'); + } catch (error) { + itunes = Application('iTunes'); + } if (level) { itunes.soundVolume = parseInt(level); @@ -87,7 +99,11 @@ function setVolume(level){ } function setMuted(muted){ - itunes = Application('iTunes'); + try { + itunes = Application('Music'); + } catch (error) { + itunes = Application('iTunes'); + } if (muted) { itunes.mute = muted; @@ -98,7 +114,11 @@ function setMuted(muted){ } function setShuffle(mode){ - itunes = Application('iTunes'); + try { + itunes = Application('Music'); + } catch (error) { + itunes = Application('iTunes'); + } if (!mode) { mode = "songs" @@ -115,7 +135,11 @@ function setShuffle(mode){ } function setRepeat(mode){ - itunes = Application('iTunes'); + try { + itunes = Application('Music'); + } catch (error) { + itunes = Application('iTunes'); + } if (!mode) { mode = "all" @@ -131,7 +155,11 @@ function setRepeat(mode){ } function getPlaylistsFromItunes(){ - itunes = Application('iTunes'); + try { + itunes = Application('Music'); + } catch (error) { + itunes = Application('iTunes'); + } playlists = itunes.playlists(); playlistNames = []; diff --git a/lib/airplay.js b/lib/airplay.js index 3c16c54..500faf4 100644 --- a/lib/airplay.js +++ b/lib/airplay.js @@ -1,7 +1,11 @@ module.exports = { listAirPlayDevices: function (callback){ - itunes = Application('iTunes'); + try { + itunes = Application('Music'); + } catch (error) { + itunes = Application('iTunes'); + } airPlayDevices = itunes.airplayDevices(); airPlayResults = []; for (i = 0; i < airPlayDevices.length; i++) { diff --git a/lib/art.applescript b/lib/art.applescript index 0f23e17..4e344c6 100644 --- a/lib/art.applescript +++ b/lib/art.applescript @@ -1 +1,30 @@ -on write_to_file(this_data, target_file, append_data) try set the target_file to the target_file as string log target_file set the open_target_file to open for access target_file with write permission log "trying" if append_data is false then set eof of the open_target_file to 0 write this_data to the open_target_file starting at eof close access the open_target_file return true on error log "failed" try close access file target_file end try return false end try end write_to_file tell application "iTunes" try set aTrack to the current track set aArtwork to data of artwork 1 of aTrack my write_to_file(aArtwork, "/tmp/currently-playing.jpg", false) on error log "error getting info" end try end tell \ No newline at end of file +on write_to_file(this_data, target_file, append_data) + try + set the target_file to the target_file as string + log target_file + set the open_target_file to open for access target_file with write permission + log "trying" + if append_data is false then set eof of the open_target_file to 0 + write this_data to the open_target_file starting at eof + close access the open_target_file + return true + on error + log "failed" + try + close access file target_file + end try + return false + end try +end write_to_file + +tell application "Music" + tell application "iTunes" + try + set aTrack to the current track + set aArtwork to data of artwork 1 of aTrack + my write_to_file(aArtwork, "/tmp/currently-playing.jpg", false) + on error + log "error getting info" + end try + end tell +end tell diff --git a/package.json b/package.json index 3b6edb2..6412f4a 100755 --- a/package.json +++ b/package.json @@ -18,7 +18,7 @@ "express": "^4.12.4", "morgan": "^1.6.1", "body-parser": "^1.12.4", - "local-itunes": "^0.3.0", + "local-itunes": "^0.4.0", "osa": "2.2.0", "osascript": "1.2.0", "parameterize": "^0.0.7" From 12613ae360c65c6b9fe62cf3b724430775dcf5ed Mon Sep 17 00:00:00 2001 From: Daniel Machin Date: Tue, 28 Jul 2020 13:04:28 -0400 Subject: [PATCH 2/3] Fix artwork applescript --- lib/art.applescript | 2 -- 1 file changed, 2 deletions(-) diff --git a/lib/art.applescript b/lib/art.applescript index 4e344c6..cbe097c 100644 --- a/lib/art.applescript +++ b/lib/art.applescript @@ -18,7 +18,6 @@ on write_to_file(this_data, target_file, append_data) end write_to_file tell application "Music" - tell application "iTunes" try set aTrack to the current track set aArtwork to data of artwork 1 of aTrack @@ -26,5 +25,4 @@ tell application "Music" on error log "error getting info" end try - end tell end tell From a6e7705038050df45c2381951a48b977021a3fc8 Mon Sep 17 00:00:00 2001 From: Daniel Machin Date: Tue, 28 Jul 2020 13:09:29 -0400 Subject: [PATCH 3/3] Clean line endings --- lib/art.applescript | 56 ++++++++++++++++++++++----------------------- 1 file changed, 28 insertions(+), 28 deletions(-) diff --git a/lib/art.applescript b/lib/art.applescript index cbe097c..08c6a04 100644 --- a/lib/art.applescript +++ b/lib/art.applescript @@ -1,28 +1,28 @@ -on write_to_file(this_data, target_file, append_data) - try - set the target_file to the target_file as string - log target_file - set the open_target_file to open for access target_file with write permission - log "trying" - if append_data is false then set eof of the open_target_file to 0 - write this_data to the open_target_file starting at eof - close access the open_target_file - return true - on error - log "failed" - try - close access file target_file - end try - return false - end try -end write_to_file - -tell application "Music" - try - set aTrack to the current track - set aArtwork to data of artwork 1 of aTrack - my write_to_file(aArtwork, "/tmp/currently-playing.jpg", false) - on error - log "error getting info" - end try -end tell +on write_to_file(this_data, target_file, append_data) + try + set the target_file to the target_file as string + log target_file + set the open_target_file to open for access target_file with write permission + log "trying" + if append_data is false then set eof of the open_target_file to 0 + write this_data to the open_target_file starting at eof + close access the open_target_file + return true + on error + log "failed" + try + close access file target_file + end try + return false + end try +end write_to_file + +tell application "Music" + try + set aTrack to the current track + set aArtwork to data of artwork 1 of aTrack + my write_to_file(aArtwork, "/tmp/currently-playing.jpg", false) + on error + log "error getting info" + end try +end tell