biotext.org.uk

Tips

How to make sure your media disk is mounted before starting iTunes

by Andrew on Dec.30, 2009, under Tips

This is an Apple Annoyance that’s been bugging me for a while — if your iTunes library is on an external HD, and you start iTunes without it mounted, the bloody thing can seriously mangle its own library index (under Tiger at least).

So I’ve replaced my iTunes Dock icon with a little AppleScript that checks whether said disk is mounted first:

1
2
3
4
5
6
7
tell application "Finder"
	if exists (disk "LaCie") then
		tell application "iTunes" to activate
	else
		display dialog "Don't start iTunes without LaCie external disk mounted." buttons {"OK"} default button 1 with title "Cannot start iTunes" with icon stop
	end if
end tell

Compile, save as script, add to dock, bingo. (Obviously, replace LaCie with the name of your external HD, as it appears in /Volumes when mounted.)

For extra points, you can give it iTunes’ icon too. Just get Info on iTunes and select the little icon in the top left (not the big one under Preview). Then just cmd-c, select the same icon in the script’s info window, and cmd-v.

Leave a Comment : more...

MacBook keyboard hacks for # (hash/pound/numbersign)

by Andrew on Nov.07, 2009, under Tips

One of the few annoying things about my oldish MacBook Pro is its keyboard, for example a few unresponsive keys, but particularly the lack of a # key. It’s a UK keyboard, and has £ for shift-3, and # is hidden in alt-3 (not labelled).

This is fine in native desktop apps, but less fine in some text-mode programs (e.g. vim), when for some reason this often produces a superscript 3 instead.

So I’ve set up a custom keyboard mapping in iTerm to map F3 to #, which works nicely. However!

If I’m SSHed in to a remote Linux machine (or even my local Ubuntu VirtualBox) neither of these keys work in X apps. But, xmodmap (via the config file ~/.Xmodmap) can help. For some reason, Macs all have a dedicated key for these characters — § and ± — which no-one ever uses. But with this line in ~/.Xmodmap we can remap it to produce #:

keycode 18=numbersign

UPDATE: I’ve found a better way which works pretty much globally…

Using Ukelele you can copy the British keyboard layout and then remap keys to your heart’s content. I’ve moved the § character to the alt-§ key combination, in case I ever need it, and moved the # character to the raw § key. This seems to be respected almost everywhere so I don’t need to mess around with alt-3 or F3 any more. Joy. It also works over JollysFastVNC to a remote RealVNC server, which none of the other methods did.

Unfortunately, things still aren’t perfect. If I actually open a VirtualBox console session into GNOME on the local Ubuntu VM, the pointless § and ± key actually produces < and > so neither of these tricks work. In fact, I can’t get anything to generate a # even though I have the MacBook Pro Intl keyboard layout selected in GNOME. Any ideas?

UPDATE 2: YES!! I’ve finally cracked it for VirtualBox. With the help of the xkeycaps command, I discovered that X the keycodes coming into Ubuntu weren’t what I thought they were — somewhere the Mac-ness of the keyboard layout was getting lost. It turned out that the § key was generating keycode 94 instead. So I set up this in .Xmodmap on the Ubuntu VM:

keycode 94=numbersign

Now it works in VirtualBox too. Leave gifts of thanks below :-)

Leave a Comment :, , , , more...

Penhayl Cottage website — design notes

by Andrew on Oct.17, 2009, under Tips

The website is now live for Penhayl Cottage, a self-catering holiday cottage in Cornwall, run by my family. I’m not much of a web designer, but thankfully there’s a lot of free tools around these days that make it easy for an amateur to do a decent-looking job.

The layout was based on a free stylesheet from Free CSS which a great resource for the design-impaired like me. The image gallery on the right side uses a neat Javascript library called Lightbox2.

The Google Map doesn’t use any fancy techniques like their Javascript API or anything. It’s just embedded in an iframe. This page was very useful in getting the URL parameters right for the marker and the zoom level.

The Flash video player is a very flexible open-source app called Flowplayer, and I used ffmpeg to convert the original MPEGs into FLVs to play in it.

I used the oldish version of iMovie that came with my Mac to compose the video clips into a single long movie for the downloadable versions, and to export the H.264 m4v version for the iPhone, although apparently there’s other tools like iSquint that can also do this. Incidentally, iMovie is a bit weird about file formats — it wouldn’t open the mpeg files which came from the original DV camera but arrived on a CD-ROM. It will happily open the same format if you plug the camera in directly, but the camera was in Cornwall and I’m in London. Thanks Apple. Fortunately, this thread explains how you can fool it into thinking a disk image containing the files is actually a plugged-in device.

I also used ffmpeg to transcode the m4v back into an mpeg-1. The results aren’t especially pretty but it will play on most players out there, and anyway, this is just a backup plan for non-Flash-users.

Andrew.

Leave a Comment :, more...

How to pull fields from MEDLINE the easy way

by Andrew on Oct.08, 2009, under Tips

In response to this thread on FriendFeed

curl -s 'http://eutils.ncbi.nlm.nih.gov/entrez/eutils/efetch.fcgi?db=pubmed&id=19614587&retmode=text&rettype=medline'|egrep '^(MH |DP )'

This will pull all the MeSH keywords and the date of publication from the article with PubMed ID 19614587. Stripping the field codes from the response is left as an exercise to the reader (but can be done trivially with a Unix command line utility).

Or for multiple documents, just use comma-separated IDs:

