Raphael Schweikert Raphael Schweikert - 11 months ago 100
AppleScript Question

How do I get actual class of JXA object

I’m trying to differentiate between iTunes playlists that are folders and ones that aren’t.

In AppleScript there seem to be two ways to do this:

  1. I can get all folder playlists and process them individually from user playlists:

    set userPlaylists to user playlists
    set folderPlaylists to folder playlists
    -- …do processing

  2. I can check the class of a playlist using
    class of pl is folder playlist
    is my playlist).

Both methods don’t seem to work with JXA.

The first method does not work because accessing
results in
Error: Message not understood
app = Application('iTunes')
). Every other type of playlist can be accessed this way (
all work). Is this a bug?

So I tried replicating the second method. As far as I understood,
should give me the object type as a string. However, it only returns the type that was requested by the object specifier that gave me the object.

So while in AppleScript
class of (first item of (playlists whose name is "«Name of a folder playlist»"))
gives me
folder playlist
ObjectSpecifier.classOf(app.playlists.whose({name: '«Name of a folder playlist»'})()[0])
just returns
(presumably because I used
to get to the object).

Isn’t there a function that would return the actual type of the playlist (e.g.
"folder playlist"
, or
"user playlist"

Answer Source

To get folders from the playlists:

folderList = itunesApp.playlists.whose({_match: [ObjectSpecifier().class, 'folderPlaylist']})()

To differentiate between userPlaylist and folderPlaylist from a playlist object:

  • use: myobj.class() == 'folderPlaylist'
myList = itunesApp.playlists()
for (var i in myList) {
    if (myList[i].class() == 'folderPlaylist') { 

    } else if (myList[i].class() == 'userPlaylist') {