The Problem
As Linux power users, we're always looking for ways to streamline our workflows. But what happens when your favorite web application doesn't provide an API or CLI? This is the situation with brain.fm, a focus-enhancing music service that runs exclusively in the browser.
No API? No CLI? No problem.
Enter xdotool: The Unsung Hero
xdotool
is a command-line utility that simulates keyboard and mouse
input. It's part of the X11 ecosystem that many Linux distributions
rely on, and it's surprisingly powerful for automation tasks.
# Install xdotool on Debian/Ubuntu sudo apt-get install xdotool
The Solution: A Simple Toggle Script
Let's create a script that can play/pause brain.fm music regardless of which window you're currently using:
#!/bin/bash # Store the currently focused window ID CURRENT_WINDOW=$(xdotool getwindowfocus) # Find the window ID of the browser with brain.fm WINDOW_ID=$(xdotool search --name "brain.fm" | head -1) if [ -z "$WINDOW_ID" ]; then echo "brain.fm window not found" exit 1 fi # Focus the window and send the space key xdotool windowactivate $WINDOW_ID xdotool key space # Return focus to the original window xdotool windowactivate $CURRENT_WINDOW
Save this script as brain-fm-toggle.sh
and make it executable with
chmod +x brain-fm-toggle.sh
.
Binding to a Hotkey
A script is only useful if it's easily accessible. Let's bind it to a keyboard shortcut:
Using xbindkeys
First, install xbindkeys:
sudo apt-get install xbindkeys
Create or edit
~/.xbindkeysrc
:# Play/pause brain.fm with Alt+p "~/path/to/brain-fm-toggle.sh" alt + p
Start (or restart) xbindkeys:
killall xbindkeys 2>/dev/null; xbindkeys
For Window Managers with Built-in Key Binding
If you're using a window manager like i3, add this to your config:
bindsym $mod+p exec ~/path/to/brain-fm-toggle.sh
Why This Matters
This isn't just about brain.fm. The principle applies to any web application.
Conclusion
The Linux command line remains one of the most powerful productivity
tools available. Don't let the lack of official APIs discourage you -
with creative use of utilities like xdotool
and key binding
mechanisms, you can automate nearly anything.
If you liked this post, please consider supporting our Free and Open Source software work – you can sponsor us on Github and Patreon or star our FLOSS repositories.