Thanks; I'll give this a try. Converting an Alias to a path isn't too difficult, there's an example in one of the sample appscripts I believe.
Niko On May 7, 2005, at 1:00 AM, Bob Ippolito wrote: > On May 6, 2005, at 6:31 PM, Niko Matsakis wrote: > >> 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? > > I don't like dealing with apple events very much because they tend to > be slow and/or unreliable.. but this is the Cocoa way to do it > (untested, but should probably work): > > from AppKit import * > import sys > import os > > def groupFiles(files): > dirs = {} > enc = sys.getfilesystemencoding() > for fn in files: > if not isinstance(fn, unicode): > fn = unicode(fn, enc) > fn = os.path.realpath(fn) > dirname, basename = os.path.splitext(fn) > try: > dirs[dirname].append(basename) > except KeyError: > dirs[dirname] = [basename] > return dirs > > def recycleFiles(files, ws=None): > if ws is None: > ws = NSWorkspace.sharedWorkspace() > results = [] > for dirname, files in groupFiles(files).iteritems(): > res, tag = > ws.performFileOperation_source_destination_files_tag_( > NSWorkspaceRecycleOperation, > dirname, > u'', > files) > results.append((res, tag, dirname, files)) > return results > > You would need to pass a sequence of paths to recycleFiles .. so you'd > need to turn those FSAlias objects into POSIX paths (I don't remember > how to do it off the top of my head). > >> Incidentally, are there searchable archives for this list? I couldn't >> seem to find any, just the month-by-month browsable ones... > > I don't know.. but there's always Google. A query that ends with > "pythonmac-sig site:mail.python.org" sans quotes is probably only > going to turn up results from this list. > > -bob > _______________________________________________ Pythonmac-SIG maillist - Pythonmac-SIG@python.org http://mail.python.org/mailman/listinfo/pythonmac-sig