curl -s 'http://eutils.ncbi.nlm.nih.gov/entrez/eutils/efetch.fcgi?db=pubmed&id=11877539,11822933,11871444&retmode=text&rettype=medline'|egrep '^(PMID-|MH |DP )'

In this case it’s good to include the PubMed ID in the results so you know which values come from which doc.

There’s a full list of the MEDLINE field codes here, and the docs for efetch are here.

If you want to actually search by content, try esearch, which will return a list of IDs you can send to efetch.

For simple queries like this, XML is totally overkill.

Leave a Comment :, more...

Finding the dimensions of a PostScript image

by Andrew on Sep.08, 2009, under Tips

If I have images in .ps or .eps format, how can I tell how big they are? i.e. how big would they print out at ‘natural’ size without any scaling?

If they have a correct bounding box defined — more likely with EPS than PS files — this bit of Perl will show you the width and height in cm:

#!/usr/bin/env perl
 
use strict;
use warnings;
 
# Usage: measure.pl <postscript-file>
# Or send it some data on stdin.
# Assuming the BBox is correct, it'll tell you the 
# dimensions in cm.
 
while( defined ( my $line = &lt;&gt; ) )
{
    if( $line =~ /^%%BoundingBox:\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)/ )
    {
        my( $llx, $lly, $urx, $ury ) = ( $1, $2, $3, $4 );
        my $x_pts = $urx - $llx;
        my $y_pts = $ury - $lly;
        my $x_in = $x_pts / 72;
        my $y_in = $y_pts / 72;
        my $x_cm = $x_in * 2.54;
        my $y_cm = $y_in * 2.54;
        printf "%.1f %.1f\n", $x_cm, $y_cm;
        last;
    }
}

Dead handy when preparing images for a paper.

PS if you are hazy about the difference between PS and EPS — like me — have a look at this article.

3 Comments :, , more...

Awesome ways to save audio streams for future listening

by Andrew on Aug.25, 2009, under Tips

For a start, you can use mplayer to save the stream to a local disk:

mplayer -bandwidth 10000000000 -cache 32 -dumpstream -dumpfile output_file.ra 'rtsp://some.server/some_stream'

The huge bandwidth parameter will make sure that no throttling happens (at the client side anyway) — i.e. you can download hours of audio in a few seconds.

The dumpstream parameter writes the stream out byte-for-byte in the format it arrives in, e.g. RealAudio in the example above. But you can use ffmpeg to decode a RealAudio stream into a wav, and lame to re-encode it as an mp3, on-the-fly:

ffmpeg -i output_file.ra -f wav - | lame - output_file.mp3

N.B. That can be quite processor-intensive, but hey, what’s dual-core for??

ffmpeg will tell you what the bitrate of the stream is, so you can use lame’s -b switch to make sure you’re not re-encoding it at a vastly higher quality than it’s actually capable of.

e.g. for spoken word:

ffmpeg -i output_file.ra -f wav - | lame -b 64 -f - output_file.mp3

(-f = fast mode, slightly quicker encoding for less quality per bit)

Or if you’re feeling especially boffiny, use a named pipe so you don’t have to save the original stream to the hard disk:

… which is apparently possible but I haven’t got it to work yet. If you know how, leave a comment before I figure it out :-)

Andrew.

Leave a Comment : more...

RetriableTask — a generic wrapper for retrying operations in Java

by Andrew on May.20, 2009, under Research, Tips

A couple of times recently I’ve needed to write methods that retry up to n times if an error occurs, and surprisingly, couldn’t find any standard patterns to accomplish this on the web. So I wrote my own. All comments appreciated (there may well be massive holes in my logic but it works so far).

(continue reading…)

11 Comments :, more...

Running RasMol from Firefox and Finder in OS X

by Andrew on Mar.24, 2009, under Tips

A colleague asked me today, how can we set up someone’s Mac to open .pdb files in RasMol when clicking a .pdb link in Firefox. This turned out to be a non-trivial operation. Here’s how I did it, with RasMol 2.7.2 and Firefox 3.0.7 on OS X 10.4.

(continue reading…)

Leave a Comment :, , , more...

Execute SQL outside of a transaction in Hibernate

by Andrew on Mar.03, 2009, under Tips

I hit upon a snag today — while PostgreSQL requires that certain maintenance commands (e.g. vacuum analyze) are executed outside of a transactional context, it’s actually quite hard to get at Hibernate’s underlying database connection directly. Each Session object has a connection() method which returns a JDBC connection object, but this actually turns out to be a Hibernate-generated proxy for the real connection object, which refuses to work outside of a transaction.

You can probably get around this by using autocommit mode instead of explicit transactions, but this is a configuration property that affects your whole application, and is considered harmful.

Eventually I hit on a really dodgy workaround. You can manually rollback the transaction at the start of your SQL statement the old-fashioned way:


<sql-query name="nasty.hack">
<![CDATA[
rollback transaction; vacuum analyze;
]]>
</sql-query>

This also works when used with the prepareStatement() method as shown here.

Frightening but effective. If you know of a better way, let me know!

Andrew.

1 Comment :, , more...

Finding out last VACUUM/ANALYZE times in PostgreSQL

by Andrew on Mar.03, 2009, under Tips

When was my database last vacuumed or analyzed? This took me a while to figure out from googling around so I’ll put it here for posterity, short and sweet.

select relname, last_vacuum, last_analyze
from pg_stat_all_tables
where schemaname = 'public'

Add the columns last_autovacuum or last_autoanalyze if you use those features.

Andrew.

1 Comment : more...

Search

Use the form below to search the site:

Leave a comment if you can't find what you need.