LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Desktop
User Name
Password
Linux - Desktop This forum is for the discussion of all Linux Software used in a desktop context.

Notices

Reply
 
Search this Thread
Old 11-26-2013, 12:20 PM   #1
SaintDanBert
Senior Member
 
Registered: Jan 2009
Location: Austin, TX
Distribution: Mint-15 with Cinnamon & KDE
Posts: 1,368
Blog Entries: 3

Rep: Reputation: 86
seeking help using 'wmctrl' with Mint and Cinnamon


I want to use wmctrl in a script at login time. Using the 'desktop-switcher' and four desktops, I want to launch some specific applications and have them appear on a specific desktop. I see how to do all of this from a command line, but I'm dashed if I can sort out how to script it at login time without a hundred lines of output parsing.

Does anyone do this sort of thing? Can and will you share your tips and tricks (blush) and scripts with LQ and me?

The man-page describes wmctrl like this:
wmctrl is a command that can be used to interact with an X Window manager that is compatible with the EWMH/NetWM specification. wmctrl can query the window manager for information, and it can request that certain window management actions be taken.
...
The man-page also contains some clear examples of specific uses but they seem more "interactive" than "scripted." There are command options that will list desktops and windows but I'll need to parse this output for the details of the things I want and then find the identifier numbers to use to make the changes and settings that I want to modify.

Thanks in advance,
~~~ 0;-Dan
 
Old 12-05-2013, 10:55 AM   #2
SaintDanBert
Senior Member
 
Registered: Jan 2009
Location: Austin, TX
Distribution: Mint-15 with Cinnamon & KDE
Posts: 1,368
Blog Entries: 3

Original Poster
Rep: Reputation: 86
Can anyone tell me why this posting has shown as a NEW (bold title in "Subscribed Threads") for almost a week in spite of no replies?

BUMP!
 
Old 12-12-2013, 12:48 PM   #3
ondoho
Senior Member
 
Registered: Dec 2013
Distribution: arch linux / crunchbang
Posts: 1,216

Rep: Reputation: 170Reputation: 170
if i understand correctly, you have some applications autostarting at login and you want them to appear on a particular workspace?
why not put it staright into the autostart script?
or, one simple hack would be to have, e.g., an executable $HOME/bin/chromium:
Code:
/usr/bin/chromium $@ &
get WINID of chromium.
wmctrl do things WINID
this is pseudocode, of course.
a quick glance at man wmctrl shows me that it shows pids also.
so:
Code:
/usr/bin/chromium $@ &
pid=$!
wmctrl do things $pid
in any case, i think wmctrl's output has to be parsed and fed back to wmctrl.

if you tell us more precisely what you want, more precise suggestions are possible.

ps: openbox supports this kind of stuff out of the box ;-)

Last edited by ondoho; 12-12-2013 at 12:49 PM.
 
1 members found this post helpful.
Old 12-13-2013, 11:58 AM   #4
SaintDanBert
Senior Member
 
Registered: Jan 2009
Location: Austin, TX
Distribution: Mint-15 with Cinnamon & KDE
Posts: 1,368
Blog Entries: 3

Original Poster
Rep: Reputation: 86
Quote:
Originally Posted by ondoho View Post
if you tell us more precisely what you want, more precise suggestions are possible.
I start a half dozen apps when I login. I have four workspaces (so that I can run the cube eye candy).
  • I open #1 and start a couple of things.
  • I open #2 and start a couple more.
  • ...
  • I repeat for each workspace.
It looks like wmctrl will let me automate this dance. This might be straight forward since I'm not moving things that are already running. Rather, I open a workspace then start things there.
Quote:
Originally Posted by ondoho View Post
if i understand correctly, you have some applications autostarting at login and you want them to appear on a particular workspace?
why not put it staright into the autostart script?
or, one simple hack would be to have, e.g., an executable $HOME/bin/chromium:
Code:
/usr/bin/chromium $@ &
get WINID of chromium.
wmctrl do things WINID
this is pseudocode, of course.
Yes, one launcher-script per application makes great sense so that I get the same details each time I launch that app. Equipped with the app-launcher scripts, I could then make a workspace-launcher script for each workspace calling the app-launchers. This might be a single script that reads a file that contains:
{workspace} {application} {script path}
or similar.

