diff --git a/MediaControl.scpt b/MediaControl.scpt new file mode 100644 index 0000000..ba4232f --- /dev/null +++ b/MediaControl.scpt @@ -0,0 +1,17 @@ +on run argv + set var to item 1 of argv + + if var is "playpause" then + using terms from application "Spotify" + tell application "Spotify" to playpause + end using terms from + else if var is "next" then + using terms from application "Spotify" + tell application "Spotify" to next track + end using terms from + else if var is "previous" then + using terms from application "Spotify" + tell application "Spotify" to previous track + end using terms from + end if +end run \ No newline at end of file diff --git a/get-media.sh b/get-media.sh new file mode 100755 index 0000000..2d198bd --- /dev/null +++ b/get-media.sh @@ -0,0 +1,22 @@ +#!/bin/bash + +# Get the currently playing Spotify track title +process_info=$(osascript -e 'tell application "Spotify" to if player state is playing then name of current track & " - " & artist of current track') + +if [[ -n "$process_info" ]]; then + if [[ "$process_info" =~ ^(.*)\ -\ (.*)$ ]]; then + artist="${BASH_REMATCH[2]}" + songTitle="${BASH_REMATCH[1]}" + else + artist="Unknown" + songTitle="$process_info" + fi +else + artist="" + songTitle="" +fi + +# Create JSON output +json_output=$(jq -n --arg title "$songTitle" --arg artist "$artist" --arg amUri "" '{title: $title, artist: $artist, amUri: $amUri}') + +echo "$json_output" \ No newline at end of file diff --git a/main.js b/main.js index 781e2f7..280d4dd 100644 --- a/main.js +++ b/main.js @@ -7,6 +7,18 @@ let win; let lastSong = ""; let lastPaused = true; +if (process.platform === 'win32') { + getMediaCommand = 'powershell -ExecutionPolicy Bypass -File get-media.ps1'; + mediaControlCommand = 'MediaControl.exe'; + } else if (process.platform === 'darwin') { + getMediaCommand = './get-media.sh'; // Adjust this path/command to your macOS script + mediaControlCommand = 'osascript MediaControl.scpt'; + } else { + console.error("Unsupported OS"); + resolve({ updated: false }); + return; + } + app.whenReady().then(() => { createWindow(); checkNowPlaying(); @@ -52,7 +64,7 @@ async function checkNowPlaying() { function checkOwnSong() { return new Promise((resolve) => { - exec(`powershell -ExecutionPolicy Bypass -File get-media.ps1`, async (error, stdout) => { + exec(getMediaCommand, async (error, stdout) => { if (error) { console.error("PowerShell error:", error); resolve({ updated: false }); @@ -139,7 +151,7 @@ function fetchAlbumArt(title, artist) { ipcMain.on('media-control', (event, command) => { if (command) { - exec(`MediaControl.exe ${command}`, (error, stdout, stderr) => { + exec(`${mediaControlCommand} ${command}`, (error, stdout, stderr) => { if (error) { console.error(`Error running MediaControl.exe:`, error); } else { @@ -398,10 +410,10 @@ function getMainPageHTML() { if (data.paused) { // Player is paused => show "Play" button - playPauseButton.textContent = "▶️"; + playPauseButton.textContent = "⏵︎"; } else { // Player is playing => show "Pause" button and update track info - playPauseButton.textContent = "⏸️"; + playPauseButton.textContent = "⏸︎"; songTitleElem.innerText = data.title ?? "Unknown Title"; songArtistElem.innerText = data.artist ?? "Unknown Artist";