Help - Search - Members - Calendar
Full Version: Switching desktop with a script
MandrivaUsers.org > Advanced Topics > Command Line, Kernel and Programming
satelliteuser083
No luck with searches on this one huh.gif . Is it possible to switch desktop with a script (i.e from current to e.g. desktop Nr 4); if so, how? Thanks.


[moved from Software by spinynorman]
{BBI}Nexus{BBI}
You should be able to do it with DCOP calls. A quick explaination/demo of how DCOP works is here: http://www.ibm.com/developerworks/linux/library/l-dcop/
iphitus
There's an app called wmctrl that might do this.
satelliteuser083
Have tried both of your suggestions and each does the trick. Many thanks. 2thumbsup.gif
Now, with that problem solved, I have just a niggle; when I start an app from a script - e.g. gnumeric - a series of messages appears (errors?) in the konsole running the script, which I would like to suppress. I've tried using '>' but bash ignores that. Any ideas? Thanks.
{BBI}Nexus{BBI}
QUOTE (satelliteuser083 @ Jul 28 2008, 04:28 PM) *
Have tried both of your suggestions and each does the trick. Many thanks. 2thumbsup.gif
Now, with that problem solved, I have just a niggle; when I start an app from a script - e.g. gnumeric - a series of messages appears (errors?) in the konsole running the script, which I would like to suppress. I've tried using '>' but bash ignores that. Any ideas? Thanks.
Try using the &.
satelliteuser083
Was a bit confused, nexus, 'cos substituting '>' with '&' didn't work. However, I found the following site, where all became clear (at least, the bit about stderr did cool.gif )
http://www.cpqlinux.com/redirect.html
Now, using 'gnumeric filename 2>err.txt', there's no error-output. Thanks. 2thumbsup.gif
{BBI}Nexus{BBI}
QUOTE (satelliteuser083 @ Jul 28 2008, 07:15 PM) *
Was a bit confused, nexus, 'cos substituting '>' with '&' didn't work. However, I found the following site, where all became clear (at least, the bit about stderr did cool.gif )
http://www.cpqlinux.com/redirect.html
Now, using 'gnumeric filename 2>err.txt', there's no error-output. Thanks. 2thumbsup.gif
I just thought backgrounding it would surpress any console output. Glad you've got it sorted. smile.gif If you don't need the err.txt file would it not be better to pipe the output to /dev/null?
satelliteuser083
Yes, you're correct, of course; '2>/dev/null' works well. Thanks. biggrin.gif

And, following on with this topic, I've come up against another problem. I want to start two apps in different workspaces but the workspace-selection is completed much faster than the app start-up, the result being that they both end up in the same (second) workspace. My script looks like this:
CODE
wmctrl -s2
gnumeric filename1 &
wmctrl -s3
kpdf filename2 &

Does anyone know of a way of delaying the execution of 'wmctrl -s3' until gnumeric has completed start-up (or any other way of ensuring that the script acts as desired)? Thanks.
satelliteuser083
When I perform the following command in a konsole, it works:
CODE
[lawrence@localhost ~]$ dcop kwin KWinInterface currentDesktop
4

The same thing in a script, in an attempt to assign this value to a variable
CODE
curws=dcop kwin KWinInterface currentDesktop

fails, with the message
CODE
kwin: Unexpected argument 'KWinInterface'
.
I assume that this is a basic script-syntax problem, but I can't see it. Can anyone help me out? Thanks.
pmpatrick
QUOTE
Does anyone know of a way of delaying the execution of 'wmctrl -s3' until gnumeric has completed start-up (or any other way of ensuring that the script acts as desired)? Thanks.


You can use wait or sleep in your script:

http://www.linux.com/articles/113976
spinynorman
QUOTE
I assume that this is a basic script-syntax problem, but I can't see it. Can anyone help me out? Thanks.


Found this on Google, which would suggest that this might work:

CODE
curws=$(dcop kwin KWinInterface currentDesktop)
satelliteuser083
Thanks, spinynorman, that worked well; just knew it was syntax. wink.gif

And, pmpatrick, 'wait' doesn't do quite what I want; I think that it does wait, but right up until gnumeric is terminated and not (as I want) until the latter is up and running, i.e ready to accept input. I'm now playing with 'sleep'; doesn't work just yet but will report when/if it does.

Many thanks to you both. 2thumbsup.gif 2thumbsup.gif
Steve Scrimpshire
I'm not at my linux box at the moment, but you might try something similar to this:
CODE
wmctrl -s2
gnumeric filename1 &
while [ $(wmctrl -l | grep gnumeric) -eq '' ]
do
sleep
done
wmctrl -s3
kpdf filename2 &
javaguy
I can't find wmctrl. I also can't find it with urpmi, and I can't find it with the package installer in MCC.

theYinYeti
It is indeed not available in urpmi, but it is really easy and fast to compile. I suggest you install "checkinstall" first, then install wmctl by replacing the "make install" command with the "checkinstall" command: this will auto-create a RPM for you smile.gif

