macotron.media
Read the track that macOS shows in Now Playing, from Music, Spotify, a browser, or any other app, and send it play, pause, next, and previous.
// Show the current track in the menu bar.
function paint(info) {
info = info || macotron.media.nowPlaying();
macotron.menubar.status("now-playing", {
title: info.title || "Nothing playing",
sfSymbol: info.playing ? "play.fill" : "pause.fill",
onClick: () => macotron.media.playPause(),
});
}
paint();
macotron.on("media:changed", paint);
Reading
nowPlaying(): { playing, title, artist, album, app, bundle, artwork? }
The current Now Playing item. playing is true while playback runs. app is the player's display name and bundle its bundle ID. When nothing is playing, playing is false and the strings are empty.
artwork, when present, is the path of a JPEG cover in the temporary folder. Macotron looks the track up in the iTunes Search API by artist and title, so the cover can be missing or wrong for tracks that are not in Apple's catalog.
When several apps report media, a playing app wins over a paused one, and a music player wins over a browser or chat app.
Each read starts /usr/bin/osascript, so a call can take a moment. Macotron reuses a reading for 5 seconds. While a plugin listens for media:changed, a watcher keeps the reading current and calls return at once. The first read of a new track can arrive without artwork; the cover follows in media:changed.
Controls
These send a command to the app that owns Now Playing. They return nothing; read nowPlaying() or wait for media:changed to see the result.
playPause(): void
Toggles between play and pause.
next(): void
Skips to the next track.
previous(): void
Goes to the previous track.
Events
media:changed
Fires when the play state, track, player, or artwork changes. The payload has the same shape as nowPlaying().
Macotron starts the watcher only when a plugin registers a media:changed listener while it loads. A listener added later, for example inside a timer, gets no events until the next reload. Register it at the top level of your plugin.
See also
- macotron.audio for devices and volume.
- The
now-playing.jsandheadphone-pause.jsplugins.