1. Apple released iTunes version 8 this week , which introduced some excellent new features , such as Genius playlists, but broke the fancy perl script that I wrote to rotate my music library on my iPod touch.

    While revisiting this, I took the opportunity to re-implement it, aiming to fix a few of it's faults, most specifically the terrible performance. I decided to use Python this time around, chiefly because of the existence of appscript , an apple event bridge with a nice syntax. Python's object and sequence semantics are a slightly better fit with AppleScript's data models, and appscript should be a more optimal solution than Mac::Glue for sending lots of messages iteratively.

    I've also improved the actual command recipe, using 'duplicate' rather than 'add' to build the playlist seems more efficient. Also the overhead of having to periodically build glue modules with the ' gluemac ' tool is removed. Sadly appscript isn't shipped with OS X, but installing it ( at least on Leopard ), is as simple as ' sudo easy_install appscript '.

    The concept behind the tool is the same : use a nominated playlist to synchronise the albums with the iPod, and pick a random set of albums from buckets organised by album rating. Currently it's set to shuffle in 10 '2 star' albums, 20 'three star' albums, and 30 'four star' albums, selected from a 'just music' smart playlist that filters the master library, removing all spoken word, and podcasts and other miscellany from the pool.

    Here's the source . I'm far less experienced at python than I am perl, so I wouldn't claim it was a particularly idiomatic solution. It does run many times more quickly than the perl / Mac::Glue solution, taking a minute or so, rather than the best part of an hour. I would put all the performance gains down to the AppleEvents bridge , appscript interface, and using more efficient apple event set operations, rather than iterating over individual data.





    posted by cms on
    tagged as
  2. Sometimes you run programs in xterm windows that try and do you a favour, by setting the xterm title property. Potentially useful enough, but aggravatingly some of them don't restore the previous title when they exit. If you're using some scheme of your own to set meaningful window titles, this is annoying.

    Here's a shell one liner that you can use to grab the current title in an xterm. You could use this to write a wrapper script that gracefully launches any such rude application, and restores the rightful title property when it's done

    /usr/X11R6/bin/xprop -id $WINDOWID | perl -nle 'print $1 if /^WM_NAME.+= \"(.*)\"$/'

    posted by cms on
    tagged as
  3. Another kind of iteration you often want to do when constructing programs, is to count things. Quartz composer provides the counter patch, which increments a running total when one of it's inputs switches from false to true. Similarly, it decrements the total whenever the signal to it's other input changes from false to true.

    By generating a regular true/false alternating value, and connecting this up to the increment line, you could generate a regular count. This composition demonstrates one way to do this. Using the Patch Time patch, a count of time in seconds is passed through a modulo 2 operator to generate a regular sequence of alternate 1s and 0s. This is connected up to the increment line of the counter, which then counts upward in integers.
    quartz composer counter generating stripe width

    The counter value is used to govern the stripe width of a vertical stripe pattern. As the patch runs, the stripe width increases every other second. This is a very simple display, but the bit generator and accumulator demonstrated are useful in a variety of ways. You can download a copy of this patch here .

    posted by cms on
    tagged as
  4. Quartz composer is a visual programming tool from Apple that ships as part of the Developer tools with Mac OS X 10.4 or later. It presents a visual object-oriented programming metaphor around Quartz and Core Graphics that allows you to simply compose graphical effects by connecting inputs and outputs of different objects together, graphically.

    You can use QC to build pipelines that respond to a variety of inputs, local or via peripheral interfaces to construct visualisers for a variety of source signals, such as MIDI, audio from the built in mic, video signals from an iSight camera, or even networked events from computers on your internet or LAN. It also can be used to procedurally generate graphics, which you can use to build fancy displays or screen savers. Some of the system screen savers that ship with OS X, like the 'word of the day' or the 'rss visualiser', are actually simple Quartz Composer scripts.

    It's an impressive tool, and ships with documentation and some examples of what you can do. You can achieve nice effects quite quickly, but there is still a learning curve to climb. As an example, a common thing you might want to do when constructing simple animating displays, is loop over a set of possible outcomes. Iterators are a common piece of the vocabulary of programming languages, but it took me a little while to figure out how to achieve this with the 'box and string' interface of this tool.

    Here is a simplistic solution solution I came up with. This sample patch demonstrates cycling over a fixed set by rendering live video from an iSight onto the surfaces of a 3d spinning cube, applying a cycle of realtime video filter effects to the image.

    quartz-composer patch editor

    You can follow the patch from left to right. The brains of the procedure is the multiplexer , which is a patch that selects one out of a set of possible numbered inputs, depending on the value fed into it's Source Index field. In order to generate a periodic iteration over the right integers, I'm employing a linear interpolator, with a range of 0 - 3 , over a duration of 20 seconds. Because this is generating floats, I'm plugging it through a Round patch, that grossly rounds it to the nearest integer, before feeding it to the multiplexer. To get an even rounded cycle, tweak the interpolater range down a step, -0.5 to 2.5. The rest of the sequence is simple - the video input is split through three filters, one of these paths is selected via the looping mechanism, and that output is connected to the Image property of the built-in cube patch.

    Here is the 7.1Kb .qtz file . It's not a terribly pretty end result, but it is quite impressive considering that it's such a tiny source file. The looping construct it illustrates is very simple, but could be used to build up any sort of repeated cycle over a set of different input paths, such as image files, or colour tones that you could connect to other patches to build cyclic displays.

    posted by cms on
    tagged as