1

I do most of my work in Ubuntu, but there are some Windows programs that have no good Linux equivalents and don't work in Wine, so I run those in a VirtualBox virtual machine running Windows 7. Unfortunately, when I'm in a Linux program there is no quick way that I know of to switch to the virtual machine. Currently the fastest way is to Alt+Tab to the two VirtualBox windows, keep holding Alt to see the both the VirtualBox Manager and the virtual machine, and then Alt+Tab once more to select the virtual machine.

Does anyone know how to program a hot key that will jump straight to the virtual machine?

rclocher3
  • 779
  • 8
  • 19

2 Answers2

1

Sure, this is what you should do:

  1. Install wmctrl

    sudo apt-get install wmctrl
    
  2. Startup your virtual machine, write down exactly the window name as it appears in the title bar. Mine is for example:

    Windows 7 [Draaiend] - Oracle VM VirtualBox
    
  3. The command to bring the VM's window to the front is then:

    wmctrl <window_name>
    

    NB: if name of the window contains spaces, use quotes around the name

    so in my case it would be:

    wmctrl -a "Windows 7 [Draaiend] - Oracle VM VirtualBox"
    
  4. Test the command running it in a terminal window

  5. Choose: System Settings > "Keyboard" > "Shortcust" > "Custom Shortcuts". Click the "+" and add the command:

    wmctrl -a "Windows 7 [Draaiend] - Oracle VM VirtualBox"
    

to a key combinbation of your choice.

From man wmctrl:

   -a <WIN>
          Switch to the desktop containing the  window  <WIN>,  raise  the
          window, and give it focus.

In case you run the machine from different snapshots

or in any other case when the window name might be extended by other strings, as mentioned by OP in a comment: copy the script below into an empty file, save it as run_vm.py. Run it by the command:

python3 /path/to/run_vm.py <machine_name>

where, like in the first example, you have to put the machine name in quotes if it contains spaces.

The script:

#!/usr/bin/env python3

import subprocess
import sys

machine = sys.argv[1]
command = "wmctrl -l"
wlist = [(" ").join(line.split(" ")[4:]) for line in subprocess.check_output(
    ["/bin/bash", "-c", command]
    ).decode("utf-8").split("\n") if machine in line][0]
command2 = "wmctrl -a "+"'"+wlist+"'"
subprocess.Popen(["/bin/bash", "-c", command2])
Jacob Vlijm
  • 83,767
  • Thanks Jacob, this is exactly what I asked for! For anyone else duplicating this solution, "wmctrl -l" can be used to list the names of all the open windows, which is helpful for discovering the name in the title bar of the VM window when the VM is in full-screen mode. Also, please know that the name in the title bar includes the name of the most recent snapshot, such as "[Draaiend]" in the example. So if another snapshot is taken, then you'll need to edit the keyboard shortcut, or else wmctrl won't be able to find the VM window. – rclocher3 Nov 04 '14 at 06:17
  • @rclocher3 I looked, but the window title seems exactly the same when I start from a snapshot. – Jacob Vlijm Nov 04 '14 at 06:42
  • Hi Jacob, I mean that if I'm on (Snapshot 4) and I create a new snapshot, (Snapshot 5), then I'd think that I'd need to edit my keyboard shortcut to do 'wmctrl -a "win7p2v (Snapshot 5) [Running] - Oracle VM VirtualBox"'. – rclocher3 Nov 04 '14 at 21:49
  • @rclocher3 Ah, I see, I'll take a look tomorrow and make a workaround if so. – Jacob Vlijm Nov 04 '14 at 21:51
  • @rclocher3 made a workaround, which also makes it more flexible I think. – Jacob Vlijm Nov 05 '14 at 14:01
  • I'm interested to hear it, Jacob! And thanks again for the fix, which makes my daily work-flow much smoother. – rclocher3 Nov 05 '14 at 21:26
1

There is absolutely no need to run VirtualBox Manager to start a Virtual Box virtual machine. Each VM can also be started in it's own window on the command line

VBoxManage startvm 'name of the machine'

or by defining a .desktop file that can be started with a custom shortcut, from the desktop, the dash, or from the launcher:

This virtual machine windows can then conveniently be contolled by Alt + Tab.

We could even go a step further and seamlessly integrate a running Windows application to the Ubuntu desktop (however there may be some interference with Unity when doing so):

Takkat
  • 142,284
  • I Think the question is about how to raise the (already running) VM with a shortcut. – Jacob Vlijm Nov 03 '14 at 07:40
  • Takkat, this is a wonderful way to avoid the problem I described. Also it saves me time, because 99% of the time I was launching VirtualBox Manager just to start the virtual machine; now I can launch the virtual machine with my Unity launcher icon. Thanks for your assistance! It's great to have such clever people helping solve problems like mine! – rclocher3 Nov 04 '14 at 06:24