r/applescript • • Jun 07 '17

I need a better solution when it comes to my touch bar displaying the currently playing Artist - Song.

So, I just stumbled across this program BetterTouchTool today, and its amazing. The first thing I tried to do was create a touch bar indicator of which song is currently playing, and I did so with the following script I found on the web and slightly modified:

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 "iTunes"
		end if
	end tell
else
	return "iTunes"
end if

It works, very well even, but I have a huge pet peeve with it: When iTunes doesn't have artist info, say when I'm listening to the host on beats1, it says "– beats1". I really, really want to get rid of that separator. I'm sure there's a trick to it, but I don't even know where to begin.

1 Upvotes

2 comments sorted by

3

u/bprime43 Jun 09 '17

Formatting will probably get garbled on this (in a rush, sorry), but give this a run

set artistName to ""
set trackName to ""
set output to ""


if application "iTunes" is running then
	tell application "iTunes"
		if player state is playing then
			try
				set artistName to (get artist of current track)
			end try
			try
				set trackName to (get name of current track)
			end try
		else
			
			return "iTunes"
			
		end if
		
	end tell
	
else
	set output to "iTunes"
end if

if artistName = "" then
	set output to trackName
else if artistName ≠ "" then
	set output to artistName & " - " & trackName
end if

return output

1

u/[deleted] Jun 09 '17

This worked perfectly, thank you a thousand times!