1

I am a beginner in Unix environment or with desktop application. I have application running and I am able to get its pid using this pgrep <<pid name>>. Can we maximize this this application or makes its window active using shell script?

Thanks in advance!

Vish
  • 832
  • 7
  • 21

1 Answers1

3

wmctrl does just that:

#!/bin/bash

pid=1234

# Note that one PID may have several windows opened, possibly 
# with the same title, so you may have to implement 
# some additional logic in order to choose the correct one.

window_refs=$(wmctrl -p -l | grep " $pid " | egrep -o 0x[0-9a-z]+)
for ref in $window_refs; do
    wmctrl -i -r "$ref" -b "add,maximized_vert,maximized_horz"
done
Vadim Landa
  • 2,784
  • 5
  • 23
  • 33
  • 1
    You should double quote variables to prevent globbing and word splitting. Also consider using `$(..)` instead of legacy backticks. – user3439894 Apr 23 '15 at 13:05
  • It is working fine for the first time. When I am minimizing the window and running the script again, it does not show up. And I am getting this message `Cannot convert argument to number.`. Please explain what does it mean `grep "$pid"`. – Vish Apr 23 '15 at 13:39
  • 1
    @user3439894: seems that $window_refs shouldn't be double-quoted, as then it gets treated as a single string, breaking the loop. Removed the quotes once again. – Vadim Landa Apr 23 '15 at 13:47
  • @Vish: should be OK now, please re-try. The command works by listing all active windows first together with the respective PIDs, then filtering out only relevant ones, and finally creating a list of window references that are actually used for manipulating the windows. – Vadim Landa Apr 23 '15 at 13:47
  • @Vadim Landa, yes some variables shouldn't be double-quoted base on where/how used, as in this case in the `for .. in $..` however `.. -r "$ref"` should be. Best to check code with [ShellCkeck](http://www.shellcheck.net). – user3439894 Apr 23 '15 at 14:04