Detect currently playing iTunes, Spotify, and Youtube(Safari)

touch-bar

#1

I was messing around trying to come up with a few things and thought id share this. It only works with Safari, and I don't plan on making updates for it. Although if anyone knows how to make it work for Netflix I'd appreciate the help. Enjoy!

if application "iTunes" is running then
	tell application "iTunes"
		if player state is playing then
			return "iTunes open and Playing || doesnt matter if anything else is open"
		else if application "Spotify" is running then
			tell application "Spotify"
				if player state is playing then
					return "iTunes open not playing || Spotify open and playing || doesnt matter if anything else is open"
				else if application "Safari" is running then
					tell application "Safari"
						try
							repeat with t in tabs of windows
								tell t
									try
										if URL contains "https://www.youtube.com/watch" then
											tell application "Safari" to tell front window to tell ¬
												(first tab whose URL contains "youtube.com/watch?v=") to ¬
												set playPause to do JavaScript [¬
													"document", ¬
													".querySelectorAll('div[class*=\"-mode\"]')[0]", ¬
													".className", ¬
													".match(/(playing|paused|ended)-mode/)[1]"] ¬
													as text
											if playPause contains "play" then
												return "iTunes open not playing || Spotify open not playing || YouTube open and playing"
											else if playPause contains "pause" then
												return "iTunes open not playing || Spotify open not playing || YouTube open and paused"
											else
												return "iTunes open not playing || Spotify open not playing || Youtube not playing"
											end if
										else
											return "iTunes open not playing || Spotify open not playing || Safari open to different page"
										end if
									end try
								end tell
							end repeat
							return "iTunes open not playing || Spotify open not playing || Safari closed to dock"
						end try
					end tell
				else
					return "iTunes open not playing || Spotify open not playing || Safari not open"
				end if
			end tell
		else if application "Safari" is running then
			tell application "Safari"
				try
					repeat with t in tabs of windows
						tell t
							try
								if URL contains "https://www.youtube.com/watch" then
									tell application "Safari" to tell front window to tell ¬
										(first tab whose URL contains "youtube.com/watch?v=") to ¬
										set playPause to do JavaScript [¬
											"document", ¬
											".querySelectorAll('div[class*=\"-mode\"]')[0]", ¬
											".className", ¬
											".match(/(playing|paused|ended)-mode/)[1]"] ¬
											as text
									if playPause contains "play" then
										return "iTunes open not playing || Spotify not open || YouTube open and playing"
									else if playPause contains "pause" then
										return "iTunes open not playing || Spotify not open || YouTube open and paused"
									else
										return "iTunes open not playing || Spotify not open || Youtube not playing"
									end if
								else
									return "iTunes open not playing || Stopify not open || Safari open to different page"
								end if
							end try
						end tell
					end repeat
					return "iTunes open not playing || Spotify not open || Safari closed to dock"
				end try
			end tell
		else
			return "iTunes open not Playing"
		end if
	end tell
else if application "Spotify" is running then
	tell application "Spotify"
		if player state is playing then
			return "iTunes not open || Spotify open and Playing || doesnt matter if anything else is open"
		else if application "Safari" is running then
			tell application "Safari"
				try
					repeat with t in tabs of windows
						tell t
							try
								if URL contains "https://www.youtube.com/watch" then
									tell application "Safari" to tell front window to tell ¬
										(first tab whose URL contains "youtube.com/watch?v=") to ¬
										set playPause to do JavaScript [¬
											"document", ¬
											".querySelectorAll('div[class*=\"-mode\"]')[0]", ¬
											".className", ¬
											".match(/(playing|paused|ended)-mode/)[1]"] ¬
											as text
									if playPause contains "play" then
										return "iTunes not open || Spotify open not playing || YouTube open and playing"
									else if playPause contains "pause" then
										return "iTunes not open || Spotify open not playing || YouTube open and paused"
									else
										return "iTunes not open || Spotify open not playing || Youtube not playing"
									end if
								else
									return "iTunes not open || Spotify open not playing || Safari open to different page"
								end if
							end try
						end tell
					end repeat
					return "iTunes not open || Spotify open not playing || Safari closed to dock"
				end try
			end tell
		else
			return "iTunes not open || Spotify open not Playing || Safari not open"
		end if
	end tell
else if application "Safari" is running then
	tell application "Safari"
		try
			repeat with t in tabs of windows
				tell t
					try
						if URL contains "https://www.youtube.com/watch" then
							tell application "Safari" to tell front window to tell ¬
								(first tab whose URL contains "youtube.com/watch?v=") to ¬
								set playPause to do JavaScript [¬
									"document", ¬
									".querySelectorAll('div[class*=\"-mode\"]')[0]", ¬
									".className", ¬
									".match(/(playing|paused|ended)-mode/)[1]"] ¬
									as text
							if playPause contains "play" then
								return "iTunes not open || Spotify not open || YouTube open and playing"
							else if playPause contains "pause" then
								return "iTunes not open || Spotify not open || YouTube open and paused"
							else
								return "iTunes not open || Spotify not open || Youtube not playing"
							end if
						else
							return "Safari open to different page"
						end if
					end try
				end tell
			end repeat
			return "Safari closed to dock"
		end try
	end tell
else
	return "Nothing open or playing"
end if



#2

This looks pretty neat! Is there any way you can paste it in AppleScript format? For some reason it won't automatically correct the format how it is pasted here.


#3

@Lucas_McDonald use three ` at the beginning and at the end of the script to wrap code

e.g.
` ``AppleScript
tell application "test"
end tell
```

(without the space I inserted at the beginning)


#5

Sorry about that. I have updated the post in AppleScript format.