New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Displaying Spotify Track Name #308

Closed
mark248am opened this Issue Jan 23, 2017 · 33 comments

Comments

Projects
None yet
@mark248am

mark248am commented Jan 23, 2017

Hey, so I'm trying to get the track name to display in the TouchBar but instead of it appearing in the touch bar i am getting it as a system notification. Whats the right way to do it?

I went to TouchBar in BetterTouchTool.
I clicked on +Widget
I chose Run script/command
I copy pasted the "Another Simple Example" code from https://developer.spotify.com/applescript-api/
I clicked save.

Now I get a button that just says "Apple Script" and the track names are showing up system notifications every 5 seconds.

thanks!

screen shot 2017-01-23 at 10 41 32 pm

@fifafu

This comment has been minimized.

Collaborator

fifafu commented Jan 23, 2017

Hi,

use this script ( you can try if it produces the correct output by using the "Run Script" button from the screenshot you attached):

tell application "Spotify"
	return name of current track
end tell

//Edit: Scroll down for much better solutions!

@mark248am

This comment has been minimized.

mark248am commented Jan 23, 2017

oh wow that was super quick and it worked!!
How can I show artist as well?

@fifafu

This comment has been minimized.

Collaborator

fifafu commented Jan 23, 2017

tell application "Spotify"
	set currentArtist to artist of current track as string
	set currentTrack to name of current track as string
	
	return currentArtist & " - " & currentTrack
end tell
@mark248am

This comment has been minimized.

mark248am commented Jan 23, 2017

Damn thats perfect! Can't believe you got this feature done. Thanks!!

@mark248am

This comment has been minimized.

mark248am commented Jan 23, 2017

Would it be technically possible to add a forward button in the Touch Bar as well?

@fifafu

This comment has been minimized.

Collaborator

fifafu commented Jan 23, 2017

Forward is currently not possible I think, however skipping to the next song should already work using the predefined "next" action

@mark248am

This comment has been minimized.

mark248am commented Jan 23, 2017

I meant skipping to the next song, I was looking for the predefined "forward" or "skip" option didn't think of "next".

Also there is a minor but at the moment with track display. If the track name is too long it doesn't scroll. It also messes up the buttons, for example in the attached photo you can see my "Full Screen" button is now under the "Esc" button. This only happens after the script reloads. So it displays correctly when the new track starts, but on refresh it messes up.

@fifafu

This comment has been minimized.

Collaborator

fifafu commented Jan 23, 2017

Could you attach an export of your setup using the export button in BTT? (Makes it easier for me to reproduce the exact issue)

@mark248am

This comment has been minimized.

mark248am commented Jan 23, 2017

Here you go

chrome.zip

@mark248am

This comment has been minimized.

mark248am commented Jan 23, 2017

I changed the item placement for the 3 buttons on the left from "Scrollable Main Container" to "stick to left" and now instead of my button falling under "esc" its over "esc". Track still doesn't scroll.

@fifafu

This comment has been minimized.

Collaborator

fifafu commented Jan 23, 2017

http://boastr.net/releases/btt1.993.zip could you check if this version fixes the layout issue?

@mark248am

This comment has been minimized.

mark248am commented Jan 23, 2017

Ok Yes and No. The below tests was done with a long track name.

So if I have the item placement for track name to the left. It works. It no longer pushes the buttons to over lap or fall under the "Esc" button.

If I have the item placement for track name to "scrollable" it works like having it to the left. No scrolling, but also doesn't move any buttons under or over "esc".

On the other hand

If I have the item placement for track name to the right then the track name overlaps the buttons to its left. check below. Not a big deal honestly since it works fine when aligned left.

@pallavmac

This comment has been minimized.

pallavmac commented Jan 24, 2017

Is it possible to add an if statement in apple script? So IF Spotify is open, show track. ELSE, leave blank

