Skip to content

Commit

Permalink
Added compatibility with MacOS <10.15 along with >=10.15
Browse files Browse the repository at this point in the history
  • Loading branch information
kkevinm committed Jan 9, 2020
1 parent e2646c3 commit 64e9175
Show file tree
Hide file tree
Showing 4 changed files with 146 additions and 32 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>com.github.kevinmussi</groupId>
<artifactId>itunes-discord-rp</artifactId>
<version>1.1</version>
<version>1.2</version>
<packaging>jar</packaging>
<name>iTunes Discord RP</name>
<properties>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

public enum OperativeSystem {
MACOS("MacOS", "macos", ".applescript", "osascript", "/Library/Application Support"),
MACOS15("MacOS", "macos15", ".applescript", "osascript", "/Library/Application Support"),
WINDOWS("Windows", "windows", ".js", "Cscript.exe", "/AppData/Local"),
OTHER("Other", "", "", "", "");

Expand All @@ -26,7 +27,14 @@ private OperativeSystem(String description, String scriptPath,
public static OperativeSystem getOS() {
String os = System.getProperty("os.name");
if(os.startsWith("Mac OS")) {
return MACOS;
// Add check for OS X 10.15 or higher for the new music app
String version = System.getProperty("os.version");
int osxVersion = Integer.valueOf(version.split("\\.")[1]);
if(osxVersion >= 15) {
return MACOS15;
} else {
return MACOS;
}
} else if(os.startsWith("Windows")) {
return WINDOWS;
} else {
Expand Down
108 changes: 78 additions & 30 deletions src/main/resources/scripts/macos/itunes_track_info_script.applescript
Original file line number Diff line number Diff line change
@@ -1,3 +1,15 @@
# Check for OS X version, if >= 10.15 then set music app name to "Music" instead of "iTunes"
set sysinfo to system info
set OS_VERSION to system version of sysinfo

considering numeric strings
if OS_VERSION "10.15" then
set MUSIC_APP to "Music"
else
set MUSIC_APP to "iTunes"
end if
end considering

set STR_STOPPED to "STOPPED"
set STR_PLAYING to "PLAYING"
set STR_PAUSED to "PAUSED"
Expand All @@ -10,43 +22,79 @@ property savedPlayerState : missing value
repeat
delay 1
# If iTunes is not running, set the state to "STOPPED" and do nothing
if application "Music" is not running then
if application MUSIC_APP is not running then
if state is not STR_STOPPED then
set state to STR_STOPPED
log state
end if
else
# If iTunes is running, execute the logic inside a try block so that, if iTunes is closed during the execution, the script doesn't launch an exception
try
tell application "Music"
set currentPlayerState to player state

if currentPlayerState is not playing and currentPlayerState is not paused then
# If there's no song playing or paused, set the state to "STOPPED" and do nothing
if state is not STR_STOPPED then
set state to STR_STOPPED
log state
end if
else
set currentTrack to current track

# If the current song or the player status has changed, log the new track information
if currentPlayerState is not savedPlayerState or currentTrack is not savedTrack then
if currentPlayerState is playing then
set state to STR_PLAYING
else
set state to STR_PAUSED
end if

# Write the track main information in the log
log name of currentTrack & ";;" & artist of currentTrack & ";;" & album of currentTrack & ";;" & state & ";;" & player position & ";;" & duration of currentTrack & ";;" & track number of currentTrack & ";;" & track count of currentTrack
end if

# Update the player state and song name to detect when the track changes
set savedPlayerState to currentPlayerState
set savedTrack to currentTrack
end if
end tell
# Apparently we can't use variables with tell so I have to replicate this block
if MUSIC_APP is "iTunes" then
tell application "iTunes"
set currentPlayerState to player state

if currentPlayerState is not playing and currentPlayerState is not paused then
# If there's no song playing or paused, set the state to "STOPPED" and do nothing
if state is not STR_STOPPED then
set state to STR_STOPPED
log state
end if
else
set currentTrack to current track

# If the current song or the player status has changed, log the new track information
if currentPlayerState is not savedPlayerState or currentTrack is not savedTrack then
if currentPlayerState is playing then
set state to STR_PLAYING
else
set state to STR_PAUSED
end if

# Write the track main information in the log
log name of currentTrack & ";;" & artist of currentTrack & ";;" & album of currentTrack & ";;" & state & ";;" & player position & ";;" & duration of currentTrack & ";;" & track number of currentTrack & ";;" & track count of currentTrack
end if

# Update the player state and song name to detect when the track changes
set savedPlayerState to currentPlayerState
set savedTrack to currentTrack
end if
end tell
else if MUSIC_APP is "Music" then
tell application "Music"
set currentPlayerState to player state

if currentPlayerState is not playing and currentPlayerState is not paused then
# If there's no song playing or paused, set the state to "STOPPED" and do nothing
if state is not STR_STOPPED then
set state to STR_STOPPED
log state
end if
else
set currentTrack to current track

# If the current song or the player status has changed, log the new track information
if currentPlayerState is not savedPlayerState or currentTrack is not savedTrack then
if currentPlayerState is playing then
set state to STR_PLAYING
else
set state to STR_PAUSED
end if

# Write the track main information in the log
log name of currentTrack & ";;" & artist of currentTrack & ";;" & album of currentTrack & ";;" & state & ";;" & player position & ";;" & duration of currentTrack & ";;" & track number of currentTrack & ";;" & track count of currentTrack
end if

# Update the player state and song name to detect when the track changes
set savedPlayerState to currentPlayerState
set savedTrack to currentTrack
end if
end tell
else
set state to STR_STOPPED
log state
end if
on error
# If iTunes is closed, set the state to "STOPPED" and do nothing
if state is not STR_STOPPED then
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
set STR_STOPPED to "STOPPED"
set STR_PLAYING to "PLAYING"
set STR_PAUSED to "PAUSED"

set state to ""

property savedTrack : missing value
property savedPlayerState : missing value

repeat
delay 1
# If iTunes is not running, set the state to "STOPPED" and do nothing
if application "Music" is not running then
if state is not STR_STOPPED then
set state to STR_STOPPED
log state
end if
else
# If iTunes is running, execute the logic inside a try block so that, if iTunes is closed during the execution, the script doesn't launch an exception
try
tell application "Music"
set currentPlayerState to player state

if currentPlayerState is not playing and currentPlayerState is not paused then
# If there's no song playing or paused, set the state to "STOPPED" and do nothing
if state is not STR_STOPPED then
set state to STR_STOPPED
log state
end if
else
set currentTrack to current track

# If the current song or the player status has changed, log the new track information
if currentPlayerState is not savedPlayerState or currentTrack is not savedTrack then
if currentPlayerState is playing then
set state to STR_PLAYING
else
set state to STR_PAUSED
end if

# Write the track main information in the log
log name of currentTrack & ";;" & artist of currentTrack & ";;" & album of currentTrack & ";;" & state & ";;" & player position & ";;" & duration of currentTrack & ";;" & track number of currentTrack & ";;" & track count of currentTrack
end if

# Update the player state and song name to detect when the track changes
set savedPlayerState to currentPlayerState
set savedTrack to currentTrack
end if
end tell
on error
# If iTunes is closed, set the state to "STOPPED" and do nothing
if state is not STR_STOPPED then
set state to STR_STOPPED
log state
end if
end try
end if
end repeat

0 comments on commit 64e9175

Please sign in to comment.