Categories
Mac

Display the currently playing track in iTunes/Spotify on the Touch Bar

Ever since I got my 2016 15″ MacBook Pro with Touch Bar, I wanted to have it show one thing: the currently playing track. Today, thanks to the amazing Better Touch Tool and some Apple Script (which I don’t like, but it gets the job done), I finally have that functionality.

It’s really easy:

  1. Open Better Touch Tool and click on the Touch Bar section.
  2. Add a widget and select the “Run Apple Script and Show Return Value” option.
  3. Click on the “Advanced Configuration” button and paste the relevant Apple Script (see below).
  4. Optional: set the Predefined Action to “Open Application / File / Apple Script …” and select iTunes/Spotify: this will always bring iTunes/Spotiy to the foreground when you tap the widget.
  5. Optional: set the iTunes/Spotify icon as the widget’s icon (You can right click on the .app bundle, click on “Show Package Contents”, then go to Contents/Resources. The icones are respectively iTunes.icns and Icon.icns, just drag them over their spot in BTT.)

Here is the script for iTunes:

And here is the one for Spotiy:

The only difference between the two is the name of the app in the first if and in the tell statement.

Better Touch Tool hides the widget if the Apple Script returns nothing (which is the case when the music is paused or when the iTunes/Spotify are closed): you can work that around by replacing the two return "" with return " ", i.e. returning a space when nothing is playing. It’s not very aesthetically pleasing on the Touch Bar because the button is asymmetric, but you can do that if you prefer it that way.

72 replies on “Display the currently playing track in iTunes/Spotify on the Touch Bar”

Grazie per gli ottimi script…. D’altronde bisogna aggiustarsi con BTT dal momento che praticamente nessuna App si sta adattando alla nuova TB…

Great tip.

If I press the cross to the left of the widget, it disappears. I find that I have to go into “advanced configuration” and press save, then it’s back.

Is there a better way of restoring the widget if closed? Or perhaps set it up permanently?

That’s a feature of BTT, as it allows you to dismiss its custom Touch Bar to reveal the system/app one. You can however define a shortcut in BTT that launches the action “Toggle BetterTouchTool Touch Bar” to quickly show/hide it.

Not really: the ability to show BTT’s custom buttons in any app is BTT-specific, not macOS native.

If you quit BTT, you will see that the (x) at the very left of the native control Strip is still there. It is not added by BTT, this is how Apple wants you to switch between the two Touch Bar layouts you choose in the native Keyboard settings (System Preferences).

BTT allows you to disable the macOS Control Strip, which will hide the X as it is part of that strip. Apparently, it’s not possible to hide part of the native control strip (the X): https://github.com/folivoraAI/BetterTouchTool/issues/875

What I meant is that if you hide BTT’s actions with the (x) you’re referring to, you can not make them appear again without either going into BTT or using a shortcut you defined in BTT itself (which is what I did).

hello, when I put the script in, and run script. after which I save but when I look one touch bar it just says the name ‘script:spotify’

any help?

update:

I get the script to work but, if I leave the bettertouchtool button from touch bar and go back it no longer displays name of song. just name of widget.

Is there anything I can do to limit the number of characters shown on that display? If a song name’s a bit longer than usual it takes up the entire touch bar.

It’s doable, replace line 7 of the script with:

set artistTitle to ((get artist of current track) & " - " & (get name of current track))
if length of artistTitle is less than 50 then
	set displayTitle to artistTitle
else
	set displayTitle to text 1 thru 50 of artistTitle
end if
return displayTitle

This displays the first 50 characters, you can adjust this arbitrary limit however you want (be sure to replace 50 in both lines!).

It’s doable, replace line 7 of the script with:

set artistTitle to ((get artist of current track) & " - " & (get name of current track))
if length of artistTitle is less than 50 then
	set displayTitle to artistTitle
else
	set displayTitle to text 1 thru 50 of artistTitle
end if
return displayTitle

This displays the first 50 characters, you can adjust this arbitrary limit however you want (be sure to replace 50 in both lines!).

