[Pythonmac-SIG] "Trashing" a file

Niko Matsakis niko at alum.mit.edu
Sat May 7 00:31:17 CEST 2005


Dear Mailing List,

I am working on a python program that needs to trash some files.   
Ideally, I would like it to move them to the Trash, but I'm not quite  
sure what the best way to do this is.

For context, I am using appscript to talk to iTunes and load its list  
of songs.  This works great.  Among other things, the program can then  
find albums that have multiple copies of the same track and purge them.

iTunes gives me back an FSAlias object as the location of the track,  
which appears to come from the much maligned Carbon standard module.

I have tried deleting the file by doing:

	appscript.app ('Finder').delete (aliasobject)

This actually does work --- the Finder makes a little trashing noise,  
and the file ends up in the trash --- but it also throws an exception,  
which makes me mildly uncomfortable.  Here is the actual output:

> ;pythonw test.py
> Traceback (most recent call last):
>   File "test.py", line 11, in ?
>     app ('finder').delete (tr.location.get())
>   File  
> "/System/Library/Frameworks/Python.framework/Versions/2.3/lib/ 
> python2.3/site-packages/appscript/specifier.py", line 168, in __call__
>     raise CommandError("Can't do command %r(%s)" % (self, args and  
> kargs and args + ', ' + kargs or args or kargs), err, trace)
> appscript.specifier.CommandError: Can't do command  
> app(u'/System/Library/CoreServices/ 
> Finder.app').delete([<Carbon.File.Alias object at 0x5f100>,  
> <Carbon.File.Alias object at 0x5f110>]), Error 0: The operation could  
> not be completed.
> ;

I mean, I could just catch and ignore this exception, but that seems  
bad.  Any suggestions on what the Right Thing To Do is?

Incidentally, are there searchable archives for this list?  I couldn't  
seem to find any, just the month-by-month browsable ones...


thanks,

Niko



More information about the Pythonmac-SIG mailing list