I needed a quick way to determine if the machine I was running a shell script on was a laptop or a desktop Mac. (The script I was working on is used to create a unique hostname for the machine automatically, and the machine type is part of that hostname)
It occurred to me that I could easily determine if the script were executing on a laptop by detecting the presence of a battery. I know that some basic battery information can be pulled from ioreg -w0 -l and that the same command returns nothing on Macs without a battery, so all I had to do what see if something came back from that command.
The code looks something like this:
#!/bin/bash
type=`ioreg -w0 -l | grep Capacity`
if [ -z "$type" ]; then
type='Desktop'
else
type='Laptop'
fi
echo $type
This seems to work well in Snow Leopard (10.6) and Lion (10.7).
