Skip to content

Instantly share code, notes, and snippets.

@SKaplanOfficial
Last active March 24, 2026 14:43
Show Gist options
  • Select an option

  • Save SKaplanOfficial/f9f5bdd6455436203d0d318c078358de to your computer and use it in GitHub Desktop.

Select an option

Save SKaplanOfficial/f9f5bdd6455436203d0d318c078358de to your computer and use it in GitHub Desktop.
AppleScript and JXA scripts to get Now Playing info. Works on macOS 15.4+, including macOS 26 beta 1.
function run() {
const MediaRemote = $.NSBundle.bundleWithPath('/System/Library/PrivateFrameworks/MediaRemote.framework/');
MediaRemote.load
const MRNowPlayingRequest = $.NSClassFromString('MRNowPlayingRequest');
const appName = MRNowPlayingRequest.localNowPlayingPlayerPath.client.displayName;
const infoDict = MRNowPlayingRequest.localNowPlayingItem.nowPlayingInfo;
const title = infoDict.valueForKey('kMRMediaRemoteNowPlayingInfoTitle');
const album = infoDict.valueForKey('kMRMediaRemoteNowPlayingInfoAlbum');
const artist = infoDict.valueForKey('kMRMediaRemoteNowPlayingInfoArtist');
return `${title.js}${album.js}${artist.js} | ${appName.js}`;
}
use framework "AppKit"
on run
set MediaRemote to current application's NSBundle's bundleWithPath:"/System/Library/PrivateFrameworks/MediaRemote.framework/"
MediaRemote's load()
set MRNowPlayingRequest to current application's NSClassFromString("MRNowPlayingRequest")
set appName to MRNowPlayingRequest's localNowPlayingPlayerPath()'s client()'s displayName()
set infoDict to MRNowPlayingRequest's localNowPlayingItem()'s nowPlayingInfo()
set title to (infoDict's valueForKey:"kMRMediaRemoteNowPlayingInfoTitle") as text
set album to (infoDict's valueForKey:"kMRMediaRemoteNowPlayingInfoAlbum") as text
set artist to (infoDict's valueForKey:"kMRMediaRemoteNowPlayingInfoArtist") as text
return title & " - " & album & " - " & artist & " | " & appName
end run
@Pen7art
Copy link
Copy Markdown

Pen7art commented Jun 3, 2025

Hey do you know if there is any way to know if the track is playing or if it's paused?

@SKaplanOfficial
Copy link
Copy Markdown
Author

@Pen7art You can use the kMRMediaRemoteNowPlayingInfoPlaybackRate key, which is 0 when paused and 1 when playing.

const isPlaying = infoDict.valueForKey('kMRMediaRemoteNowPlayingInfoPlaybackRate').js == 1;

@starsea
Copy link
Copy Markdown

starsea commented Jun 14, 2025

how to pause the player? use applescript press space? is there another way?

@SKaplanOfficial
Copy link
Copy Markdown
Author

SKaplanOfficial commented Jun 14, 2025

@starsea Like this:

use framework "AppKit"

property play : 0
property pause : 1
property togglePlayPause : 2
property stop : 3
property nextTrack: 4
property previousTrack: 5

on run
	-- Load MediaRemote classes into the ObjC runtime
	set MediaRemote to current application's NSBundle's bundleWithPath:"/System/Library/PrivateFrameworks/MediaRemote.framework/"
	MediaRemote's load()
	
	-- Run some media playback command
	my sendCommand(play)
	delay 2
	
	my sendCommand(pause)
	delay 2
	
	my sendCommand(togglePlayPause)
	delay 2
	
	my sendCommand(stop)
end run

-- Sends a media command to the local player (i.e. your computer, not an external device)
on sendCommand(theCommand)
	set MRNowPlayingController to current application's NSClassFromString("MRNowPlayingController")
	set commandOptions to current application's NSDictionary's alloc()'s init()
	set controller to MRNowPlayingController's localRouteController()
	controller's sendCommand:theCommand options:commandOptions completion:(missing value)
end sendCommand

or in JXA:

ObjC.import('Foundation');

play = 0;
pause = 1;
togglePlayPause = 2;
stop = 3;
nextTrack = 4;
previousTrack = 5;

function run() {
	// Load MediaRemote classes into the ObjC runtime
	const MediaRemote = $.NSBundle.bundleWithPath('/System/Library/PrivateFrameworks/MediaRemote.framework/');
	MediaRemote.load
	
	// Run some media playback command
	sendCommand(play);
	delay(2);
	
	sendCommand(pause);
	delay(2);
	
	sendCommand(togglePlayPause);
	delay(2);
	
	sendCommand(stop);
}

// Sends a media command to the local player (i.e. your computer, not an external device)
function sendCommand(command) {
	const MRNowPlayingController = $.NSClassFromString('MRNowPlayingController');
	const controller = MRNowPlayingController.localRouteController;
	const commandOptions = $.NSDictionary.alloc.init;
	controller.sendCommandOptionsCompletion(command, commandOptions, null);
}

@ungive
Copy link
Copy Markdown

ungive commented Jun 15, 2025

To anyone stumbling upon this, looking for a solution to use the MediaRemote framework in its full capacity, I found a solution to load it on all versions of macOS, including 15.4 and above: https://github.com/ungive/mediaremote-adapter

@Pen7art
Copy link
Copy Markdown

Pen7art commented Jul 15, 2025

@SKaplanOfficial thank you for the answer. Btw do you know why browsers elapsed time doesn't update?

@ungive
Copy link
Copy Markdown

ungive commented Jul 15, 2025

@Pen7art Check this for why elapsed time appears as not being updated: ungive/mediaremote-adapter#6

@SKaplanOfficial
Copy link
Copy Markdown
Author

@Pen7art @ungive If you just want the exact time elapsed, you can use $.MRNowPlayingRequest.localNowPlayingItem.metadata.calculatedPlaybackPosition.

@ejbills
Copy link
Copy Markdown

ejbills commented Nov 29, 2025

@SKaplanOfficial - wasn't able to reproduce the command functionality on macOS 26.1. Reading media works, however writing to the media client seems to be blocked. Can anyone else confirm?

@SKaplanOfficial
Copy link
Copy Markdown
Author

@ejbills Commands (play, pause, etc.) work for me on the 26.2 beta, tested with built-in apps, Spotify, and YouTube. I can't easily test on 26.1 at this point, but if there was a bug, it seems to be fixed.

@kreatoo
Copy link
Copy Markdown

kreatoo commented Feb 9, 2026

@SKaplanOfficial is there a way to get album artwork using this? Thanks.

@ejbills
Copy link
Copy Markdown

ejbills commented Feb 9, 2026

@kreatoo try kMRMediaRemoteNowPlayingInfoArtworkData

@ungive
Copy link
Copy Markdown

ungive commented Feb 9, 2026

@kreatoo Last time I tried I couldn't get the artwork to load with JXA/osascript, it would just be empty

@kreatoo
Copy link
Copy Markdown

kreatoo commented Feb 10, 2026

Yeah, seems so. kMRMediaRemoteNowPlayingInfoArtworkData is just returning null. Thanks for the help!

@SKaplanOfficial
Copy link
Copy Markdown
Author

@kreatoo In case it's still relevant: seems MediaRemote can't do it, but MediaPlayer can (but only for Music.app, I think).

use framework "Foundation"
use framework "MediaPlayer"
use scripting additions

-- Get the system-wide music player
set player to current application's MPMusicPlayerController's systemMusicPlayer()

-- Get item for the current track
set nowPlayingItem to player's nowPlayingItem()

if nowPlayingItem is not missing value then
	-- Get artwork as 500x500px PNG image
	set artworkImage to nowPlayingItem's artwork()'s imageWithSize:{500, 500}
	set imageRep to (artworkImage's representations()'s objectAtIndex:0)
	set artworkData to imageRep's representationUsingType:(current application's NSPNGFileType) |properties|:(missing value)
	
	-- Output to downloads directory
	set filePath to POSIX path of (path to downloads folder) & "artwork.png"
	artworkData's writeToFile:filePath atomically:false
else
	return false
end if

JXA equivalent:

(() => {
	ObjC.import('MediaPlayer');
	
	// Get the system-wide music player
	const player = $.MPMusicPlayerController.systemMusicPlayer;
	
	// Get item for the current track
	const currentTrack = player.nowPlayingItem.item;
	
	if (currentTrack) {
		// Get artwork as 500x500px PNG image
		const artworkImage = currentTrack.artwork.imageWithSize((500,500));
		const artworkData = artworkImage.representations.objectAtIndex(0).representationUsingTypeProperties($.NSPNGFileType, $({}));
		
		// Output to downloads directory
		return artworkData.writeToFileAtomically(`${Application('System Events').downloadsFolder.posixPath()}/artwork.png`, false);
	} else {
		return false;
	}
})()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment