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

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


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.

@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)

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

I'm no coder, but I'm wondering why the youtube one won't work for other browsers, like chrome?

This script is calling for the application safari. It does not support chrome

Yea, but I tried changing the code (i understand very little javascript), to support chrome. I can get it to click stuff on youtube, but can't seem to get the duration and stuff