macos support

This commit is contained in:
2025-04-08 14:44:20 -07:00
parent e3297c7983
commit 0961746560
3 changed files with 55 additions and 4 deletions

17
MediaControl.scpt Normal file
View File

@@ -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

22
get-media.sh Executable file
View File

@@ -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"

20
main.js
View File

@@ -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";