Search code examples
javascriptapplescriptitunesjavascript-automation

Is there a better way to add a new playlist to iTunes through Javascript for Automation (JXA)?


There must be a better way to create a new element in a collection (if that's what really happens behind the abstraction).

I'm looking for the JXA equivalent of this applescript

if (count of (playlists whose name is plName)) is 0 then
  make new playlist with properties { name: plName }
end if

I have tried a number of things, but can't get an equivalent. The closest I can get is to duplicate an existing playlist and then rename it...

if ( iTunes.userPlaylists.whose( { name: {_equals: plName } } ).length == 0 ) {
    mnp = iTunes.userPlaylists[0].duplicate()
    mnp.name.set( plName )
}

Any hints on the cleanest way to test for a playlist's existence, or to select a playlist to duplicate are welcome. Like most people, due to the appalling lack of documentation for JXA, I'm figuring most of the application interaction stuff out by experimentation.

My main question though is whether there is a way to create an empty playlist without having to duplicate one. Not that it matters I guess if I have a work around, but it just feels like a kludge.


Solution

  • What about the simplest way:

    mnp = iTunes.UserPlaylist().make()
    mnp.name = plName
    

    Have fun, Michael / Hamburg