For me, the hard part will be keeping things simple without going over the moon trying to make something "general purpose." I'm a huge fan of small programs that do a few things well using standard interfaces Now where have I heard that before (grin).
Quote:
Originally Posted by ondoho View Post
a quick glance at man wmctrl shows me that it shows pids also.
so:
Code:
/usr/bin/chromium $@ &
pid=$!
wmctrl do things $pid
in any case, i think wmctrl's output has to be parsed and fed back to wmctrl.
It has been forever since I've done output parsing with any complexity.
That is my motivation for seeking something that someone else has already done that I might hack.
Quote:
Originally Posted by ondoho View Post
ps: openbox supports this kind of stuff out of the box ;-)
I'll need to look at openbox to see if that makes sense for me.

Great comments, Thanks.
~~~ 8d;-Dan
 
Old 12-13-2013, 01:42 PM   #5
SaintDanBert
Senior Member
 
Registered: Jan 2009
Location: Austin, TX
Distribution: Mint-15 with Cinnamon & KDE
Posts: 1,368
Blog Entries: 3

Original Poster
Rep: Reputation: 86
I found this article, Automatically Start Linux Desktop Programs. While it does not speak to use of wmctrl, it covers part of the problem space. Specifically, what to start during login.

Armed with the details from this article, one can configure a set of per-application scripts that will run during login processing. Each of these scripts then would use wmctrl to deploy the application onto the chosen workspace. The script might even create the workspace if it did not exist, but that might prove messy.

Following this approach, the choice of which workspace to occupy might be a parameter to the per-application script:
Code:
     ...
     myWkSpace="$1"
     myOpts="$2"       #I know there are better ways
     ...
     setWorkspace $myWkSpace
     ...
     launchApp $myAppCmd $myOps
     ...
where setWorkSpace and launchApp are shell functions that encapsulate the details of workspace manipulation and application launch respectively. The functions also encapsulate error detection, reporting, and recovery.

Thinking out loud,
~~~ 0;-Dan
 
Old 12-13-2013, 03:17 PM   #6
ondoho
Senior Member
 
Registered: Dec 2013
Distribution: arch linux / crunchbang
Posts: 1,216

Rep: Reputation: 170Reputation: 170
hmmm...
do you usually start programs from the menu, dash or whatever that is in cinnamon or kde, and not from the command line?
iirc, in that case the .desktop file in /usr/share/applications is called, which you could edit to your liking, so that it would always open that app in a specific workspace.

i will look into the parsing of wmctrl output later, if you still want it. kinda lazy & tired right now...
 
1 members found this post helpful.
Old 12-16-2013, 12:59 PM   #7
SaintDanBert
Senior Member
 
Registered: Jan 2009
Location: Austin, TX
Distribution: Mint-15 with Cinnamon & KDE
Posts: 1,368
Blog Entries: 3

Original Poster
Rep: Reputation: 86
Quote:
Originally Posted by ondoho View Post
hmmm...
do you usually start programs from the menu, dash or whatever that is in cinnamon or kde, and not from the command line?
iirc, in that case the .desktop file in /usr/share/applications is called, which you could edit to your liking, so that it would always open that app in a specific workspace.
Modifications to the public dot-desktop files means that the workspace assignment code would need to discover if the current user used that specific desktop before doing the app assignment. It would also involve a system wide conventions about how to deploy workspaces.
ASIDE -- While 99.44% of the time only one human uses the workstations. However, I routinely have more than one login for that single human. There is the everyday login, of course, but then I have a login for heavy-coder user and another for web-developer user. I also have logins that activate alternate desktop environments so that I can see what they are like or take advantage of those features. For example, I have a KDE login where I work with Caligra and friends writing eBooks ... while I play with KDE. (My usual desktop is Cinnamon.)
Implementing local, per-user dot-desktop (or whatever) files appears to make a lot of sense.
Code:
     # this is pseudo-code
     if (starting-app-XYZ )
     then
         setWorkspace $myWkSpace
         ...
         launchApp $myAppCmd $myOps
         ...
     fi
