[SOLVED]xdotool, minimize window by name(not by name, but solved)

About writing shell scripts and making the most of your shell
Forum rules
Topics in this forum are automatically closed 6 months after creation.
Post Reply
rickNS
Level 9
Level 9
Posts: 2981
Joined: Tue Jan 25, 2011 11:59 pm

[SOLVED]xdotool, minimize window by name(not by name, but solved)

Post by rickNS »

Seems I tried every combination I can think of and I can not get this to work.

If I use the window ID number it works, trouble is numbers keep changing, so not much good in a script.
eg.
xdotool windowminimize 0x03600008
works, but
xdotool windowminimize Calculator
does not,
nor does,
xdotool windowminimize "Calculator"
xdotool search --classname Calculator windowminimize
xdotool search --name "Calculator" windowminimize (this one produce no output error, so is syntax OK ?)

And I have googled for a good while going in circles it seems.
This seemed pretty close,
https://askubuntu.com/questions/703628/ ... m-terminal
saying to minimize a window, do,

xdotool windowminimize <window_id>

and window_id can be substituted with, $(xdotool getactivewindow)
That did not work either.

First answer here https://askubuntu.com/questions/4876/ca ... mmand-line 42 votes, does not work.

Anyway is there a way to make it work with the window name ?
And BTW today is the first time I "played" with xdotool.

EDIT, PS, this is not even for myself, just looking for an easy way to solve this thread viewtopic.php?t=409441
Last edited by rickNS on Mon Dec 11, 2023 11:23 am, edited 1 time in total.
Mint 20.0, and 21.0 MATE on Thinkpads, 3 X T420, T450, T470, and X200
Coffeeee
Level 2
Level 2
Posts: 98
Joined: Sun Mar 13, 2022 8:30 am
Location: virgo supercluster, milky way galaxy, solar system, earth

Re: xdotool, minimize window by name

Post by Coffeeee »

I think I remember having problems with xdotool after distro update. I used wmctrl instead for window control.

It's all a vague memory though. I could be wrong.

this should do it

Code: Select all

sleep 1 && wmctrl -r "windowname" -b toggle,hidden & RunSomeProgram
sleep 1 is to let the program load first. you could replace it with a condition

Code: Select all

while ! [[ "$(wmctrl -l)" =~ "windowname" ]] 
do
    sleep 1
done &&
wmctrl -r "windowname" -b toggle,hidden &
RunSomeProgram
you can change sleep 1 to less to reduce lag but it will eventually increase cpu. Try sleep 0.3
rickNS
Level 9
Level 9
Posts: 2981
Joined: Tue Jan 25, 2011 11:59 pm

Re: xdotool, minimize window by name

Post by rickNS »

Coffeeee wrote: Mon Dec 11, 2023 7:03 am
this should do it

Code: Select all

sleep 1 && wmctrl -r "windowname" -b toggle,hidden & RunSomeProgram
Did not seem to work.
Even with Calculator (as test program) already open this bit,
wmctrl -r "Calculator" -b toggle,hidden
did not work, also tried "gnome-calculator", and the "numerical id". No difference.
Mint 20.0, and 21.0 MATE on Thinkpads, 3 X T420, T450, T470, and X200
User avatar
Koentje
Level 7
Level 7
Posts: 1581
Joined: Tue Jan 04, 2022 6:23 pm
Location: Netherlands

Re: xdotool, minimize window by name

Post by Koentje »

If minimizing the window with ID works, why not get the window ID with wmctrl and minimize it with xdotool? So do a list with wmctrl, then grep the window name you want and awk the window ID.
Rekenmachine is dutch for Calculator, so change it to whatever you want to minimize.

Code: Select all

xdotool windowminimize $(wmctrl -l | grep Rekenmachine | awk '{print $1}')
rickNS
Level 9
Level 9
Posts: 2981
Joined: Tue Jan 25, 2011 11:59 pm

Re: xdotool, minimize window by name

Post by rickNS »

Koentje wrote: Mon Dec 11, 2023 10:08 am If minimizing the window with ID works, why not get the window ID with wmctrl and minimize it with xdotool? So do a list with wmctrl, then grep the window name you want and awk the window ID.
Rekenmachine is dutch for Calculator, so change it to whatever you want to minimize.

Code: Select all

xdotool windowminimize $(wmctrl -l | grep Rekenmachine | awk '{print $1}')
Excellent, thanks.
That works, and your script solves that other thread nicely too.

I was trying to use cut to get the id into a varible, wmctrl -l | grep Calculator | cut -b 1-10, but I kept getting errors, and the terminal was left open, disown did not seem to help. I am not a coder by any stretch but I tried haha.

