SKaplanOfficial/PyXA

Request: Filter by whether an attribute exists

Closed this issue · 2 comments

In AppleScript, there is an exists method to check if an attribute is defined on an object. A common use case for this is determining if a parent object exists in an object hierarchy (i.e. "if parent exists"), and it is usually more performant because the full object reference does not need to be resolved to evaluate it.

Would it be possible to add "exists" support to filters?

My particular use case is to get all "top-level" Music folder playlists, filtering out any folders that are sub-folders. I can retrieve this in AppScript as follows, but would prefer to do this natively in PyXA if possible.
[f for f in appscript.app('/System/Applications/Music.app').folder_playlists() if not f.parent.exists()]

Added to the upcoming release. Both the filter approach and list methods are support, i.e.:

import PyXA
app = PyXA.Music()

top_level_playlists_1 = app.playlists().filter("parent", "not exists")
top_level_playlists_2 = app.playlists().not_exists("parent")

sub_playlists_1 = app.playlists().filter("parent", "exists")
sub_playlists_2 = app.playlists().exists("parent")

I've aldo added an exists() method on all XAObjects, so you can do the following as well:

import PyXA
app = PyXA.Music()
top_level_folders = [x for x in app.playlists() if not x.parent.exists() and x.special_kind == app.PlaylistKind.FOLDER]

Appscript has a folder_playlists() method, but I'm not sure how exactly that is implemented, since "folder playlists" aren't a list contained by anything in Music's scripting dictionary.

This is awesome - thank you!