Starting a VirtualBox VM through a Shortcut on OS X

Posted by: admin  :  Category: OS X, VirtualBox

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
#!/bin/sh
/usr/bin/VBoxManage startvm Win7onMBP
EOF

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">
<plist version="1.0">
<dict>
<key>CFBundleExecutable</key>
<string>startvm</string>
<key>CFBundleIconFile</key>
<string>win7.icns</string>
</dict>
</plist>
EOF

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.

10 Responses to “Starting a VirtualBox VM through a Shortcut on OS X”

  1. phaq » Blog Archive » Refining application shortcut for shared BootCamp / VirtualBox Windows VM Says:

    […] Here’s a follow-up to my previous posts on sharing Windows 7 in BootCamp and VirtualBox and launching a VirtualBox VM directly through an icon shortcut. […]

  2. ToSca Says:

    This is of great help.

    Thank you.

  3. thib Says:

    hello, I made the startvm work, but now it’s the app that doesn’t launch … “You cannot open the application because it is maybe broken or incomplete.

    thx

  4. gdelmatto Says:

    I guess the permissions for the StartVM script are wrong, please ensure that you run this command:

    chmod 755 “Windows 7.app/Contents/MacOS/startvm”

  5. Iniciando uma VM do VirtualBox diretamente pelo Dock | Blog do Menotti Says:

    […] uma VM do VirtualBox diretamente pelo Dock Posted on 23 de março de 2012 by Ricardo Menotti Starting a VirtualBox VM through a Shortcut on OS X This entry was posted in Diversas and tagged Mac OS X, Post que resolve, VirtualBox by Ricardo […]

  6. Ken Says:

    Genius – Thank you for posting.

  7. Thomas Says:

    Thank You!

  8. John Peden Says:

    Did the same thing myself for a Ubuntu VM. Works a treat!

  9. MGarneau Says:

    This was great. Thanks for writing this up. Made the process really simple.

  10. Ricardo Says:

    Works great. I did it for Ubuntu. Thank you for sharing.