Wednesday, April 21, 2010

Switch Primary Monitor in Ubuntu 10.4

The default Monitor Preferences dialog, while sweet, does not allow you to make your secondary monitor your primary. This is a problem if you want the top and bottom Ubuntu Panels displayed on the secondary monitor. Thankfully I found this sweet little script.


#!/bin/sh
#
# Change Primary Monitor for Gnome
# ver 1.0
#
# Copyright (c) 2010 michal@post.pl
#
# This file is free software. You can redistribute it
# and/or modify it under the terms of the GNU
# General Public License (GPL) as published by
# the Free Software Foundation, in version 3.
# It works for me. I hope it works for you as well.
# NO WARRANTY of any kind.
#


# get list of top-level gnome panels
getTopPanels() {
gconftool-2 --all-dirs /apps/panel/toplevels
}

# get monitor number for this panel
getMonitor() {
local PANEL=$1
gconftool-2 --get $PANEL/monitor
}

# set monitor to display on for given top-level panel
setMonitor() {
local PANEL=$1
local NEW=$2
gconftool-2 --set --type int $PANEL/monitor $NEW
}

# return number of connected monitors
getConnectedMonitors() {
xrandr --query | grep -c '^.* connected'
}

# compute next monitor
nextMonitor() {
# number of monitors
local CURRENT=$1
local MONITORS=$2
awk 'BEGIN{ print ('$CURRENT' + 1) % '$MONITORS'; }'
}

# logging finction
log() {
echo $@ 1>&2
}

# main logic below #############

MONITORS=`getConnectedMonitors`
log "Detected $MONITORS connected monitors"

getTopPanels | while read PANEL
do
MONITOR=`getMonitor $PANEL`
NEW=`nextMonitor $MONITOR $MONITORS`
log "Panel $PANEL is displayed on $MONITOR. Switching to monitor $NEW."
setMonitor $PANEL $NEW
done

13 comments:

NicolaSigel0508 said...

how do u do?................................................................

Liviu said...

U saved my life with this script:)
May God Bless you :)

Subiet said...

Brilliant!

Matt said...

Thank you very much! This script solved my problems. Running ubuntu 10.04.

Dmitrii said...

that was so cool!

Whynot said...

Thanks so much - super slick :)

tina said...

Thanks!! This worked like a charm. I put this in a .sh file and ran thru Terminal.

Shane said...

YOU ARE THE MAN!!!

This script worked like a charm!

Eric Brand said...

Awesome find! This script is great. Thanks for posting this, you seriously saved me a lot of time. :)

Jonathan said...

Lovely!

doom said...

Thanks it works!

Márton Miklós said...

Great script thanks!

Chang (ช้าง) said...

Thanks