Thanks, very much.
Marking solved
Mint 20.0, and 21.0 MATE on Thinkpads, 3 X T420, T450, T470, and X200
User avatar
Koentje
Level 7
Level 7
Posts: 1581
Joined: Tue Jan 04, 2022 6:23 pm
Location: Netherlands

Re: [SOLVED]xdotool, minimize window by name(not by name, but solved)

Post by Koentje »

Thanks!

I don't use cut too often.. awk for this is way better IMO.. it doesn't depend on length of characters you want to grab. $1 grabs anything until the first space. $2 grabs anything from the first space to the second space and so on. If you use the -F option you can specify another separator.

Say this line is in a string called line and | is the seperator
123|456|789

And you want the number 456 out of it
echo "$line" | awk -F'|' '{print $2}'

This wil get you 123789
echo "$line" | awk -F'|' '{print $1 $3}'

If you want a space between them
echo "$line" | awk -F'|' '{print $1" "$3}'
User avatar
xenopeek
Level 25
Level 25
Posts: 29615
Joined: Wed Jul 06, 2011 3:58 am

Re: [SOLVED]xdotool, minimize window by name(not by name, but solved)

Post by xenopeek »

Koentje wrote: Mon Dec 11, 2023 12:27 pm I don't use cut too often.. awk for this is way better IMO.. it doesn't depend on length of characters you want to grab. $1 grabs anything until the first space. $2 grabs anything from the first space to the second space and so on. If you use the -F option you can specify another separator.
If you have a separator character, cut does the job fine and is more readable IMHO. Aside if you want to print multiple fields without an output delimiter it's a bit noisy:

Code: Select all

$ echo "$line" | cut -d'|' -f2
456
$ echo "$line" | cut -d'|' -f1,3
123|789
$ echo "$line" | cut -d'|' -f1,3 --output-delimiter=''
123789
Image
rickNS
Level 9
Level 9
Posts: 2981
Joined: Tue Jan 25, 2011 11:59 pm

Re: [SOLVED]xdotool, minimize window by name(not by name, but solved)

Post by rickNS »

xenopeek wrote: Mon Dec 11, 2023 12:59 pm
If you have a separator character, cut does the job fine and is more readable IMHO. Aside if you want to print multiple fields without an output delimiter it's a bit noisy:

Code: Select all

$ echo "$line" | cut -d'|' -f2
456
$ echo "$line" | cut -d'|' -f1,3
123|789
$ echo "$line" | cut -d'|' -f1,3 --output-delimiter=''
123789
So not to beat a dead horse, I have another question, if either of you can, or would be so kind to explain this.
OK so I can cut the window ID like this,

Code: Select all

wmctrl -l | grep Calculator | cut -d ' ' -f1
0x03600008
"seems" the same, results the same as,

Code: Select all

wmctrl -l | grep Calculator | awk '{print $1}'
0x03600008
However when combined with the xdotool command I get these errors,

Code: Select all

xdotool windowminimize $(wmctrl -l | grep Calculator | cut -d ' ' f1)
cut: you must specify a list of bytes, characters, or fields
Try 'cut --help' for more information.
There are no windows in the stack
Invalid window '%1'
Usage: windowminimize [options] [window=%1]
--sync    - only exit once the window has minimized (is not visible)
If no window is given, %1 is used. See WINDOW STACK in xdotool(1)
And the window does NOT minimize.

Unlike the command with awk by
Koentje wrote: Mon Dec 11, 2023 12:27 pm awk for this is way better IMO
Which works without error, and the window minimizes.
Mint 20.0, and 21.0 MATE on Thinkpads, 3 X T420, T450, T470, and X200
User avatar
Koentje
Level 7
Level 7
Posts: 1581
Joined: Tue Jan 04, 2022 6:23 pm
Location: Netherlands

Re: [SOLVED]xdotool, minimize window by name(not by name, but solved)

Post by Koentje »

If i run the script like this

Code: Select all

#!/bin/bash

gnome-calculator &

while [ "$(wmctrl -l | grep Calculator)" = "" ]; do
  echo "Waiting..." > /dev/null
done

xdotool windowminimize $(wmctrl -l | grep Calculator | cut -d' ' -f1)
...it works!
User avatar
xenopeek
Level 25
Level 25
Posts: 29615
Joined: Wed Jul 06, 2011 3:58 am

Re: [SOLVED]xdotool, minimize window by name(not by name, but solved)

Post by xenopeek »

rickNS wrote: Mon Dec 11, 2023 7:27 pm xdotool windowminimize $(wmctrl -l | grep Calculator | cut -d ' ' f1)
cut: you must specify a list of bytes, characters, or fields
You have cut -d ' ' f1 instead of cut -d ' ' -f1
Image
Post Reply

Return to “Scripts & Bash”