Toggling audio devices in Mac OS X
I frequently switch between my external computer speakers and my headphones. Repeatedly plugging and unplugging my headphones started wearing out my headphone jack, so I hooked up an iMic to a USB port to give myself a second headphone jack. This allows switching between them in software through the Sound preference pane, which is more convenient, but still takes too long. So the next step is automating the switch. This thread on Mac OS X Hints has people discussing various ways to do it. The easiest GUI way to do the switch on Snow Leopard is just option-clicking the menu bar volume control and selecting the new output device (on Leopard, you can do effectively the same by installing SoundSource).
I'd like to avoid using the mouse at all, of course. Someone got the sources to SoundSource and created a little command-line app called audiodevice that can do the switch without a GUI. Making progress, but that still requires executing a custom command to switch from the headphones to speakers or vice versa. As a final step, I threw together a quick script that uses audiodevice to toggle between the two without any arguments:
#!/usr/bin/perl -w
use strict;
my $device1 = 'Headphones'; # my speakers, plugged into my computer's jack
my $device2 = 'iMic USB audio system'; # my headphones, plugged into my iMic's jack
foreach (`audiodevice`) {
if (/output: (.*)/) {
if ($1 eq $device1) {
system("audiodevice output '$device2'");
exit;
} elsif ($1 eq $device2) {
system("audiodevice output '$device1'");
exit;
} else {
print "Unrecognized output device '$1'.\n";
exit;
}
}
}
print "Failed to find output device.\n";
I saved this script as audioswitch and added it to my LaunchBar index so I can call it from anywhere using only 4 or 5 keystrokes. Works exactly as I'd like, and you can modify it to toggle between any two devices by substituting the name of the devices (in the Output tab of the Sound preference pane) into the script.
UPDATE (3/11/10): I've fixed up the above script so that it automatically figures out what the two output devices are and switches between the two accordingly. This means that it'll work for switching between the iMic and either internal or external speakers. Also fixed a problem where system sounds would continue to play through the original device after switching and added a Growl notification with growlnotify.
#!/usr/bin/perl -w
use strict;
my $device1;
my $device2;
foreach (`audiodevice output list`) {
chomp;
if (defined $device1) {
$device2 = $_;
last;
} else {
$device1 = $_;
}
}
foreach (`audiodevice`) {
if (/output: (.*)/) {
if ($1 eq $device1) {
activate($device2);
exit;
} else {
activate($device1);
exit;
}
}
}
print "Failed to find output device.\n";
sub activate {
my $device = shift;
system("audiodevice output '$device'");
system("audiodevice system '$device'");
system("growlnotify -n audioswitch -m 'switching to $device'");
}
Remote controlling an iSight
One of the neat things to do with a remote Mac is to control the built-in iSight remotely to snap photos. There are several commercial applications to do this, but we're going for the low-tech approach: taking pictures via command line over SSH. The free app iSightCapture is designed to do this, but hasn't been updated in awhile and doesn't work as-is on anything after 10.4.9 (including 10.5 and 10.6) due to new security restrictions. I put together a little script that basically wraps iSightCapture so that it works as designed on an up-to-date system.
You'll need to have iSightCapture installed and in your $PATH -- you can either do that by manually installing the binary at the above link or via MacPorts with 'sudo port install isightcapture'. Then create a new file in /usr/local/bin (or wherever) and put in the following Perl snippet:
#!/usr/bin/perl -w
use strict;
foreach (split(/\n/, `ps aux | grep loginwindow`)) {
if (/^\w+\s+(\d+).+?loginwindow\.app/) {
my $sudo = ($> == 0) ? '' : 'sudo ';
exec("$sudo launchctl bsexec $1 isightcapture @ARGV");
exit;
}
}
print "Failed to find loginwindow process.\n";
I called the new binary 'snap', so at the command-line, I just type 'snap image.jpg' instead of 'isightcapture image.jpg'. The only real difference from isightcapture is that you need sudo -- that's an intentional Mac OS X security restriction that can't be circumvented (and probably shouldn't) as far as I know. All the iSightCapture command-line options should work as-is, so just refer to its documentation for those. The wrapper script is definitely a hack that might break with a future OS update, but has worked flawlessly for me on several different machines running a variety of Mac OS X versions.
Planescape: Torment in Crossover
Update (July 2010): This method appears to have been broken by Crossover Games 9.0.0, and remains broken in the beta of Crossover Games 9.1.0, because switching the resolution with SwitchResX causes Crossover to freak out. It remains working flawlessly in Crossover Games 8.1.4, so stick to that version for now (I've filed a ticket with Codeweavers on this issue so hopefully it'll get fixed). Downgrading from 9.0.0 to 8.1.4 was as simple as replacing the application for me.
Ever since Macs made the switch to Intel processors and made it easily possible to run Windows applications, I've taken the opportunity to play some classic PC games that never made it to the Mac. One of these is Planescape: Torment, an RPG with a unique setting and great story (a feature sadly often lacking from more modern titles). I started playing it around a year and a half ago through Crossover Games, which in addition to not requiring a reboot, doesn't even need Windows. PS:T worked flawlessly in Crossover (both on my iMac and MacBook) until Apple issued the Mac OS X 10.5.6 update, which included new graphics drivers that completely broke the game -- I (and everyone else playing through Crossover) couldn't even get to the title screen without the game complaining about a bad resolution or color depth and crashing.
Seeing as I was only about a third of the way into the game when this happened, I tried pretty extensively to get the game running again. My Boot Camp installation (Vista at the time) ran the game but had weird graphical glitches on the spell effects as a result of modern graphics drivers, and I didn't really want to have to reboot anyways. VMware Fusion 3 running either Windows 7 or XP also runs the game but exhibits an extremely annoying cursor and animation flicker (I've encountered this flicker in other apps on Fusion 3 that worked great in Fusion 2, and it's one of the reasons I'm not very impressed with v.3 compared to v.2 -- but that's a rant for another time). I also tried in Parallels, which thankfully didn't have any of the aforementioned graphical glitches, but unfortunately also exhibited constant sound glitches including pops and crackles.
Last night I came across my CDs again, decided to take one more whack at it, and, to my suprise, managed to get the game running perfectly in an up-to-date Crossover Games (8.1.4) on a brand-new 27" iMac running Mac OS X 10.6.2. For anyone else frustrated by being unable to get this game running, here's how I did it:
First, we have to install the game and a whole bunch of community mods. These mods do a variety of nice things like making the game run in modern widescreen resolutions, fixing a pile of bugs not fixed by the official patch, and both adding and expanding game content. All good stuff that you should be installing anyways because it gives PS:T a lot of much-needed polish, but in this case, we'll also need them to get the game to run at all. To do the installation and modding, follow this very detailed PS:T installation guide, with a few caveats:
- While installation of the base game works perfectly in Crossover, the mod installations didn't seem to work. I worked around this by performing only the base installation in Crossover, then firing up a Windows XP virtual machine in Fusion to do the full installation (base installation again, then all mod installations as described in the install guide). Once that was done, I copied the modded "Planescape - Torment" folder back to my Mac and replaced the unmodded folder that Crossover had installed in the Program Files folder. The .app file that Crossover installs still works fine for launching the replaced application. For performing the full PC installation, any VM or native installation should do (Fusion, Parallels, Boot Camp, a separate PC, etc).
When installing the widescreen mod, make sure you use the exact native resolution of the Mac that you'll be playing on, even if you think it's overkill for a game that was released at 640x480. In my case, that was 2560x1440 for my 27" iMac. This is where the larger text mod comes in handy -- I installed the 80% increase. I think if you don't install the native resolution, you won't be able to get the game to launch at all.
I've discovered that the above is actually not true -- you can use any resolution as long as it's natively supported by your monitor. I've also found that running at very high resolutions like 2560x1440 causes some graphical jerkiness, particularly in text scrolling, which is distracting considering the extensive dialogs in the game. I settled on using 1600x900 on my 27" iMac, which is perfectly smooth and still looks great. On a 13" laptop, I'd recommend using the native 1280x800, which is perfectly smooth on an aluminum MacBook.
One you've gotten your modded installation set up in Crossover, there's one more obstacle to overcome -- you have to set your monitor to thousands of colors or Planescape will error out on launch. The problem here is that the Displays preference pane in Snow Leopard no longer lets you change the color depth from Millions. To fix this, we need to install the SwitchResX tool, which will let us change to thousands of colors. The option we want in this utility is a bit hidden -- first go to the Menus tab of the SwitchResX Preferences, then activate the Menu Extra by checking the box. This makes a SwitchResX menu appear in the Mac OS X top toolbar, which should allow you to select Thousands of Colors.
Almost there. To launch the game, switch to thousands of colors before launching Crossover Games. If Crossover is already open when you switch, it won't work. If you're not running Torment at your monitor's native resolution, you shouldn't need to switch that manually -- the game seems to be able to switch the resolution itself, just not the bit depth. After switching to thousands of colors and then launching Crossover, you should be able to start Torment from the programs menu, and (fingers crossed) assuming everything worked, Torment should launch to the intro videos successfully. If you don't have the physical CD in the drive, the game may demand CD 2 -- I worked around that by mounting a disk image of disc 2, which was accepted without complaint.
I've only tried actually playing the game for a few minutes since getting it running again, but it seems to be perfect -- runs smoothly and no apparent graphical or sound glitches. The one issue I have on my iMac is that the very bottom of the screen is clipped in-game. I suspect that's a result of the UI mods expecting a typical 16x10 resolution rather than the 16x9 resolution of the new iMacs, but I'll either find a workaround or just play on my 16x10 MacBook. After installing on my MacBook, I found that the screen clipping issue was still present -- it doesn't look to be a symptom of any particular resolution. Luckily, I played around a bit and found a very easy one-time fix. In Crossover Games, go to Configure -> Manage Bottles, then select the bottle that Torment's installed in (in my case, that's TORMENT_1). Select the Control Panel tab and launch winecfg. In the Wine configuration window, select the Graphics tab and deselect "Allow the window manager to decorate the windows" and "Allow the window manager to control the windows". That is, none of the four "Window Settings" boxes should be checked. After you've done that and hit Apply, go back to the Control Panel and reboot the bottle by launching the reboot item. Now launch Planescape...no clipping!
If you've gone to all the trouble of setting this up, you probably already know what a great game Planescape: Torment is, but regardless -- have fun!
My Plug for the SheevaPlug
A month or two ago I picked up a Marvell SheevaPlug to use as a multipurpose home server. Previously, I'd been running all my network services (primarily this web site and an SSH server) on my iMac, but this had several downsides: I had to leave my iMac on all the time, and, more seriously, all my services went down every time I wanted to reboot into Windows. Being a lifelong Mac user, I didn't have an old PC lying around to dedicate as a full time Linux server, and my tiny apartment wouldn't accommodate a tower very well anyways. The SheevaPlug was an ideal alternative -- tiny, silent, very low power, and powerful enough for my home needs.
The plug comes with a 1.2 GHz single-core ARM processor, 512 MB of RAM, 512 MB of built-in flash (preloaded with an Ubuntu 9.04 installation), and a gigabit Ethernet port. The processor isn't going to compete with a typical dual or quad-core Intel processor, but it's got plenty of punch for a regular Linux installation and beats the heck out of a typical NAS box. The flash is something of a problem, since it's quite easy to fill up the 512 MB with applications and obviously you can't store any appreciable amount of media on it. Luckily, the plug also has a USB 2.0 port and an SD card slot. The board itself also has an eSATA connection, but the most common SheevaPlug model (manufactured by GlobalScale Technologies) doesn't have it exposed. Some newer manufacturers have started building in an eSATA port and some adventurous souls have successfully modded their plugs to expose the existing connection, but I don't trust myself that much with a soldering iron so I've stuck to USB for external hard drives. My plug and all peripherals only cost me about $200 total for the plug ($99, purchased here), a 500 gig hard drive ($50), an external USB enclosure ($30), and annoyingly steep shipping costs for the plug ($20). I'm not using the SD card slot at the moment, but a popular use for it is to put the kernel and OS on an SD card and just use a USB hard drive for media -- this is beneficial both because the bootloader is more reliable booting from an SD card than USB and also because it allows easily swapping out the OS on multiple cards.
The default Ubuntu installation is fully functional, which is nice because you can just connect the ethernet cable and SSH into the plug. Unfortunately, there are a few weird or broken things in the default install that make doing a clean install desirable. A bigger problem is that you can't upgrade the plug to Ubuntu 9.10 because 9.10 builds with an incompatible ARM instruction set, so as soon as support for 9.04 ends, you're out of luck. This is why a significant number of people have loaded other distros onto it (Debian and Gentoo being the two I've seen most mentioned). Being most familiar with Ubuntu, I went with Debian, which also has a great installation site courtesy of Martin Michlmayr. I installed everything on my USB hard drive and left the internal flash untouched as a backup. Booting the kernel off USB is a little flaky due to the buggy u-boot bootloader, but once the plug is booted it's rock solid for weeks on end.
I'm running a whole pile of services on my plug 24/7:
- This web site: Apache 2, PHP 5, and MySQL 5, with WordPress for the blog.
- The Transmission BitTorrent client, controlled via the slick web interface. Transmission is nice and efficient and rarely makes my plug sweat even when maxing out my cable connection.
- mt-daapd for serving music to iTunes clients on my network, gradually switching over to forked-daapd for both music and video streaming to Front Row.
- An NFS server sharing a dedicated media partition on the external hard drive -- I use this to share a single iPhoto library across my iMac and two laptops, and also house my music collection on it. I've set my iMac to manage my NFS music collection in iTunes directly, so whenever I import new music on my Mac, it gets copied over to the SheevaPlug automatically where it's picked up by forked-daapd and shared across the network. This also lets me easily sync my iPod on my Mac without maintaining two copies of my music library. I picked NFS for sharing vs AFP or Samba because (1) I don't have any Windows clients to worry about, and (2) NFS easily has the best performance. My LAN is only 100 mbit rather than gigabit, but the SheevaPlug easily maxes out my bandwidth (~11-12 MB/sec). I've read accounts that on a gigabit network, the plug can push around 30 MB/sec over NFS, though significantly less with Samba.
I'm constantly refining how I use my plug, but I've been very happy with how it's worked out, and I can now turn off or reboot my Mac into Windows at will without interrupting my site, torrents, or music streaming. Getting everything running just right definitely requires some careful research and trial and error, though -- don't expect to get everything working in one night (or one weekend, for that matter). Lastly, I should point you to the most useful general SheevaPlug resources on the web I found:
- The aforementioned Debian on SheevaPlug site for getting a fresh Debian installation running
- The ComputingPlugs wiki -- lots of good info for getting set up initially, though some of the kernel specific stuff (like patches) are out of date.
- The OpenPlug wiki -- I referred to this a couple of times when I couldn't find things on the ComputingPlugs site, and finally,
- The Plug Computer Forums -- forums for everything from the bootloader to software installations to hardware mods. Lots of good stuff and a place to ask questions if you're stuck.
Happy hacking!