Welcome back, python

It’s been too long since I last coded in python. I’ve been doing a fair bit of AppleScript, and JavaScript over recent months, but tonight I had cause to do some scripting in python. And, how I’ve missed it. I still keep wanting to write if (something) then, but I’m getting over it. I’ve been doing some stuff to organise digital photos, as indicated in the previous post. What I’ve come up with so far is some code that grabs the EXIF data from the file, and if it’s a photo (and larger than 640, to remove thumbnails from the process), move/copy/link it to a particular location. This location is $base_dir/year/month/day/, where $base_dir is a definable constant, and year/month/day is grepped from the EXIF data. I’ve come up with two simple methods of getting the EXIF data - one is to use the EXIF.py module (not installed by default), the other uses jhead. The jhead one is much faster, but I wonder whether I can hand-code one that is even faster, since I only need three bits of data (date, xSize and ySize). [Some time passes…] A grep is marginally faster, but is only easy to do when you know the structure of the data you are looking for. A Date/Time is easy:

1     grep '[12][0-9][0-9][0-9]:[01][0-9]:[0-3][0-9] [0-2][0-9]:[0-5][0-9]:[0-5][0-9]' -m1 -o -a filename.JPG

But getting the same for a resolution (which could just be any value that is a multiple of 8, or even any other number if the image had been edited!), is a bit tricky. Regardless, jhead is much, much faster than EXIF.py, by several orders of magnitude. So I’ll use that. Anyway, the cool bits of my code are:

1     date_string = os.popen('jhead ' + filename + ' | grep Date/Time').read().split(' : ')[1].strip()
2     
3     resolution = os.popen('jhead ' + filename + ' | grep Resolution').read().split(' : ')[1].strip().split(' x ')
4     
5     if resolution[0] < 640 and resolution[1] < 640: 
6         return None # Low-res, must be a thumbnail.
7     
8     date_dir = os.path.join(base_dir, date_string.split(' ')[0].replace(':','/'))
9     

I’m fairly happy with these bits of code, and of course there are more things being added. I do have it working that it grabs the data, and creates links in the chosen directory. I’ve decided to use links until I’ve finished getting all of the album data from iPhoto, since then I don’t have to worry about disk space (hard links don’t take up any room, they are just another reference to the file). Jaq wanted to know if there was a UI for my system - I think there will be. Initially, it will just be using the Finder (and perhaps some Folder Action Scripts to move the files to the right locations), and Spotlight to index the files, but I may move to a proper UI. I think I’ll still use Spotlight as the data store - that way it doesn’t rely on an external database, and if the data is fixed, it updates in my system automatically. Unlike iTunes. This is really heading back to the way it could be done in BeOS. I could have just had this system, and a series of Queries, and it would have all been beer and skittles. The next step will be to look at Albums. At this stage I’m thinking of using hard links for albums, but the thing that worries me about that is the Macintosh’s habit of not rewriting to a file, but saving a new copy, and then overwriting that. Perhaps soft links will be better for Albums. Again, that will rely on the OS doing the maintenance, not my application. Of course, the problem might be with Spotlight not really indexing files that well. I’ve run mdimport, and only one file seems to really be appearing. The others appear as ‘ghost’ files, that AFAIK are aliases. Or so it appears to the system.

Barber’s Adagio For StringsWilliam OrbitOpen Space ★★★★

MacOS Internet Sharing keeps stopping

I’ve borrowed a laptop from work, and a Wireless Access Point device, so now I can do stuff from my bedroom late at night, like I am this. However, just recently I have come across some sort of strange behaviour. Sometimes, but not all of the time, MacOS’s Internet Sharing feature seems to get turned off. Which means I need to get access to the machine and restart it. Not much fun when I’m all toasty warm in bed, and the Mac is in the cold, cold study. So, I’ve done a little bit of research. I might be able to restart it using: $ sudo /usr/libexec/InternetSharing I’ll try that next time it drops out… but I’d rather have a better fix.

Changing MacOS Proxy Settings

We’ve recently gotten a heap of new iMacs (the intel ones), and I’ve been helping the IT guys set them up - including things like cloning the partitions from one machine to the rest. We did come across an issue where the cloning seemed to fail - the way we solved this was to format the drive first. I think it was a permissions issue: because the files were write-protected, Carbon Copy Cloner failed to overwrite the settings. Which meant that they booted up looking the same, although all of the new applications were installed. The other problem we had is that the Proxy Server settings needed to be changed. The new machines were fine - the settings were made before the cloning occurred, but the old machines still had an invalid setting. I tried using AppleScript, but since the only way to do this is with UI scripting, which is notoriously hard to do right, and tends to break when applications are updated, or even just recompiled. Finally, it twigged that I could just copy the preferences file from one machine to the rest. Because the file is protected, you will need Administrator rights, and you may need to move the original file out of the way first, via the Terminal. You could also do this using ssh, which would mean that the other machines wouldn’t even need to be logged in. And the file in question is: /Library/Preferences/SystemConfiguration/network.plist or something similar. I’m not at a Mac to be able to check right now.

More Character Palette Goodness

This is super cool. While attempting to get back to a ‘normal’ listing of characters, I happened to click on By Radical, and up came a list of characters and their meanings! Thus, it’s possible to create your own meanings, without having to use Google! (I last wrote about the Character Palette in Show Selected Character.)

Mail.app Chokes on HTML Email

I’ve noticed that Mail.app really takes ages to render HTML emails, or those with images in them. Seconds elapse between clicking on a message, and it fully rendering in the preview pane.

Dr Karl uses a Mac!

Dr. Karl from Triple J uses a Mac. He talked about using Spotlight to index and search his files on his computer during the last 5 minutes of the February 23rd edition of his Radio segment.

Tiger DVD Burning

Something is either wrong with Tiger’s Finder DVD burning, or Xbox Media Centre’s DVD reading. Any DVDs I burn from a Burn Folder in Tiger will not work properly on the Xbox. The disc name is reported fine by Xbmc, and the first track will play if the disc is inserted (and autoplay is turned on), but no tracks are reported as being there by the browser. Toast-burned DVDs work fine, and all burned DVDs work on the iMac itself, or on a PC. Very frustrating in that I don’t do this often enough to remember, so I keep having to re-burn discs so I can watch them on the Xbox. I can’t wait to finish the house and have my home network all hooked up again. Having to burn a movie back to CD/DVD just to watch it is very frustrating… (All of my DVDs are packed away in storage, and I cannot be bothered opening up boxes to try and find the one I’m looking for).

hostname changes on PPP connection

I think this another thing new to Tiger: when I initiate a PPP connection, my hostname changes, to things like: fliax7-a170.dialup.optusnet.com.au Now, this doesn’t have too many issues, except when I try to initiate a connection with what the hostname used to be, such as a local VNC connection to the other user’s desktop. The solution is to do the following: edit /etc/hostconfig, and replace: HOSTNAME=-AUTOMATIC- with HOSTNAME=the_hostname