Skip to content

Instantly share code, notes, and snippets.

@joshuaswilcox
Last active July 29, 2024 00:14
Show Gist options
  • Save joshuaswilcox/7251527 to your computer and use it in GitHub Desktop.
Save joshuaswilcox/7251527 to your computer and use it in GitHub Desktop.
Applescript to show current playing song and determine if its spottily or iTunes that is playing
if application "Spotify" is running and application "iTunes" is not running then
tell application "Spotify"
if player state is stopped then
set display to "No Track Playing"
else
set track_artist to artist of current track
set track_name to name of current track
set track_duration to duration of current track
set seconds_played to player position
set state to ""
if player state is paused then
set state to "(Paused) "
end if
set display to state & track_artist & " - " & track_name & " - " & (round ((seconds_played / track_duration) * 100)) & "%"
end if
end tell
else if application "Spotify" is running and application "iTunes" is running then
--Get current states of iTunes and Spotify
tell application "iTunes" to set itunesState to (player state as text)
tell application "Spotify" to set spotifyState to (player state as text)
if (itunesState is "paused" or itunesState is "stopped") and spotifyState is "playing" then
tell application "Spotify"
if player state is stopped then
set display to "No Track Playing"
else
set track_artist to artist of current track
set track_name to name of current track
set track_duration to duration of current track
set seconds_played to player position
set state to ""
if player state is paused then
set state to "(Paused) "
end if
set display to state & track_artist & " - " & track_name & " - " & (round ((seconds_played / track_duration) * 100)) & "%"
end if
end tell
else if itunesState is "playing" and (spotifyState is "paused" or spotifyState is "stopped") then
tell application "iTunes"
if player state is stopped then
set display to "No Track Playing"
else
set track_artist to artist of current track
set track_name to name of current track
set track_duration to duration of current track
set seconds_played to player position
set state to ""
if player state is paused then
set state to "(Paused) "
end if
set display to state & track_artist & " - " & track_name & " - " & (round ((seconds_played / track_duration) * 100)) & "%"
end if
end tell
else if (itunesState is "paused" or itunesState is "stopped") and spotifyState is "paused" then
set display to "No Track Playing"
else
set display to "Crazyman!!!!"
end if
else if application "iTunes" is running and application "Spotify" is not running then
tell application "iTunes"
if player state is stopped then
set display to "No Track Playing"
else
set track_artist to artist of current track
set track_name to name of current track
set track_duration to duration of current track
set seconds_played to player position
set state to ""
if player state is paused then
set state to "(Paused) "
end if
set display to state & track_artist & " - " & track_name & " - " & (round ((seconds_played / track_duration) * 100)) & "%"
end if
end tell
else
set display to "No music app is running"
end if
@typkrft
Copy link

typkrft commented Jul 3, 2020

Awesome Snippet! What display is this manipulating? Can this be modified to continuously export the current song playing to a txt file?

Saw this comment randomly and was board. Thought I would take a stab. Could definitely be improved upon. Better late than never.

on getMusicInfo()
	set theName to ""
	set theState to ""
	
	try
		with timeout of 2 seconds
			tell application "Music"
				if it is running then
					set theName to name of current track as text
					set theState to player state as text
				end if
			end tell
		end timeout
	end try
	
	set theList to {theName, theState}
	return theList
end getMusicInfo

on writeNameToFile(theName)
	do shell script "echo " & quoted form of theName & " >> ~/songNames.txt && sed -i '' '/^$/d' ~/songNames.txt"
	log "Wrote " & theName & " to ~/songNames.txt"
end writeNameToFile

on main()
	set theState to item 2 of getMusicInfo()
	set currentTrack to item 1 of getMusicInfo()
	set newTrack to ""
	
	if theState is "playing" then
		writeNameToFile(currentTrack)
	end if
	
	repeat while true
		
		set theState to item 2 of getMusicInfo()
		log {currentTrack, newTrack}
		
		if theState is not "playing" then
			delay 2
		else if theState is "playing" and currentTrack is not newTrack then
			writeNameToFile(newTrack)
			set currentTrack to item 1 of getMusicInfo()
			set newTrack to currentTrack
			delay 2
		else if theState is "playing" and currentTrack is newTrack then
			delay 2
			set newTrack to item 1 of getMusicInfo()
		end if
		
	end repeat
end main

main()

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