resulting in this app ALWAYS running within a given workspace. I need to do more thinking about that general case. For now, I'm interested in the the specific start-at-login case.
Quote:
Originally Posted by ondoho View Post
i will look into the parsing of wmctrl output later, if you still want it. kinda lazy & tired right now...
Years [cough, er, decades] ago, I did all sorts of command piped-to sed or awk and such. There is so much dust and rust on those skills now.
 
Old 12-16-2013, 04:03 PM   #8
ondoho
Senior Member
 
Registered: Dec 2013
Distribution: arch linux / crunchbang
Posts: 1,216

Rep: Reputation: 170Reputation: 170
with wmctrl you can send a window to a particular workspace:
man wmctrl:
Code:
       -r <WIN>
              Specify a target window for an action.
       -t <DESK>
              Move  a window that has been specified with the -r action to the
              desktop <DESK>.

...

       <DESK> A Desktop is always specified by an integer which represents the
              desktop numbers. Desktop numbers start at 0.

       <WIN>  This  argument  specifies  a  window  that  is  the target of an
              action. By default the argument is treated as if were a  string,
              and  windows  are  examined  until one is found with a title the
              contains the specified string  as  a  substring.  The  substring
              matching is done in a case insensitive manner. The -F option may
              be used to force  exact,  case  sensitive  title  matching.  The
              option  -i  may  be  used  to  interpret  the window target as a
              numeric window identity instead of a string.
if you know the title of the window you want to move, it's not so hard.
otherwise you need to get hold of the WINID of the just started app.
i don't know if "echo $WINDOWID" in a shellscript will work.
experiment with xprop.

i think you're really going to have to get your teeth into it now and start unearthing those old skills.
stackoverflow often has useful (bash) solutions, just a web search away.
 
1 members found this post helpful.
Old 12-21-2013, 10:31 AM   #9
SaintDanBert
Senior Member
 
Registered: Jan 2009
Location: Austin, TX
Distribution: Mint-15 with Cinnamon & KDE
Posts: 1,368
Blog Entries: 3

Original Poster
Rep: Reputation: 86
I found another article Switching Monitor Profiles that offers some additional details of my desired automatic desktop+workspace activation.

I realized that my desktop and workspace configuration changed when I connected an external monitor -- usually a projector -- or when I docked my laptop to my 26-inch monitor.

Regards,
~~~ 0;-Dan
 
Old 12-21-2013, 10:38 AM   #10
SaintDanBert
Senior Member
 
Registered: Jan 2009
Location: Austin, TX
Distribution: Mint-15 with Cinnamon & KDE
Posts: 1,368
Blog Entries: 3

Original Poster
Rep: Reputation: 86
Quote:
Originally Posted by ondoho View Post
with wmctrl you can send a window to a particular workspace ...
if u know the title of the window you want to move, it's not so hard.
otherwise you need to get hold of the WINID of the just started app.
i don't know if "echo $WINDOWID" in a shellscript will work.
experiment with xprop.
Your suggestion requires that I identify the window associated with a running application. At log-in, I will be starting applications so my script would need to:
  • choose a WORKSPACE
  • choose an APPLICATION
  • LAUNCH the application
  • ... repeat
Since I'm doing this during log-in, I have a simpler situation.
You correctly describe an approach that would move a running application to a specific workspace. While thinking through your comments, I realized that what I want to do is much easier (blush) and requires far less output parsing.

Thanks,
~~~ 0;-Dan
 
