MacMost Q&A Forum • View All Forum QuestionsAsk a Question

How Do I Filter Music Tracks By MediaKind Using JavaScript?

Using JavaScript I’m trying to filter music tracks by mediaKind. Using AppleScript: “set musicVideos to every track whose media kind is music video” in a tell statement returns a list of the music videos. Using what I believe is equivalent JavaScript code: “var musicVideos = Application (“Music”).tracks.whose({“mediaKind”: {_equals: “music video”}});” returns an empty array. Using the choice of “song” as media kind (JavaScript mediaKind) also produces a populated list using AppleScript and an empty array using JavaScript. I don’t seem to have any difficulty filtering music tracks by other track properties using JavaScript.

Using JavaScript I’m trying to filter music tracks by mediaKind. Using AppleScript: “set musicVideos to every track whose media kind is music video” in a tell statement returns a list of the music videos. Using what I believe is equivalent JavaScript code: “var musicVideos = Application (“Music”).tracks.whose({“mediaKind”: {_equals: “music video”}});” returns an empty array. Using the choice of “song” as media kind (JavaScript mediaKind) also produces a populated list using AppleScript and an empty array using JavaScript. I don’t seem to have any difficulty filtering music tracks by other track properties using JavaScript.

Device: Mac Running Catalina

App: Music
—–
Dana Stevens

Comments: 2 Responses to “How Do I Filter Music Tracks By MediaKind Using JavaScript?”

    4 years ago

    Not sure why whose doesn't work for you there, but you can just bypass it easily enough.

    var allTracks = Application('Music').tracks;
    var allTrackKinds = allTracks.mediaKind();
    var musicVideos = [];
    for(var i=0;i<allTrackKinds.length;i++) {
    	if (allTrackKinds[i] == 'music video') {
    		musicVideos.push(allTracks[i].name());
    	}
    }

    This will give you an array with the names of all music videos. Remove the .name() and you get track references instead, and you can loop through them and do things with them.

    Dana Stevens
    4 years ago

    Perfect. Thanks Gary!

Comments Closed.