Right now, every time I quit spotify, it reopens :(

Also a max size for it too. Because some songs take up the whole touchbar length

@fifafu

This comment has been minimized.

Collaborator

fifafu commented Jan 24, 2017

@pallavmac

something like this will check if Spotify is running and also only display max 20 characters (not tested yet):

if application "Spotify" is running then
	tell application "Spotify"
		set currentArtist to artist of current track as string
		set currentTrack to name of current track as string
		
		set completeString to currentArtist & " - " & currentTrack
		set numberOfCharacters to length of completeString
		if numberOfCharacters > 20 then
			return text 1 thru 20 of completeString
		else
			return completeString
		end if
	end tell
	
else
	return ""
end if

@lucameier

This comment has been minimized.

lucameier commented Jan 24, 2017

My Script checks if Spotify is running and if so, shows the track name, artist name and album name.

I configured the widget, that it change its color to red, if the result matches "Spotify not running". Also i configured a predifined action to open Spotify with a click on the widget.

Here is my Code:

tell application "System Events"
	set num to count (every process whose name is "Spotify")
end tell

if num > 0 then
	tell application "Spotify"
		set playState to (player state as text)
		if playState is equal to "playing" then
			set trackName to name of current track
			set artistName to artist of current track
			set albumName to album of current track
			set trackInfo to trackName & " | " & artistName & " | " & albumName
		else
			set trackInfo to "Nothing playing"
		end if
		return trackInfo
	end tell
else
	set trackInfo to "Spotify not running"
	return trackInfo
end if

credits to CaptainAverage on obsproject.com

@mark248am

This comment has been minimized.

mark248am commented Jan 25, 2017

How did you make it appear in red? can you export and share your setup?

@Pogopooc

This comment has been minimized.

Pogopooc commented Jan 25, 2017

Hmm, nice! What's about a Album Cover? 😊

@lucameier

This comment has been minimized.

lucameier commented Jan 26, 2017

@mark248am

How did you make it appear in red?

when the widget is selected, click on Advanced Configuration. Then you can define an alternative color and put the text in the textfield on the right.

bildschirmfoto 2017-01-26 um 18 59 31

can you export and share your setup?

of course 😄 have fun: btt settings2.zip

@mark248am

This comment has been minimized.

mark248am commented Jan 26, 2017

ah, i thought you could change the actual color of the font to red not the box. got confused.

@bhagya7893

This comment has been minimized.

bhagya7893 commented Jan 31, 2017

Is it possible that the widget disappears when Spotify isn't playing?

@Touchy28

This comment has been minimized.

Touchy28 commented Feb 24, 2017

Is this possible for iTunes too?
[Edit]: YES!

Here ist my iTunes Script with an cutter for names, if they too long:
Here is my Code:

tell application "System Events"
	set num to count (every process whose name is "iTunes")
end tell
set maxSize to 20

if num > 0 then
	tell application "iTunes"
		set playState to (player state as text)
		if playState is equal to "playing" then
			set trackName to name of current track
			set artistName to artist of current track
			set albumName to album of current track
			if length of trackName is greater than maxSize then
				set trackName to text 1 thru (maxSize - 3) of trackName & "..."
			end if
			if length of artistName is greater than maxSize then
				set artistName to text 1 thru (maxSize - 3) of artistName & "..."
			end if
			set trackInfo to trackName & " | " & artistName
		else
			set trackInfo to "Break"
		end if
		return trackInfo
	end tell
else
	set trackInfo to "iTunes close"
	return trackInfo
end

Change the size with the maxSize variable

Hope you enjoy it!

@theikid

This comment has been minimized.

theikid commented Mar 13, 2017

Hey guys, this is fantastic work! Is it possible to show the cover art for the song playing in itunes ? Thanks !

@bnsena

This comment has been minimized.

bnsena commented May 16, 2017

Hey, all of this is wonderful! Is it possible to only show this information, say, when a new track plays. Then after like 10 seconds or so it disappears.
Like it takes over the touch bar while showing the song information and then once a specific time has passed it will go away and show what was there originally?

@Tompz

This comment has been minimized.

Tompz commented Sep 23, 2017

Hi all,

Just a quick ask - as I've just used the following with great results:

Is there any way to have the Artist and Track name as scrolling text please?

I really like the idea of being able to limit the size of touchbar info by number of characters displayed, but would love to have the info scrolling.

Code I used on BTT:

tell application "System Events"
	set num to count (every process whose name is "iTunes")
end tell
set maxSize to 20

if num > 0 then
	tell application "iTunes"
		set playState to (player state as text)
		if playState is equal to "playing" then
			set trackName to name of current track
			set artistName to artist of current track
			set albumName to album of current track
			if length of trackName is greater than maxSize then
				set trackName to text 1 thru (maxSize - 3) of trackName & "..."
			end if
			if length of artistName is greater than maxSize then
				set artistName to text 1 thru (maxSize - 3) of artistName & "..."
			end if
			set trackInfo to trackName & " | " & artistName
		else
			set trackInfo to ""
		end if
		return trackInfo
	end tell
else
	set trackInfo to "iTunes close"
	return trackInfo
end if

Thanks in advance!

@fifafu

This comment has been minimized.

Collaborator

fifafu commented Feb 3, 2018

Scrolling text is currently not possible, but I'm looking for solutions to this.

@fifafu fifafu closed this Feb 3, 2018

@samuelthng

This comment has been minimized.

samuelthng commented Apr 30, 2018

Hi people.

Just in case anyone wanted to have an AppleScript Predefined Action for the Spotify Widget.

If Spotify isn't running when you tap on the widget, it will open the application.
If Spotify is already running, it will toggle play/pause then update the track info.
Credits to the guys who posted before. This is really just a Frankenstein of their work.

tell application "System Events"
	set num to count (every process whose name is "Spotify")
end tell

if num is 0 then
	tell application "Spotify"
		activate
	end tell
	set trackInfo to "Spotify"
else if num > 0 then
	tell application "Spotify"
		playpause
	end tell
	delay 0.25
	tell application "Spotify"
		set playState to (player state as text)
		if playState is equal to "playing" then
			set trackName to name of current track
			set artistName to artist of current track
			set albumName to album of current track
			set trackInfo to trackName & " | " & artistName & " | " & albumName
			set numberOfCharacters to length of trackInfo
			if numberOfCharacters > 20 then
				set trackInfo to (text 1 thru 17 of trackInfo) & "..."
			end if
		else
			set trackInfo to "Nothing playing"
		end if
	end tell
end if
return trackInfo

Enjoy!

@stass69

This comment has been minimized.

stass69 commented May 11, 2018

Hi everyone!

just want on long press to make previous track and make spotify icon active like press on it it will open spotify. so how is it realize?

@stass69

This comment has been minimized.

stass69 commented May 11, 2018

can i changed color of font though?

@tmaier-nordisk

This comment has been minimized.

tmaier-nordisk commented May 20, 2018

Can the text (Artist - Track) be running through when it is too long for the area it can be displayed?

@Tompz

This comment has been minimized.

Tompz commented May 27, 2018

@florinme

This comment has been minimized.

florinme commented Jul 13, 2018

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?

@BirkhoffLee

This comment has been minimized.

BirkhoffLee commented Oct 7, 2018

@tmaier-nordisk @fifafu @Tompz Scrolling is possible, all credits to https://imgur.com/gallery/cHwJ5. Use the following code snippet:

set stringToScroll to trackInfo # the text you want to scroll
set speed to 2 # letters per second
set maxLength to 20 # max letters

if length of stringToScroll < maxLength then
	return stringToScroll
end if

set stringToScroll to stringToScroll & " - " # append scroll separator
set len to ((length of stringToScroll) - 1)
use framework "Foundation"
set t to (current application's NSDate's timeIntervalSinceReferenceDate) as real
set p to (t * speed) mod len

return (text (p + 1) thru (p + 1 + maxLength - 1) of (stringToScroll & stringToScroll & stringToScroll)) #& "..." # the & "..." at the end of this is optional
@lock

This comment has been minimized.

lock bot commented Nov 8, 2018

This thread has been automatically locked. The BetterTouchTool issue tracker has moved to https://community.folivora.ai. In case you still experience the issue please create a new post there.

@lock lock bot locked as resolved and limited conversation to collaborators Nov 8, 2018

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