Why should I dare to launch the VirtualBox GUI each and every time when I in fact only want to start a particular VM?
So my intent is to create an icon in the OS X dock which allows me to start my Windows VM directly with one single click.
Not to say that I’m a lazy person, but I dare to get around the GUI for such a simple and straight forward task 😉
Luckily VirtualBox provides us with everything needed to accomplish this through the “VBoxManage” command.
The rest is just some (not so really) magic OS X trickery, as you’ll find out shortly.
Download the Icon
You’ll most likely want to have a decent icon for your application shortcut.
Since OS X can’t work with standard Windows .ico files, you’ll need a special OS X icon (.icns format).
As a convenience I provide you with my own here. Just download to your “Downloads” folder. You will need to extract it first before you continue.
If you want to make your icon, here’s a short howto on this topic.
Create the Application
This task is entirely done from OS X Terminal, so let’s start a new Terminal.
If your Mac is set with defaults, your startup directory will be your home directory, which should evaluate to /Users/USERNAME.
Mine is /Users/Gianpaolo for example. You can always check your current working directory using the ‘pwd’ command.
I keep all my VMs under my home directory in a folder called “VirtualBox VMs”, where each VM again has its own directory.
My Windows VM is called “Win7onMBP”, so I need to change to it’s directory first:
cd "VirtualBox VMs/Win7onMBP"
As said earlier, the “VBoxManage” command will be used to launch the VM. As a convenience I create a real OS X application shortcut, which can be used like every other application, e.g. dragged onto the OS X dock. This offers a maximum of integration into the Finder as possible. Though it may seem a bit more complicated to setup, you’re actually better of with this method than with tradional aliases.
Just paste these lines below directly into your Terminal to create the application and all of it’s dependencies.
For it work properly, please make sure you downloaded my icon file to your Downloads folder as “win7.icns”.
mkdir -p "Windows 7.app"
mkdir -p "Windows 7.app/Contents"
mkdir -p "Windows 7.app/Contents/MacOS"
mkdir -p "Windows 7.app/Contents/Resources"
cp ~/Downloads/win7.icns "Windows 7.app/Contents/Resources"
cat > "Windows 7.app/Contents/MacOS/startvm" << EOF
/usr/bin/VBoxManage startvm Win7onMBP
chmod 755 "Windows 7.app/Contents/MacOS/startvm"
cat > "Windows 7.app/Contents/Info.plist" << EOF
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
Here’s a screenshot of my Terminal transcript:
Add the Application to the Dock
As said before, I want the application icon in my dock. So here’s how to do it. For this you won’t need Terminal anymore, so you can safely close it now. Instead you’ll need to fire up Finder and navigate to your VM directory. Find the “Windows 7” app created earlier and select it.
Then simply drag it onto the OS X dock.
Voilà, you’re done.