Search code examples
bashapplescript

Using AppleScript, how can I get track details when searching for tracks in my Music library


I have a shell script (BASH) which uses the osascript command to search the Music app for songs (seems to only work with tracks in my library, not those from Apple Music streaming, but that's for another question).

osascript -e "tell application \"Music\" to search playlist \"Music\" for \"${2}\""

gives me a comma-delimited string of results of the form

shared track id 63569 of user playlist id 60784 of source id 66

How can I then query Music using AppleScript through BASH for details for this result?


Solution

  • I found that this process seemed to work.

            search_results=`osascript -e "tell application \"Music\" to search playlist \"Library\" for \"${2}\""`;
            IFS=',' read -r -a results <<< "$search_results"
            for result in "${results[@]}"
            do
                track_id=`echo ${result} | cut -d ' ' -f 4`
                track_details=`osascript -e "tell application \"Music\" to get {name, artist, album} of the track id \"${track_id}\""`;
                echo "${track_details}"
            done