Old 12-21-2013, 12:28 PM   #11
ondoho
Senior Member
 
Registered: Dec 2013
Distribution: arch linux / crunchbang
Posts: 1,216

Rep: Reputation: 170Reputation: 170
yes, but there lies another problem:
(case scenario: openbox as window manager)
when i start an application on a particular desktop, and then switch to another desktop before the application window comes up, the window appears on that new desktop.
so i think your safest choice is getting hold of the WINDOWID variable.
i know a shellscript that uses this from inside a terminal window to position that terminal window - i'll just give you the whole script:
Code:
#!/bin/bash

# icon from http://www.iconfinder.com/icondetails/17547/48/prompt_terminal_icon
# xseticon from http://www.leonerd.org.uk/code/xseticon/
# Solutions from
# http://superuser.com/questions/363614/leave-xterm-open-after-task-is-complete
# http://unix.stackexchange.com/questions/3197/how-to-identify-which-xterm-a-shell-or-process-is-running-in
# http://unix.stackexchange.com/questions/16774/how-to-assign-an-icon-to-a-program-in-openbox

# Remember that you'll need to set up an alias in your .bashrc and edit this script for the right path.

running=$(ps aux | grep "xterm" | grep -v "grep" | grep -c "xterm")

if [ "$running" -gt "7" ];then
	running=0
fi

