[Pythonmac-SIG] How to fathom appscript

Nicholas Riley njriley at uiuc.edu
Tue Dec 19 01:20:54 CET 2006


On Mon, Dec 18, 2006 at 11:11:27PM +0000, Hamish Allan wrote:
> itunes = app('iTunes')
> x = itunes.sources.first.playlists[its.name.contains('MyName')]
> 
> This code fetches any playlists containing the string 'MyName'. But I
> want an exact match. Using 'equals' rather than 'contains' doesn't
> work.

You just use '==' instead.

In [4]: itunes.sources.first.playlists[its.name == 'Stations']()
Out[4]: [app(u'/Applications/iTunes.app').sources.ID(41).user_playlists.ID(1936)]

But in this case, you don't need to.

In [6]: itunes.sources.first.playlists['Stations']()
Out[6]: app(u'/Applications/iTunes.app').sources.ID(41).user_playlists.ID(1936)

Most objects support multiple reference forms, as you'll see if you
look at their documentation.

> How do I find out which operations its.name supports? And more
> generally, what is possible in other similar situations with different
> objects?

Use .help().  So, for example, you can see that the playlists can be
referenced by index, name or ID:

In [7]: itunes.sources.first.playlists.help()
==============================================================================
Appscript Help (-t)

Reference: app(u'/Applications/iTunes.app').sources.first.playlists

------------------------------------------------------------------------------
Description of reference

Element: playlists -- by index, name, id
[...]

Note the reference forms above.

It also helps to view the scripting dictionary, either using
appscript's tools for doing so in a Web browser, or just with Script
Editor (as I normally do).

> I also want to do what the following code suggests:
> 
> x = itunes.sources.first.playlist_folders[its.name.equals('MyFolder')]

You want 'folder_playlists' not 'playlist_folders'.  So again it's
pretty simple:

In [15]: itunes.sources.first.folder_playlists['Statistics']()
Out[15]: app(u'/Applications/iTunes.app').sources.ID(41).folder_playlists.ID(122340)

> y = itunes.sources.first.playlists[its.parent.equals(x)]

Unfortunately here you run into a problem, as Apple didn't fully
implement terminology for 'folder playlists', but you can view the
scripting dictionary to figure it out.  'parent', while a property of
playlists, isn't always set.  So the only thing I could figure out was
to iterate through them in Python:

In [45]: stats_id = itunes.folder_playlists['Statistics'].id()
In [46]: [p for p in itunes.user_playlists() if p.parent.exists()
   ....:  and p.parent.id() == stats_id]
Out[46]: 
[app(u'/Applications/iTunes.app').sources.ID(41).user_playlists.ID(1032),
 app(u'/Applications/iTunes.app').sources.ID(41).user_playlists.ID(1158),
 app(u'/Applications/iTunes.app').sources.ID(41).user_playlists.ID(1060)]

-- 
Nicholas Riley <njriley at uiuc.edu> | <http://www.uiuc.edu/ph/www/njriley>


More information about the Pythonmac-SIG mailing list