Archive | Mac

Running a BASH script when my Laptop is opened at home – Part 1

My laptop hard drive died. I was upset only because there was data on my laptop that had not been moved over to my desktop. My desktop has time machine and is rsynced to several other drives and locations.

So I lost data. My own fault because I was not diligent in moving data off my laptop.

To make sure this did not happen again, I needed the ability to run a script when I open my laptop at home. But how?

  • First, I thought about identifying being at home by my wireless SSID. That was okay, but what if I was connected by wire?
  • Second, I thought about identifying home based on my subnet. Well, I could find myself on a network with the same range, and that could be bad.
  • Then I found this link that showed some code on how to get the MAC address of the defined Default Gateway. Perfect.

MAC addresses should be unique. Therefore the BASH script should only run when I am on my home network.

Here is the BASH script to find the Default Gateway’s MAC address

GATEWAY=`netstat -rn | grep default | cut -c20-35`
MACADDRESS=`arp -n $GATEWAY | cut -f4 -d' '`

Next step is to use this code when I open my Laptop lid.

Using a MacBook to connect to a Cisco router

We have a “USBG-232MINI” USB to Serial adapter and I needed to connect to a Cisco router. GNU screen to the rescue!!

The command is

screen /dev/tty.usbserial-A9005yuF 9600 (Where A9005yuF is probably unique)

And to exit hit Control+A then K.

Google Reader Starred items to

I use from Reinvented Software as my archiving solution – my knowledge base. I like the product because it leaves the pdfs I create on the filesystem and the db contains the tags and links associated with each file. I used to use Yojimbo, but it keeps all the files in their database. I am not sure it is that big of an issue (especially because we are considering SharePoint as a document management system!), but I am living with I just need a way to get my data to my iPhone – but that is another issue.

My information consumption workflow starts in Google reader, and Reeder for the iPad and iPhone, and ends in Interesting items are “Starred” in Google Reader, and I needed a way to get the starred items to I could not find a way to do it in bulk until I ran across this post explaining how to dump your starred items to a html document. I took the script a little further and I used apple script to import the url into


require "rubygems"
require "open-uri"
require "simple-rss"
feed = ""
rss = SimpleRSS.parse open(feed)
rss.entries.each do |item|
puts "Downloading: #{item.title.sub( ":", "-" )}\n"
%x(osascript -e 'tell application \"Together\" to import url \"#{}\" as web PDF')

Make your starred items public, and change the “0000000000000000000000” to your user id (as described in the the original post). Run it, and 50 starred items at a time will be added to your
My colleague suggested that I unstar the item automatically after added to Together, but I will have to sit down and figure that out.

OS X: Move files based on creation date

I wanted to organize my flip videos – I wanted them moved into a folder named after their creation date.

One of the scripts that I found uses ls’s “–time-style” parameter. BSD’s ls does not have this value. MacPorts’ coreutils port contains gls (GNU’s ls) that does support the parameter. So here is my script to move movies into a sub folder based on the movies creation date.

for filename in *.MP4; do
    datepath="$(gls -l --time-style=+%Y-%m-%d $filename | awk '{print $6}')"
    echo "$datepath"
    if ! test -e "$datepath"; then
        mkdir -pv "$datepath"
    mv -v $filename $datepath

OS X Finder Service to convert TIFF files to JPG

It drives me nuts that defaults to .tif as the file type. And no matter how many times I try the recommended :

defaults write type jpg

I can not get it to default to save as a jpg (and I tried jpeg). All I can guess is that this command does not work in 10.6. I don’t know. It drives me nuts.

So I sat down and created a quick OS X Service in Automator that takes files as an input and changes them to jpg. First in automator I selected “Service” as my workflow type

Next, I set the Service receives selected “files or folders” in “any application” values. Then I added the “Change Type of Images” action and set “To Type” to JPEG.

Hit save as “ChangeToJPEG” and now when I right click an annoying .tif, I have an option under Services to ChageToJPG is still annoying, but this makes it more tolerable.

Xnest, xdmcp and X11 on CentOS

It has been a while since I have used XNest. It works. Slow, but it works. I can ssh into a box and bring back a full X session back to my Mac.

