Skip to content

Instantly share code, notes, and snippets.

@brad-jones
Created March 25, 2015 02:57
Show Gist options
  • Save brad-jones/00170879c0cebf524697 to your computer and use it in GitHub Desktop.
Save brad-jones/00170879c0cebf524697 to your computer and use it in GitHub Desktop.
Automating Linux Desktop with wmctrl
#!/bin/sh
# WMCTRL Desktop Automation script
# ================================
# Author: Brad Jones <[email protected]>
#
# I assume you know what wmctrl is, if not not see:
# https://sites.google.com/site/tstyblo/wmctrl/
#
# I also assume you actually read the documentation.
# I have tested this on Fedora 21 running cinnamon and it works great.
# By using gtk-launch we open the app with any special options defined in the desktop file
# and we don't have to worry about the app blocking our script. By the way this is opening an instance of Gmail.
gtk-launch chrome-pjkljhegncpnkpknbcohdijeoejaedia-Default.desktop /home/brad/.local/share/applications
# Wait for the window to appear, the first time we open Chrome it takes a tiny bit longer for the window to appear.
# You may need to adjust these sleep times to match the performamce of your system.
sleep 2
# We make an assumption that the last window to open is the window
# we want to work with, lets grab it's ID from the list of open windows.
window_id=$(wmctrl -l | tail -1 | cut -f1 -d" ")
# Assign it to our first workspace. Workspaces ids are 0 based.
wmctrl -i -r "$window_id" -t 0
# I have dual monitors and want some windows on the left or right screen.
# To make it appear on the right we would just offset the X Position by the resolution of the first monitor.
# In this case we want Gmail on the left screen so we 0 everything out. We don;t try and resize the window
# here, we use the next command to do that hence the -1 values.
wmctrl -i -r "$window_id" -e 0,0,0,-1,-1
# Maximise the window
wmctrl -i -r "$window_id" -b add,maximized_vert,maximized_horz
# Im lazy and have not bothered creating a function here we simply rinse and repeat.
# Open Google Music in the first workspace on the right screen.
gtk-launch chrome-https___play.google.com_music_listen.desktop /home/brad/.local/share/applications
sleep 1
window_id=$(wmctrl -l | tail -1 | cut -f1 -d" ")
wmctrl -i -r "$window_id" -t 0
wmctrl -i -r "$window_id" -e 0,1920,0,-1,-1
wmctrl -i -r "$window_id" -b add,maximized_vert,maximized_horz
# Open terminal on workspace 1 on left screen
gtk-launch gnome-terminal.desktop /usr/share/applications
sleep 1
window_id=$(wmctrl -l | tail -1 | cut -f1 -d" ")
wmctrl -i -r "$window_id" -t 1
wmctrl -i -r "$window_id" -e 0,0,0,-1,-1
wmctrl -i -r "$window_id" -b add,maximized_vert,maximized_horz
# Open brackets on workspace 1 on right screen
gtk-launch brackets.desktop /opt/brackets
sleep 1
window_id=$(wmctrl -l | tail -1 | cut -f1 -d" ")
wmctrl -i -r "$window_id" -t 1
wmctrl -i -r "$window_id" -e 0,1920,0,-1,-1
wmctrl -i -r "$window_id" -b add,maximized_vert,maximized_horz
# I then created a new Startup Application entry in Cinnamon
# So when I login my intial environment is there are waiting.
# It's kind of cool watching everything startup actually :)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment