LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Starting GUI on specific workspace from CLI or shell script (https://www.linuxquestions.org/questions/linux-newbie-8/starting-gui-on-specific-workspace-from-cli-or-shell-script-882107/)

PClOStinspace 05-22-2011 11:10 AM

Starting GUI on specific workspace from CLI or shell script
 
Is it possible to launch an app via CLI or a shell script and tell it to open on a specific workspace??

I am trying to write a script that will launch several apps after boot/login and place them each on different workspaces.

The script looks like:-
Code:

#!/bin/sh

sleep 10 && guake &
nautilus &
transmission-gtk &
firefox &
opera &
banshee &
conky -c /home/gregg/.conky/conkyForecast.imagebasedtemplate.conkyrc &
conky -c /home/gregg/.conky/conkyrc_grey
exit

so far.

It seems to be doing what I want, if I run:-
Code:

$ bash -x ./startuptasks.sh
the output looks like:-
Code:

+ conky -c /home/gregg/.conky/conkyrc_grey
+ firefox
+ opera
+ nautilus
+ banshee
+ conky -c /home/gregg/.conky/conkyForecast.imagebasedtemplate.conkyrc
+ transmission-gtk
+ sleep 10
Conky: /home/gregg/.conky/conkyrc_grey: 26: config file error
Conky: /home/gregg/.conky/conkyForecast.imagebasedtemplate.conkyrc: 64: config file error
Conky: desktop window (e00089) is subwindow of root window (15d)
Conky: window type - normal
Conky: drawing to created window (0x2000001)
Conky: drawing to double buffer
Conky: desktop window (e00089) is subwindow of root window (15d)
Conky: window type - normal
Conky: drawing to created window (0x3c00001)
Conky: drawing to double buffer
[Info  17:14:56.375] Running Banshee 2.0.0: [Ubuntu Natty (development branch) (linux-gnu, i686) @ 2011-04-18 16:21:33 UTC]

(firefox-bin:14145): LIBDBUSMENU-GTK-CRITICAL **: dbusmenu_menuitem_property_set_shortcut: assertion `gtk_accelerator_valid(key, modifier)' failed
+ guake
[Warn  17:15:03.991] Caught an exception - System.ApplicationException: Invalid frame dimensions (in `Hyena.Gui')
  at Hyena.Widgets.AnimatedImage.ExtractFrames () [0x00000] in <filename unknown>:0
  at Hyena.Widgets.AnimatedImage.Load () [0x00000] in <filename unknown>:0
  at Banshee.Gui.Widgets.TaskStatusIcon..ctor () [0x00000] in <filename unknown>:0
[Info  17:15:05.798] Updating web proxy from GConf
[Info  17:15:06.285] All services are started 7.956078
[Info  17:15:20.219] nereid Client Started
[Info  17:15:20.351] GStreamer version 0.10.32.0, gapless: True, replaygain: True
[Warn  17:17:49.238] Forcefully breaking out of RCS loop b/c change in total_width less than 1.0
[Warn  17:17:52.819] Forcefully breaking out of RCS loop b/c change in total_width less than 1.0

This seems to indicate some errors and it looks like things are happening in a different order to my script, any clues on what they are and how to fix them??

macemoneta 05-23-2011 06:00 PM

The answer is yes, but it's not as simple as you might imagine. In a Gnome2 or Gnome3 fallback environment, you can easily switch to specific workspaces.

I use this script, called 'desktop' to select the workspace:

Code:

#!/bin/bash
#-------------------------------------------------------------------------------
#
# desktop
#
# Author:  Mace Moneta, mmoneta@optonline.net
# Version:  1.1
# Created:  01/13/2010
# Modified: 10/04/2010
#
# Description: Switch to the requested desktop, in metacity or compiz
#
#-------------------------------------------------------------------------------

#-------------------------------------------------------------------------------
#      ************************** Variables **************************
#-------------------------------------------------------------------------------
# Desktop number passed?
dt=$1
if [ "$dt" == "" ]
then
  echo "Error: missing desktop number"
  exit
fi

#-------------------------------------------------------------------------------
#      ************************* Subroutines *************************
#-------------------------------------------------------------------------------
function dsktop () {
  if [ $COMPIZ -eq 1 ]
  then
      # The target desktop number (begins with 0)
      TVPN=$(( $1 % ${NF} ))
      # The X coordinate of the target viewport
      TVPX=$(( ${TVPN} * ${WW} ))
      # Change to the target viewport
      /usr/bin/wmctrl -o ${TVPX},0
  else
      /usr/bin/wmctrl -s $1
  fi
}

#-------------------------------------------------------------------------------
#      ************************* Mainline ****************************
#-------------------------------------------------------------------------------

# Disable globbing
set -f noglob

# Is compiz running?
COMPIZ=0
window_manager="`/usr/bin/wmctrl -m | /bin/grep -i compiz`"
if [ "$window_manager" != "" ]
then
  COMPIZ=1
fi

# The information about the desktop
INFO=$(/usr/bin/wmctrl -d | /bin/grep "\*")

# The width of the desktop
DW=$(echo "${INFO}" | /bin/awk '{sub(/x[0-9]+/, "", $4); print $4}')

# The width of the workarea
WW=$(echo "${INFO}" | /bin/awk '{sub(/x[0-9]+/, "", $9); print $9}')

# The number of faces on the cube
NF=$(($DW/$WW))

dsktop $dt
/bin/sleep 2
exit

You call it with 'desktop 0' to switch to the first desktop, 'desktop 1' for the second, etc.

You also have the issue of synchronizing applications launched as background tasks. For example, Firefox can take a long time to launch. As a result, you need to wait for the window to actually appear. For example:

Code:

/usr/bin/firefox "$@" &
FFPID=$!
FFWINDOW=`/usr/bin/wmctrl -l |/bin/grep "Mozilla Firefox" | /usr/bin/awk '{print $1}'`
COUNT=0
while [ "$FFWINDOW" == "" ]; do
  /bin/sleep .1
  FFWINDOW=`/usr/bin/wmctrl -l |/bin/grep "Mozilla Firefox" | /usr/bin/awk '{print $1}'`
  if [ $((COUNT++)) -ge 100 ]; then exit ; fi
done

On a system with gnome-shell with its dynamic workspaces, all you can do it switch to the last (free) workspace, since you can't preallocate them. You can do this with, for example:

Code:

/usr/bin/wmctrl -s `/usr/bin/wmctrl -d | /usr/bin/tail -n1 | /bin/awk '{print $1}'`
/bin/sleep 1


PClOStinspace 05-23-2011 08:06 PM

I am using Unity on Ubuntu 11.04, so would either of the above work???

macemoneta 05-23-2011 08:29 PM

Sorry, I haven't tried Ubuntu Unity. There's no harm in trying them out. If Unity gives you preallocated workspaces, try the 'desktop' script. If it dynamically allocates them, try the wmctrl. If neither work, then Unity isn't a EWMH (Extended Window Manager Hints) compatible X Window Manager.

PClOStinspace 05-23-2011 08:39 PM

OK, too tired to try now but will have a go another day and see what happens.

I'll try the gnome shell script first as I believe Unity works in a similar way(?) Do i just insert that bit of code before/after the line that launches the app in my script??

macemoneta 05-23-2011 08:50 PM

Yes, if you have preallocated workspaces, you can do something like:

Code:

# application1 on first workspace
desktop 0
application1 &

# application2 on second workspace
desktop 1
application2 &

...

If it ends up switching workspaces before the application window appears, you'll need to add code similar to the Firefox example to wait for the window to appear.


All times are GMT -5. The time now is 07:13 AM.