Yves.
satelliteuser083
I still have a snag, I'm afraid, Steve. The "while" line produces in the following error-message:
CODE
line 14: [: -eq: unary operator expected

The "(wmctrl -l | grep gnumeric)" part works OK in a konsole, so there seems to be a syntax-problem elsewhere. I've tried a few changes to the line (using '(' instead of '[', for example) but I'm absolutely useless at de-bugging bash-syntax, so I'll have to ask you to look at it for me, please.
Thanks.
Steve Scrimpshire
When I get home today, I will check the syntax. It may be that you left off a space or I just missed a little something.
satelliteuser083
Well, how about this , Steve? I noticed that the "(wmctrl -l | grep gnumeric)" command didn't work completely correctly in a konsole, so instead of
CODE
gnumeric filename1 &
while [ $(wmctrl -l | grep gnumeric) -eq '' ]
do
sleep
done

I tried
CODE
gnumeric filename1 &
while [ "$(wmctrl -l | grep Gnumeric)"  = "" ]
do
sleep 2
done

and now it works.
Bash didn't like 'sleep' without an argument, so that was easy; also, strangely enough, gnumeric is actually called Gnumeric (with a capital G; you have to use the exact name in the title-bar), which is why the 'while' line jammed up. I'm not sure if I've really solved the syntax-prob, or if it's just sheer coincidence, but I don't much care. Until the next time, that is cool.gif . What's your opinion?
Steve Scrimpshire
Possibly...it could be your use of double quotes instead of single quotes, using the = instead of -eq, etc.

Sorry, I forgot to look yesterday. Not sure you need the "" around the "$(wmctrl...". I think the case problem and the = sign did the trick...you might, though. What we really should do is grep -i gnumeric which makes the grep case-insensitive, in case they change it. Then I think you're good. Let me know if you do need the "" around "$(wmctrl..." and let me know if you need double instead of single quotes and all that. I guess my bash is rusty.
satelliteuser083
Yes, Steve, the -i with grep is the answer to the name problem. cool.gif And, I assume that the "s are necessary, because the script failed without them.
Anyway, here is my finished script; hope it helps someone out there (please excuse the comments if they are too obvious, but they won't be to me in, say, 6 months unsure.gif )
CODE
#!/bin/sh
#
# the following 4 variables are necessary because:
# unfortunately dcop Desktop-Nrs go from 1 to n for Desktops 1 to n, whereas
# wmctrl uses values 0 to n-1 for Desktops 1 to n; therefore, to get the correct
# value (for wmctrl) it is necessary to use a value exactly 1 LESS than that
# used by dcop
#.
dcop_gnu_WS=5
wmctrl_gnu_WS=" 4 "
dcop_kpdf_WS=6
wmctrl_kpdf_WS=" 5 "
error_flag=0
#
# First, check if either of the two programs is already running IN THE INTENDED WS; if
# either is, this script will fail, so it simply terminates immediately.
#
# So, for example, test (using 'wmctrl -l') whether or not gnumeric is running IN WS 5;
# if it is, set the error-flag
if [ "$(wmctrl -l | grep -i gnumeric)"  != "" ] &&
   [ "$(wmctrl -l | grep -i $wmctrl_gnu_WS)"  != "" ]; then
  echo "error, gnumeric already running in WS $dcop_gnu_WS, please remove gnumeric from that WS" &&
  error_flag=1;
fi
if [ "$(wmctrl -l | grep -i kpdf)"  != "" ] &&
   [ "$(wmctrl -l | grep -i $wmctrl_kpdf_WS)"  != "" ]; then
  echo "error, kpdf already running in WS $dcop_kpdf_WS, please remove kpdf from that WS" &&
  error_flag=1;
fi
#echo "error_flag="$error_flag
if [ "$error_flag" = "1" ]; then
# either gnumeric or kpdf is running (or both), error; terminate.
echo "terminating... goodbye :(";
else
#
# run the following commands (i.e. start gnumeric and kpdf), using '(' bla bla bla ...')'
# start with '(' on a new line, finish with ');', also on its own new line.
#
(
curws=$(dcop kwin KWinInterface currentDesktop)
dcop kwin KWinInterface setCurrentDesktop $dcop_kpdf_WS
# start kpdf as a background-task (the terminating '&'), then wait until it is ready
# to accept input
kpdf output.pdf &
while [ "$(wmctrl -l | grep -i kpdf)"  = "" ]
do
sleep 2
done
echo "kpdf started in ws"$(dcop kwin KWinInterface currentDesktop)
#
echo "kpdf started, starting gnumeric..."
dcop kwin KWinInterface setCurrentDesktop $dcop_gnu_WS
echo "starting gnumeric..."
# start gnumeric as a background-task (the terminating '&'), then wait until it is ready
# to accept input
gnumericfilename1.xls 2>/dev/null &
while [ "$(wmctrl -l | grep -i gnumeric)"  = "" ]
do
sleep 2
done
echo "gnumeric started in ws"$(dcop kwin KWinInterface currentDesktop)
#
echo "gnumeric started, returning to original desktop..."
dcop kwin KWinInterface setCurrentDesktop $curws
);
fi


Many thanks 2thumbsup.gif 2thumbsup.gif
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Invision Power Board © 2001-2008 Invision Power Services, Inc.