I just figured out how to pass text from wherever to the search field in Music, with help from daemon's answer, which no longer works. This should work for what you want to do in conjunction with what you have.
Replace your "open location" line with a variable name for your concatenated string. Add this code below yours and pass that variable in place of 'input' (in my case 'input' is text from any application, which I use to select text of an artist name in an email/webpage/message that I want to send to Music's search).
First it checks to see if the main Music window is open vs the MiniPlayer, and open it if not to enable search via cmd-O, the cmd-F to find, then passes the input and hits return:
tell application "Music" activate end tell tell application "System Events" if not (exists (window "Music" of process "Music")) then tell process "Music" keystroke "0" using command down end tell end if tell process "Music" keystroke "f" using command down keystroke input key code 36 end tell end tell
So, something like this (I don't have Spotify to check that section, but this should work assuming your code there is correct):
tell application "Spotify" if player state is not stopped then set currentArtist to artist of current track as string set currentTrack to name of current track as string set spotTrack to currentArtist & " " & currentTrack end if end tell tell application "Music" activate end tell tell application "System Events" if not (exists (window "Music" of process "Music")) then tell process "Music" keystroke "0" using command down end tell end if tell process "Music" keystroke "f" using command down keystroke spotTrack key code 36 end tell end tell
The only thing I couldn't figure out is how to check if the search field is already in focus, because if it is, the cmd-F causes a system alert sound. Generally not an issue as typically you'll search and interact with something else before running this script again, so calling it good. :)