
<h2>Blather Configuration Part 1: Desktop Management</h2>

<p>In this episode I show how to start adding more commands, how to use the language updater script, and how to start doing some basic desktop navigation. I'll show you how to open and quit applications, and how to switch from one application to another using your voice. </p>

<p>For information about installing blather for the first time, as well as the startup script that I use, please refer to <a href="https://hackerpublicradio.org/eps.php?id=2039">episode 0 of this series</a>, which has examples and links for this stuff.</p>

<p>To start using the language updater script, you need to move it or copy it from the blather source code directory into your path (e.g. ~/bin/). To add new commands you will have to edit the main command configuration file:</p>

<pre>~/.config/blather/commands.conf</pre>

<p>Commands are configured in a "key: value" pair, where the key is what you wish to say, and the value is the command that will be executed when you say it. We will start out with some very basic ones, but these can be as elaborate as your imagination and scripting skills will allow. You can execute built-in system commands, or you can write your own scripts that will be executed upon the voice command.</p>

<p>Here's an example of a basic desktop application command set:</p>


<pre>OPEN CHROMIUM: chromium &amp;
GO TO CHROMIUM: wmctrl -a "google chrome"
QUIT CHROMIUM: wmctrl -c "google chrome"
</pre>

<p>The first command launches Chromium, the second one will switch focus to Chromium when you are currently in another program, and the third one closes Chromium. This makes use of the command line tool <a href="https://linux.die.net/man/1/wmctrl"><code>wmctrl</code></a>, which is a very handy window management tool. The <code>wmctrl -a</code> command chooses which window to put focus on (or close) based on the window title, which in the commands above is given in quotation marks. There are many options to how <code>wmctrl</code> can find windows and take actions, but for now we will just use this basic option.</p>

<p>Once you have one command set of this kind working as you like, it's very easy to set up additional command sets for all of the desktop applications you use most often.</p>

<p>Some applications are more difficult to handle than others. For example media players typically change the window title based on which track is playing. This makes it impossible to use the static window title option above, so I resort to a bit of scripting to help it find the right window to put focus on or close: </p>

<pre>OPEN clementine: clementine &amp;
GO TO clementine: rid=$(pgrep clementine -u $(whoami) |head -n 1) &amp;&amp; rwinname=$(wmctrl -lp |grep $rid |sed -e "s/.*$rid * //" | sed -e "s/$(hostname) //") &amp;&amp; wmctrl -a "$rwinname"
</pre>

<p>Opening the music player is easy. Switching to it is something else. To make this work I first find the process ID of the Clementine music player, and then I use the <code>wmctrl</code> list command to list all of the windows that are open and I grep for the process ID that I found in the first part. Then I extract the window name from that command's output and use the result inside quotation marks in the very last command to change Focus to that window. Whew!</p>

<p>One last basic desktop navigation command for this episode. This is one that I use probably more than any other command. What it achieves is the alt + Tab Key stroke, which switches Focus to the previous window. Here's how I do it:</p>

<pre>
BACK FLIP: xdotool key alt+Tab
</pre>

<p>This makes use of the wonderful <code>xdotool</code> package to execute a virtual keystroke. Magic!</p>

<h2>Links</h2>

<ul>
<li><a href="https://hackerpublicradio.org/eps.php?id=2039">Blather setup</a> (first episode in the series)</li>
<li>CLI tools: <a href="https://manpages.ubuntu.com/manpages/trusty/man1/xdotool.1.html">xdotool</a>, <a href="https://linux.die.net/man/1/wmctrl">wmctrl</a></li>

<li>snapshot of my <a href="https://paste.jonkulp.net/sicegamupi.tex" target="_blank">blather commands file</a></li>
</ul>