In CentOS 5.5 I had to edit /etc/gdm/custom.conf and add:

Enable=true under [xdmcp]. Restart X and now I can run:

Xnest :1 -geometry 1024×768 -kb -query localhost

which will bring a gnome session back to my X11 server.

Very easy with out having to open any ports!

GNU date vs BSD date

I usually develop and test my BASH scripts on my mac, mostly for use on RedHat systems. Occasionally I run into problems with this workflow. Recently I realized there was a differnce between the date command on RedHat and the date command in OS X. Turns out BDS date != GNU date. The workaround, install coreutils from Mac Ports, and add this alias to my .bashrc:

alias date=”/opt/local/bin/gdate”


Update: gdate is part of the GNU coreutils, and the MacPorts install command for gdate is: sudo port install coreutils

My mac ports cheat sheet

These 3 commands will check for new ports, upgrade outdated ports, and remove older versions.
  • sudo port selfupdate
  • sudo port upgrade outdated
  • sudo port uninstall inactive

To make sure you have a slimmed down install, use port_cutleaves to remove unnecessary ports. There are often “Build Dependencies” (like autoconf, automake, libtool, m4, help2man, p5-locale-gettext) that are no longer needed after a package is installed.

  • sudo port install port_cutleaves
  • sudo port_cutleaves (I run this a couple of times)

Waking a sleeping Mac Pro upon opening a folder


I have two macs at home, a MacPro and a MacMini. The MacMini is attached to our TV. I put my MacPro to sleep when I leave for work in the morning. My wife comes home and tries to play videos for my son on the Mac Mini. The videos are actually on the Mac Pro, but it is transparent to her, when the machine is on. And that is the problem. When she clicks on the symbolic lynk and the MacPro is sleeping she can’t find the videos she is looking for.

I needed a way to wake the MacPro when she is looking for the videos.

This is longer post describing my whole Wake/Sleep setup. Requirements are MacPorts, and a Wake on LAN (WOL) utility. I use DDWRT, so there is one on my home router.

I am a big fan of MacPorts. I used to use Fink, but I switched, and I don’t remember why. There are two utilities in MacPorts that are useful for sleeping macs, Sleepwatcher and wakeonlan. You could install Sleepwatcher via source, but I prefer a Package management system.


Sleepwatcher is the most important part of this system. I used to put my Mac to sleep every night at 11 pm, but if I enabled “Wake for network access” in the energy saver preference, the machine would wake up every two hours. This article describes the problem and a solution – sleepwatcher.

So I installed sleepwatcher via MacPorts. Then I added the following two lines to my /opt/local/etc/rc.sleep ( I could not get it working in my “$home/.sleep” file)

/bin/sleep 1
/usr/sbin/systemsetup -setwakeonnetworkaccess on >/dev/null

Then I added the following to my /opt/local/etc/rc.wakeup (again I could not get my “$home/.wakeup” to work)

/usr/sbin/systemsetup -setwakeonnetworkaccess off >/dev/null

This allows the machine to go to sleep and not wake until it receives a WOL packet.

That takes care of the sleep part.


Now my machines are sleeping (properly), and they can be woken from a WOL packet. Since I use DDWRT, I can go to the web interface and wake a machine (I have OpenVPN tunnels going all over the place, so i can access the web interface internally). It occurred to me that if there is a web interface, there has to be a WOL executable on the router. With public key authentication, I can connect to my DDWRT router with the following command and wake a machine:

ssh homerouter "/usr/sbin/wol -i 192.168.X.255 xx:xx:xx:xx:xx:xx"

That takes care of the wake part.

Folder Actions

To have a machine wake when I access a folder, I add the following applescript to a “Folder Actions”:

on opening folder this_folder
		tell application "Finder"
				set ping_result to (do shell script "ping -c 1 machine.trying.towake;echo -n")
				if "100.0% packet loss" is in ping_result then
					do shell script "ssh homerouter "/usr/sbin/wol -i 192.168.X.255 xx:xx:xx:xx:xx:xx" "
				end if
			end try
		end tell
	on error errmsg
	end try
end opening folder

If the machine does not answer a ping, the script will ssh to the ddwrt router and launch the wol executable to wake the sleeping machine.

A complex system, but it works.