Fix VirtualBox icon app name in Gnome Favorites/Activities menu on Linux

(I sometimes craft my headlines to be search-engine-friendly, which is why they sometimes look odd in person). If you have multiple VirtualBox images running in Ubuntu or similar Debian-based flavors of Linux, you know it can be frustrating to switch between the different images using the favorites icons. They all look the same, and they all have the same generic "VirtualBox Machine" label. You have to click on one, see which screen comes up, and if it's not the one you wanted, click on the next one.

And to make matters worse, the identical icons appear in random order, so you can never be sure which one is which.

Then, to compound things even further, sometimes the icons appear with no images at all.

It's been a problem for years. Maybe one of these days VirtualBox will fix it, but maybe not.

There are lots of complicated attempts to fix this problem, but I finally found a very simple solution, using xdotool. It works like this:

$ xdotool search --onlyvisible --name "VM Name" set_window --class "Whatever"

You simply need to do a sudo apt install xdotool to get this working.

I wrote a script around it, and here it is. I call it vboxNamedStart.sh:

#!/bin/bash
vboxmanage list runningvms | grep -q "$1"
if [ $? -eq 0 ]
then
echo "$1 already running"
else
vboxmanage list vms | grep -q "$1"
if [ $? -eq 1 ]; then
echo "$1 is not a valid Virtual Machine name. Try again?"
exit 0
fi
echo "Starting $1..."
VBoxManage startvm "$1"
# previous line ends too fast, so wait...
sleep 5
fi
xdotool search --onlyvisible --name "$1" set_window --class "$1"

If you already started the VM, it will simply change the name. If you haven't started it yet, it will start it, then change the name. If you just want to change the name, use the one-liner I mentioned above.

Save this script to your home folder. Feel free to rename the script if you want. Use chmod +x vboxNamedStart.sh in order to make it executable.

Then you can call it like this:

./vboxNamedStart.sh WindowsBox

to start the VM named 'WindowsBox'.

Or, you can create an alias or two to make it even easier:

nano ~/.bash_aliases
alias cbox="/home/clearhat/vboxNamedStart.sh 'Clearhat UbuntuBox'"
alias wbox="/home/clearhat/vboxNamedStart.sh 'Clearhat Windows'"
alias qbox="/home/clearhat/vboxNamedStart.sh Geronimo"

(After making this change, restart your terminal session to get aliases working). Then all you have to do is type 'cbox' and it will start the correct VirtualBox VM, rename its icon, and refresh the image it so that the icon actually appears.

To find out the actual names of your VM images, either look at the VirtualBox manager UI, or run this:

vboxmanage list vms

Here's what it looks like in action:

Have fun.

Note, the error checking for passing invalid names is pretty fragile, so you'll get a messy error if you try to call it with a name that is partially correct. You may find other issues because regular expressions are involved, but for the most part, it works well and is short enough you can easily hack it to make it work better if you want.

Update 2021-11-19: I found that adding the "--onlyvisible" flag helped resolve some of the fragile-naming issue, it turned out that some of the problem happened when there were several windows open(?) for the same vm image. Not sure why because I'm not an X windows expert, but confining the search to windows which were visible helped. You may find that tweaking such flags will help you with your edge cases. Also note the "wmctrl -l" command might be able to help narrow your query if you happen to be running a lot of VMs with similar names.

Add a comment

Fields followed by * are mandatory

HTML code is displayed as text and web addresses are automatically converted.

Page top