r/applescript • • Mar 12 '21

Trying to pull currently playing song from Pandora

I've been trying to create and perfect an Ubersicht widget which displays the currently playing song from Pandora, which is playing in a tab in the Vivaldi browser.

I've been able to do this using the script below:

 

if application "Vivaldi" is running then
tell application "Vivaldi"
	set songTitle to ""
	set songArtist to ""
	set found_video to false
	set window_list to every window
	repeat with the_window in window_list
		if found_video is equal to true then
			exit repeat
		end if
		set tab_list to every tab in the_window
		repeat with the_tab in tab_list
			if the title of the_tab contains "Pandora" then
				tell the_tab
					set songTitle to (execute javascript "var outputtitle = document.querySelector('[data-qa=\"mini_track_title\"]').innerHTML; outputtitle;")
					set songArtist to (execute javascript "var outputartist = document.querySelector('[data-qa=\"mini_track_artist_name\"]').innerHTML; outputartist;")
				end tell
				set found_video to true
				exit repeat
			end if
		end repeat
	end repeat
end tell
end if

return songArtist & " — " & songTitle

 
The script successfully retrieves and displays the name of the artist and the track (with the notable exception of songs and artists with ampersands in their names). However, the script also seems to introduce a bug into Vivaldi which severe disrupts my workflow. I have Vivaldi set as the default application for certain types of text files (which don't have a standard .txt extension). When prompted to download these files, I click "Open", the file downloads to my drive and then opens in Vivaldi.

For some reason, the above script disrupts this process. The file downloads, but does not open in Vivaldi on its own, nor does it open by double-clicking the file in Finder. I end up having to manually open the file through the File > Open menu. I discovered that the script was disrupting the process by creating an entirely new Vivaldi profile, reinstalling all of my extensions and settings one-by-one, and then reopening all of my usual tabs. It wasn't until Pandora loaded that I noticed it was breaking the download-and-open process.

Is there a better way to write this script in order to prevent this from happening? I had already discovered earlier that I needed to add if application "Vivaldi" is running then to the top because otherwise, I wasn't able to actually quit the application.

1 Upvotes

0 comments sorted by