There are some amazing Python programmers out there who can do some pretty impressive and abstract stuff. However most of what I use Python for is as a quick and dirty scripting system. I’m rarely writing full elegant programs. Rather I just want to get something done and done quickly. Most of the time I’m not looking for every potential boundary problem. I’ll be able to tell if it doesn’t do what I want!
One thing I sometimes want to do it fix names in iTunes. That’s because I have some files where the name is mangled. (It’s all lower case, it has the track number prepended, or the like) While it’s possible to write a general case solution to this problem it’s much better to just have a script framework and modify a few lines for the particular problem.
Here’s how I do it using Appscript.
import re from appscript import * def fix_itunes_names(): iTunes = app(u'/Applications/iTunes.app') selection = iTunes.windows[1].selection.get() for track in selection: print track.name() newname = re.sub(r"\d+\s*-\s","", track.name()) print newname #track.name.set( newname )
Yes I could get fancier. But why? The above looks for names with a number and dash before the track name. First run through I merely print the new name to ensure no hidden “gotchas.” (Something I think quite important before running a script) When it’s ready I uncomment the final line.
Now instead of using re.sub which does a nice regex substitution I could have it use a title capitalization routine or almost anything else. The point is to make a flexible script template I modify for the problem at hand rather than trying to solve the general case.
Related posts:
.jpg)
Recent Comments