The widget worked normally for while, but now it stops displaying the song that is actually playing. It doesn’t display the current song anymore, and is stuck on this song that has been already played 3/4 hours ago. Any help? Thanks!

Thanks a lot for this. One of those times where there is actually an application for the TouchBar and Spotify devs are not taking advantage,

I’ve tried to merge iTunes and Spotify playing together:

if application "iTunes" is running then
	tell application "iTunes"
		if player state is playing then
			return (get artist of current track) & " - " & (get name of current track)
		else if application "Spotify" is running then
			tell application "Spotify"
				if player state is playing then
					return (get artist of current track) & " - " & (get name of current track)
				else
					return ""
				end if
			end tell
		end if
	end tell
else
	return ""
end if

Hello,
That is what I’ve been looking for, thanks for the article.

I have critical questiosn though. When I add widget with itues(or whichever), it starts displaying but It stops displaying after I press button “X” which is displayed before ESC. and after that I need to to save widget again to see current track.
my touchbar have following buttons:
(X) ESC itunes-playing track touchbar controls.

Is there any way to get rid of (X) and always see currently playing track all the time regardless of application?

I have widget in global settings

I set the same settings of touchbar as you mentioned in screenshot here in the comments

That’s the expected behavior: the “X” closes BTT’s custom TouchBar.
I set a shortcut in the Keyboard section of the app that allows me to quickly open it back as needed.
You have to look for the “Toggle BetterTouchTool Touch Bar” action, which I mapped to ^⇧T.

edit: think i figured it out, i added a couple extra spaces between the artist and song title, that seems to have done the trick!

-- This script can be used in conjunction with Better Touch Tool to display the currently playing track on the MacBook Pro TouchBar
-- More info here: https://lucatnt.com/2017/02/display-the-currently-playing-track-in-itunesspotify-on-the-touch-bar
 
if application "iTunes" is running then
	tell application "iTunes"
		if player state is playing then
			return (get artist of current track) & "   -   " & (get name of current track)
		else
			return ""
		end if
	end tell
else
	return ""
end if

Hello Luca!

This is exactly what I was looking for but with one hitch, is it possible to also have the music controls with the name of the song/artist playing? This is so that you can see what is the next song when you skip etc… I tried making a custom script but to no avail. Any help would be appreciated! Thanks 🙂

You mean showing three buttons like these?
(⏮ the song I played before this one) (⏯ this song) (⏭ the next song)

I played around with AppleScript but I was unable to get the names of the previous and the next track in the currently playing playlist 😔

Hi thank you so much for this,
I know now it return a string with playing state,
I’m trying to make a button that can copy the “return”(currently playing track string) into my clipboard,
but didn’t work, is there a way can make it happen?

Hi,
actually it’s pretty easy. You just have to assign the “Run AppleScript in Background” action to your custom Touch Bar button, and enter the same script you use for Spotify/iTunes, only replacing line 7 with this one:

set the clipboard to ((get artist of current track) & " - " & (get name of current track))

Great!, thanks. i only mis the pause and next button now (in the same screen)

hey there

I’ve followed the steps, open up spotify and it just shows the date, time, GMT and year we are in.

Any ideas???
cheers

You need to remove the first line of the script. When you add a new Widget and click the ‘Advanced Configuration’ button, BTT puts a default script (`return do shell script “date” #example: execute a shell script and use the result`) that returns the Date/Time. That script is just an example, and should be removed.

Luca it’s awesome! I’m trying to do the same thing with SoundMate app 🙂 Looks like they have a different dictionary.. any help?

Brilliant!
Thanks so much!
Is there a way to add a custom button which would add the currently playing song in a spotify playlist?
Cheers!

Hi! Technically it would be possibile, it just wouldn’t be this easy: Spotify does not offer AppleScript actions to manage playlists, so it would need to be done through their API, which is not something I can whip out in a couple minutes.
Sorry I couldn’t be more helpful 😐

hi! this is great.. i just wanted to know if instead of the spotify or itunes icon, i could display the original album artwork for the song..
thanks..

I don’t think that’s possible, unfortunately: the icon can only be a static image.

