[Pythonmac-SIG] ScriptingBridge vs. AppScript

has hengist.podd at virgin.net
Fri Oct 10 19:46:05 CEST 2008


Jon Smith wrote:

> I've been using Appscript and have been incredibly happy with it.  
> However
> I'm having a ton of problems getting the list of tracks from my  
> library. I
> was assuming the problem was with my library/iTunes itself however I  
> can get
> the track list with ScriptingBridge. Am I doing something wrong? Why  
> would
> ScriptingBridge work and AppScript Timeout? Any ideas.
>
> #!/usr/bin/env python
> from ScriptingBridge import *
> from appscript import *
>
> def main(argv=None):
>     itunes =
> SBApplication.applicationWithBundleIdentifier_("com.apple.iTunes")
>     lib = itunes.sources()[0].playlists()[0]
>     tracks = lib.tracks()
>     print len(tracks)
>     tracks2 =
> app(u'/Applications/ 
> iTunes.app').library_playlists['Library'].file_tracks.get()
>     print len(tracks2)

Your SB and appscript examples are actually doing very different  
things [1]. The SB code is sending a 'count' event to iTunes and  
getting back an integer indicating the number of tracks in the  
playlist. The appscript code is sending a 'get' event and getting back  
a list of references to the tracks in that playlist.

Here's some direct comparisons of counting vs. getting (I've  
normalised everything else for clarity):

- counting tracks in SB and appscript:

	itunes =  
SBApplication.applicationWithBundleIdentifier_("com.apple.iTunes")
	lib = itunes.sources()[0].playlists()[0]
	tracks_ref = lib.tracks()
	print len(tracks_ref)

	itunes = app(id='com.apple.iTunes')
	lib = itunes.sources[1].playlists[1]
	tracks_ref = lib.tracks
	print tracks_ref.count(each=k.item)

- getting tracks in SB and appscript:

	itunes =  
SBApplication.applicationWithBundleIdentifier_("com.apple.iTunes")
	lib = itunes.sources()[0].playlists()[0]
	tracks_list = lib.tracks().get()
	print tracks_list

	itunes = app(id='com.apple.iTunes')
	lib = itunes.sources[1].playlists[1]
	tracks_list = lib.tracks.get()
	print tracks_list


Anyway, I'm guessing you've a very large playlist and by the time  
iTunes has constructed a list of references to every track in it your  
'get' event has timed out. To increase the timeout duration in  
appscript, include a 'timeout' argument in the 'get' command, e.g.:

	itunes = app(id='com.apple.iTunes')
	lib = itunes.sources[1].playlists[1]
	tracks_list = lib.tracks.get(timeout=600) # 10 minute timeout
	print tracks_list

HTH

has

[1] It's an easy mistake though to make as SB obfuscates its internal  
workings even more than AppleScript. Despite the misleading name,  
SBElementArray is *not* a real array at all, and creating one does not  
automatically send a 'get' event to the target application to obtain a  
list of references (you have to invoke its -get method for that).

-- 
Control AppleScriptable applications from Python, Ruby and ObjC:
http://appscript.sourceforge.net



More information about the Pythonmac-SIG mailing list