caleb531 / play-song

An Alfred workflow for quickly and easily playing music in the Apple Music app
MIT License
109 stars 9 forks source link

Feature Request: Universal Search of Artist, Album, and Song Title. #60

Closed jetblackrx89 closed 3 years ago

jetblackrx89 commented 3 years ago

I used to love an app called Coversutra and your script is by far to closest to how it used to work.

One thing that I loved was the simplicity of invoking a hotkey and having my search text return all matches for artists, albums, and song titles, all co-mingled in one giant result. It would show the artist, album (with cover art), and song title in the results. And furthermore, the results would be "bundled" in a pretty logical way. Any albums under the same artist would appear successively in the results. Any songs under the same album would appear successively in the results.

Currently with your script, I've mapped 3 different hotkey combinations to launch "playsongby, "playsongin", and "playsong", but I would like one single hotkey to launch a search in all 3.

Do you think this would be possible? Thanks!

caleb531 commented 3 years ago

@jetblackrx89 That's a neat idea. However, it would be challenging to implement because the AppleScript API for querying the Music app only returns results as individual songs, not albums/artists as singular units. This creates a few problems:

  1. If only individual songs are returned in the results, how do I know whether to display a result as a song, artist, or album?
  2. I could run the three searches (for songs, artists, and albums) individually, then combine the result sets. But how should I combine them? And there would be a performance penalty which is the sum of time it takes to perform each search separately
  3. Alfred can only show a maximum of 9 results on-screen at the same time, so if there's all this hierarchy going on of songs-grouped-under-albums-grouped-under-artists, then it would be a pretty long list of results, I would think.
jetblackrx89 commented 3 years ago

First, thanks a lot for considering this.

Here's what I was thinking. I have no idea about the computational cost to do this: Untitled-1-2

jetblackrx89 commented 3 years ago

Also in terms of how to avoid a performance / feasibility from a scripting perspective:

I notice in your script it already has some hard cutoffs. For example, if I search by song and just type "a", I get only the first 15 results in my library even though there's probably hundreds of matching songs. I don't know if that's a limitation of Afred, the API calls to/from Apple Music, or the handler scripts you wrote. Depending on where the limitation lies, then yeah, it could be hard to implement a "universal" search where the results list will only be larger. You can't exactly sort through a data set if the data set that's being returned is incomplete in the first place.

So my idea might only work if Apple Music itself had an API call to return a pre-sorted list in the manner I was describing. and then you could impost the 15 result limit like you're currently doing to truncate that result.¯_(ツ)_/¯

caleb531 commented 3 years ago

@jetblackrx89 The 15-result cutoff was added as a performance optimization, actually. If you try increasing that number yourself (to something like 30 or 50), you'll notice that my workflow takes more time from the point you finish typing your query to the point Alfred starts displaying results.

From my past testing, I believe the bottleneck here isn't the Music library call itself, but rather the number of results AppleScript must subsequently process in returning the result set to you. I am unsure whether the precise inefficiency lies in the pure AppleScript-native iteration, or in my need to convert each result to a JSON string.

I don't know how much AppleScript experience you have, but if you want to hack away at the code to explore possible optimizations, you are certainly welcome to!

caleb531 commented 3 years ago

@jetblackrx89 Any other thoughts of my latest feedback?

jetblackrx89 commented 3 years ago

I think you can close this out as I can't think of an implementation. Thanks again for even just considering this feedback.

caleb531 commented 3 years ago

@jetblackrx89 Sounds good, and thank you for providing your feedback!