I don’t think it’s possibile – well, not easily – since Sonos.app does not appear to have an AppleScript dictionary that could expose the current track.

Yes it is possible to add previous/next/play/pause buttons (you’d just need Apple scripts that trigger those actions), but they can’t replace the native now playing control AFAIK.

Thanks for this!, by the way…on the part when you said

“…Better Touch Tool hides the widget if the Apple Script returns nothing (which is the case when the music is paused or when the iTunes/Spotify are closed): you can work that around by replacing the two return “” with return ” “, i.e. returning a space when nothing is playing. It’s not very aesthetically pleasing on the Touch Bar because the button is asymmetric, but you can do that if you prefer it that way….”

1. No need put space, you can write anything cool, like ‘Play Music’ or ‘Open iTunes’ for example.
2. Also, Instead you can put space as advised, then change button colour to BLACK, the button will disappear leaving just the icon. I do this with custom text or app name, looks way cooler I think.

See: https://imgur.com/a/vkZCt2f

Hey really great tip !
I was wondering 2 extra features:
• If there would be a way to display itunes control touch bar features when press on the Better touch bar — iTunes: track ID.
• Instead of Itunes Icons: show album cover ?

Thanks !

Just saw that my question about The album cover is already here and impossible… sorry.

Is there any way to automatise the launching of the script at the end of the playing track instead of having the script running every X time ?

Thanks for this
But if I want to display lyrics on my Touch Bar.
How should I do ?

It’s not easy. You would have to find some API that would give you the timestamped lyrics and somehow sync it to the playback. I think it’d be more effort than it’s worth.

This for some reason stopped working for me. It now just doesn’t update when the song changes, even though my settings are correct. Anyone else having issues with the latest version of BetterTouchTool?

Would it be possible to make a tutorial on how to put lyrics I would really appreciate that.

Hi Luca, thanks for this but I wonder if you can help me. I am running the Mojave dev beta and so far I cannot get any version of iTunes now playing widget to work – it just doesn’t appear in the Touch Bar. Do you know if this is a know issue with AppleScript and Mojave that I just haver to wait on, or if there is a fix?

Sorry for double post but I thought this might be helpful – when I click “run script” in the settings window for the widget I get the error “not authorised to send Apple Events to iTunes”

I don’t really know, I’m not yet running Mojave on my main Mac so I haven’t tried debugging the issue. I’ll update this post when I upgrade and (hopefully) manage to solve this. Thanks for your comment with the error, it helps to point me in the right direction!

I’m getting the same but for the Spotify script (“Not authorized to send Apple events to Spotify”). Any ideas? 🙁

I didn’t like the way that BTT took over the touch bar (also, I didn’t want to pay for it), so the next-best way I could find was to add the “Now Playing” widget to the Notification Bar and when I want to see what’s playing it’s just a two-finger swipe from the right.
I think this is better than using the touch bar, and when I want to use that I don’t really need to see the track title.

Hi I don’t really understand how to get the spotify icons as the icon. Where is the .app bundle? Is it in BTT??

The .app bundle is the Spotify/iTunes app itself.
Look for it in the Applications folder, right click, show package contents, navigate to Contents/Resources and look for the .icns icon file.

Can this be done with the Google Play Desktop Player? If so how. I tried changing the word Spotify to ?Google play Desktop Player” but it did not work.

I don’t think so. Google Play Desktop Player doesn’t seem to offer AppleScript capabilities, AFAIK.

does anybody know what kind of apple script would need to enter if I wanted to save current playing song to library on Spotify?

thanks a lot!

Hi there, I used a lot pock, but as the developer stopped to update this app is getting not very stable (especially since Big Sur release). I started to use MTMR. It’s very customisable and you can achieve everything you want. If you don’t like the presets, you need to know a little of programming (writing on Json files and know the AppleScript commands) and you can make your Touch Bar doing whatever you want!

https://ibb.co/2cKLzPW
https://ibb.co/V2m9Tgd
https://ibb.co/pjY4s3H
https://ibb.co/JvKzHPC
https://ibb.co/cNfddqP

Leave a Reply to LucaTNT Cancel reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.