Ubuntu – Configure Unity Dash to forget the last command

unityunity-dash

The problem

When I activate the Unity Dash it remembers the last application, file or folder that I accessed using the Dash. If the previous item was a folder and this time I am looking for an application, the Dash fails to find the application.

How to reproduce

  1. launch the Dash by pressing the 'super' key
  2. type your username to find your home folder (other folder names will do)
  3. press 'enter' to launch Files
  4. launch the Dash again
  5. type 'firefox' or any other application (without first pressing 'escape')

The workaround

Pressing escape immediately after launching the Dash, thus clearing the previous search term.

The question

Can I configure Dash to forget the previous search term and does anyone know if this is the expected behaviour?

Best Answer

Simple solution would be to just right-click on the dash itself and select the specific category you want to search - Dash will be cleared automatically for that.

enter image description here

I've noticed that the re-opened Dash, has the previous text highlighted. Hitting backspace key allows for clearing and carrying out new searches without issues. If only we could automate doing that . . . Well, we can with application called xdotool and a bit of scripting magic.

Install xdotool via apt-get install xdotool and save the following script. Its basic task is to determine whether or not the active window is Dash, and automate hitting BackSpace key to clear Dash. This is meant to run as python script_name.py and to be launched when user logs in by adding this script to Startup Applications.

#!/usr/bin/env python
import dbus,time,subprocess,os

def run_cmd(cmdlist):
    """ utility: reusable function for running external commands """
    new_env = dict(os.environ)
    new_env['LC_ALL'] = 'C'
    try:
        stdout = subprocess.check_output(cmdlist, env=new_env)
    except subprocess.CalledProcessError:
        pass
    else:
        if stdout:
            return stdout


def get_dbus(bus_type, obj, path, interface, method, arg):
    """ utility: executes dbus method on specific interface"""
    if bus_type == "session":
        bus = dbus.SessionBus()
    if bus_type == "system":
        bus = dbus.SystemBus()
    proxy = bus.get_object(obj, path)
    method = proxy.get_dbus_method(method, interface)
    try:
        if arg:
            return method(arg)
        else:
            return method()
    except dbus.exceptions.DBusException:
        return None

def active_is_dash():
    base = ['session','org.ayatana.bamf']
    dbus_call = base + ['/org/ayatana/bamf/matcher', 'org.ayatana.bamf.matcher','ActiveWindow',None]
    active_window = str(get_dbus(*dbus_call))
    dbus_call =  base + [active_window,'org.ayatana.bamf.view','Name',None] 
    active_name = str(get_dbus(*dbus_call))
    if active_name == 'unity-dash': return True


command = 'xdotool key BackSpace'.split()
flag = None
while True:
    time.sleep(0.25)
    if active_is_dash():
        time.sleep(0.25)
        if not flag: run_cmd(command)
        flag = True
    else: flag = False