snark=$(echo $WINDOWID)
xseticon -id $snark $HOME/bin/icons/terminal_prompt$running.png
i'm surprised that the most important keyword "window manager" hasn't been used in this thread yet.
you should really check your (kde's) window manager if that offers a solution. dig deep.
then you wouldn't need wmctrl at all.
 
1 members found this post helpful.
Old 12-21-2013, 03:05 PM   #12
SaintDanBert
Senior Member
 
Registered: Jan 2009
Location: Austin, TX
Distribution: Mint-15 with Cinnamon & KDE
Posts: 1,368
Blog Entries: 3

Original Poster
Rep: Reputation: 86
Quote:
Originally Posted by ondoho View Post
yes, but there lies another problem:
(case scenario: openbox as window manager)
when i start an application on a particular desktop, and then switch to another desktop before the application window comes up, the window appears on that new desktop.
so i think your safest choice is getting hold of the WINDOWID variable.
RIGHT! I clearly overlooked that issue though I've fought it a thousand times..

So, in terms I've described elsewhere, once I select the WORKSPACE and launch the APPLICATION there, I must wait for a completed launch. I can launch everything on a given WORKSPACE, but if I try to move on, those applications will appear on a different WORKSPACE than I intended.

I don't think that there will be a WINDOWID until that application cranks enough to create its master/parent window. Once that window exists, for sure, I need to grab the WINDOWID so it is available for any other manipulations.

There might be some way to force-create a window and force the app to use it rather than wait for the app to crank its internal window-create.

Thanks in advance,
~~~ 0;-Dan
 
Old 12-21-2013, 05:56 PM   #13
ondoho
Senior Member
 
Registered: Dec 2013
Distribution: arch linux / crunchbang
Posts: 1,216

Rep: Reputation: 170Reputation: 170
yes another way to think this is to wait until the application maps a window (and maybe sets some flag somewhere that can be identified), and then send it to a specific desktop. would result in a short flash, i guess.

but this is really window managers task.
unfortunately i know nothing about kde's wm.

fwiw, people have written window managers specifically to achieve sth like that.
 
Old 12-31-2013, 05:33 PM   #14
legion1978
LQ Newbie
 
Registered: Dec 2013
Posts: 1

Rep: Reputation: Disabled
been trying the same thing for some time. i have 2 options now. devilspie (xfce) and wmctrl(cinnamon)

in xfce a script launches devilspie first at login and then every app i need, with some seconds apart from each other.

Code:
#!/bin/bash
devilspie &
sleep 5; someapp&
sleep 10; someotherapp
devilspie moves them, like this

Code:
(if
   (is (application_name) "someapp")
   (begin
      (set_workspace 0)
   )
)
(if
   (is (application_name) "someotherapp")
   (begin
      (set_workspace 3)
   )
)
this works fine in xfce, but not in cinnamon, so i run a script at startup with wtrml

Code:
#!/bin/bash
sleep 5; someapp&
sleep 10; someotherapp&
sleep 1;myappmover.sh
so it check for windows and send them to their intended ws, like this

Code:
#!/bin/bash

check_process() {
  [ "$1" = "" ]  && return 0
  [ `pgrep -n -f $1` ] && return 1 || return 0
}

while [ 1 ]; do
  wmctrl -r "someapp" -t 0
  wmctrl -r "someotherapp" -t 1
  sleep 15
done
is stays idle and runs every 15 secs

dont know if its correct, since i hacked some scripts i found, but it does work. EDIT: that check_process() is because the script originally checks for apps running and then start them if not. not sure if i need that but i left it in.

though my problem now is that wmctrl is checking (and moving) only one instance of the matched window, so if i have 2 chromiums open, it only moves one of them to the respective ws. and since devilspie dont work with cinnamon, it cannot send it upon start. but it is progress

EDIT2: i figure hat i should do is list all windows, identify app or class or id and then send it to its ws. but its beyond me..
EDIT3: i also think cinnamon is able to do this by itself, because when restarting cinnamon (alt+f2 r) it returns every window to its last workspace, so there must be a way. dont know how though.

Last edited by legion1978; 12-31-2013 at 05:58 PM. Reason: adding comments
 
Old 01-01-2014, 06:56 AM   #15
ondoho
Senior Member
 
Registered: Dec 2013
Distribution: arch linux / crunchbang
Posts: 1,216

Rep: Reputation: 170Reputation: 170
ok, let's concentrate on wmctrl.
Quote:
Originally Posted by legion1978 View Post
EDIT2: i figure hat i should do is list all windows, identify app or class or id and then send it to its ws. but its beyond me..
no, it's not!
i just threw something together and it was fun.
i used "man wmctrl", and this and this and this.
Code:
#!/bin/bash

sleep 5; someapp &
pid_someapp=$!
sleep 10; someotherapp &
pid_someotherapp=$!
sleep 1

while [ 1 ]; do
    echo "$(wmctrl -l -p)" | while read winid _ pid _ _
    do
    echo "$winid - $pid"
        case $pid in
            $pidsomeapp)
               wmctrl -r $winid -t 0 ;;
            $pidsomeotherapp)
               wmctrl -r $winid -t 1 ;;
            *)
               continue ;;
        esac
    sleep 15
done


if [ "$pid" == "$pid_someapp" ]
then
else
if [ "$pid" == "$pid_someotherapp" ]
then 
done
i haven't tested this, there might be flaws.
best to try this from a terminal and see what output you get.

Quote:
EDIT3: i also think cinnamon is able to do this by itself, because when restarting cinnamon (alt+f2 r) it returns every window to its last workspace, so there must be a way. dont know how though.
as said before in this thread, you really should check out what your window manager is capable of.
i don't know what wm cinnamon uses.

Last edited by ondoho; 01-01-2014 at 06:59 AM.
 
1 members found this post helpful.
  


Reply

Tags
desktop, switcher, window manager, wmctrl


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
LXer: Linux Mint 16 Will Have Cinnamon 2.0 LXer Syndicated Linux News 0 06-05-2013 04:40 PM
Linux Mint Problems Cinnamon LinWinApp Linux - General 1 02-16-2013 08:31 PM
Mint-Cinnamon menus and panels Chris.Bristol Linux Mint 1 01-11-2013 10:06 PM
LXer: Linux Mint 13 Cinnamon: Not Quite There Yet LXer Syndicated Linux News 0 09-03-2012 01:30 PM


All times are GMT -5. The time now is 05:32 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration