Following on from my automation of running the VMWare vSphere
Client from my OS X
Desktop,
it occured to me that it would also be useful to be able to launch
VMWare Fusion VMs directly
from my desktop -- eg, using Alfred -- without any navigation effort.
And particularly to have the VMWare Fusion "Virtual Machine Library"
disappear automatically: it auto-opens every time VMware Fusion
is launched, with
no apparent way to turn it off -- except Single VM
mode
which does not suit my use case. That way I could, eg, type
"ciscovpn
" and have my VM for the Cisco VPN Client start, or type
"dockervm
" and have my VM for running
Docker
start.
The first part is trivial: VMs can be launched by opening their
*.vmwarevm
directory,
which will bring up VMWare Fusion and "play" that VM -- starting
it or resuming it as required. That can be done with open
on the
command line, or easily in Automator.
The second part is a little bit more complicated. Superuser.com
provided the necessary hint to close arbitrary named
windows,
by using AppleScript to tell a named application to close any windows
with a specific name. I have extended the shell wrapper around the
AppleScript so it can optionally try to close the window multiple
times (at 1 second intervals) to get around race conditions in the
timing of the window appearing. The resulting script,
closewindow
(BSD
licensed) makes closing the application window from the command
line trivial.
To tie this all together we can use Apple Automatator again:
Open
Automator
and create an ApplicationDrag "Get Specified Finder Items" from the Action Library, and then use Finder to find the right VMWare VM directory and drag that onto the action.
Drag "Open Finder Items" from the Action Library below that (I left it set to its "Default Application" setting, which should work for
*.vmwarevm
directories).Finally below that drag "Run Shell Script", and have it run:
~/.bin/closewindow "VMWare Fusion" "Virtual Machine Library" 3
which will make three attempts (with one second between each attempt) to close the "Virtual Machine Library" window. Normally the first or second attempt should work.
Then save that as an application, eg, "ciscovpn
", put it in
/Application/Wrappers
.
It should then be possible to invoke Alfred, and type ciscovpn
and
the Virtual Machine will launch, with the Virtual Machine Library window
disappearing quickly.
The result is not perfect (the "Virtual Machine Library" window does still pop up briefly if VMWare Fusion was not already running), but it cleans itself up quickly, so it is much more convenient than doing all the steps by hand.
ETA, 2016-10-24: It turns out that VMware removed AppleScript
support in VMware Fusion 7,
and it is also not present in VMware Fusion 8/8.5 either. Which
means that closewindow
does not work. However there is an
alternative, which is to use the vmrun
command embedded in VMware
Fusion -- at least
VMware Fusion 8 -- to start the VM, which avoids the "Virtual Machine
Library" window appearing at all. Usage is something like:
"/Applications/Purchased/VMware Fusion.app/Contents/Library/vmrun" start /VMDIR/VMNAME.vmx
(at least with the custom location where I put the VMware Fusion.app
).