Need edit of this .py script please! [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
User avatar
Logansfury
Level 6
Level 6
Posts: 1237
Joined: Fri Oct 27, 2023 4:08 pm
Location: Las Vegas NV, USA

Need edit of this .py script please! [SOLVED]

Post by Logansfury »

Hello everyone,

I have acquired the following python3 script that closes all open windows. This was close to but not quite what I wanted. I wanted all open windows MINIMIZED.

Can anyone edit this script or provide a script to do so please?

Code: Select all

import subprocess

# Define a function to close all open windows
def close_all_windows():
    try:
        # Use wmctrl to list all open windows
        windows_list = subprocess.check_output(["wmctrl", "-l"]).decode("utf-8").strip().split("\n")
        # Iterate through the windows and close them one by one
        for window in windows_list:
            window_id = window.split()[0]
            subprocess.run(["wmctrl", "-ic", window_id])
    except subprocess.CalledProcessError as e:
        print("Error:", e)

# Call the function to close all open windows
close_all_windows()
Thank you for reading,

Logan
Last edited by Logansfury on Sat Feb 24, 2024 6:14 pm, edited 1 time in total.
Image <-- Cick for sudo inxi --usb -Fxxxnmprz output, updated hourly!
User avatar
xenopeek
Level 25
Level 25
Posts: 29615
Joined: Wed Jul 06, 2011 3:58 am

Re: Need edit of this bash script please!

Post by xenopeek »

You can throw that whole bunch of code away. wmctrl -k on toggles your desktop manager's "show desktop" function and minimizes all windows
Image
schellasu
Level 3
Level 3
Posts: 140
Joined: Sat Aug 05, 2023 6:41 am

Re: Need edit of this bash script please!

Post by schellasu »

Logansfury wrote: Sat Feb 24, 2024 5:21 pm I have acquired the following python3 script that closes all open windows. This was close to but not quite what I wanted. I wanted all open windows MINIMIZED.
man wmctrl tells, that Option "-c" CLOSES a window, so why you are wondering?
User avatar
AndyMH
Level 21
Level 21
Posts: 13759
Joined: Fri Mar 04, 2016 5:23 pm
Location: Wiltshire

Re: Need edit of this bash script please!

Post by AndyMH »

There is also an icon in the panel that does it for you.
Screenshot from 2024-02-24 21-51-35.png
Screenshot from 2024-02-24 21-51-35.png (191.1 KiB) Viewed 330 times
Thinkcentre M720Q - LM21.3 cinnamon, 4 x T430 - LM21.3 cinnamon, Homebrew desktop i5-8400+GTX1080 Cinnamon 19.0
User avatar
Logansfury
Level 6
Level 6
Posts: 1237
Joined: Fri Oct 27, 2023 4:08 pm
Location: Las Vegas NV, USA

Re: Need edit of this bash script please!

Post by Logansfury »

xenopeek wrote: Sat Feb 24, 2024 5:48 pm You can throw that whole bunch of code away. wmctrl -k on toggles your desktop manager's "show desktop" function and minimizes all windows
Show desktop was indeed the way. I have a working piece of code now:

Code: Select all

# Define a function to minimize all windows
def minimize_all_windows():
    try:
        # Use wmctrl to activate the "Show Desktop" action
        subprocess.run(["wmctrl", "-k", "on"])
    except subprocess.CalledProcessError as e:
        print("Error:", e)

# Call the function to minimize all open windows
minimize_all_windows()
Thank you very much for the solution :)
Image <-- Cick for sudo inxi --usb -Fxxxnmprz output, updated hourly!
User avatar
Logansfury
Level 6
Level 6
Posts: 1237
Joined: Fri Oct 27, 2023 4:08 pm
Location: Las Vegas NV, USA

Re: Need edit of this bash script please!

Post by Logansfury »

AndyMH wrote: Sat Feb 24, 2024 5:54 pm There is also an icon in the panel that does it for you.
Screenshot from 2024-02-24 21-51-35.png
Hi Andy,

I have used the Show the desktop button frequently. On my setup it resides in the right corner of the top panel. In this specific case however, I needed a scripted way to accomplish it. I suppose I could have used xdotool to position the cursor above the button and click it, but this python script is working fine.
Image <-- Cick for sudo inxi --usb -Fxxxnmprz output, updated hourly!
User avatar
Koentje
Level 7
Level 7
Posts: 1581
Joined: Tue Jan 04, 2022 6:23 pm
Location: Netherlands

Re: Need edit of this .py script please! [SOLVED]

Post by Koentje »

That's a lot of code for only wmctrl -k on!!!

A bash script does it in 2 lines...

Code: Select all

#!/bin/bash
wmctrl -k on

Let me guess.. chatgpt?
Image
User avatar
Logansfury
Level 6
Level 6
Posts: 1237
Joined: Fri Oct 27, 2023 4:08 pm
Location: Las Vegas NV, USA

Re: Need edit of this .py script please! [SOLVED]

Post by Logansfury »

Koentje wrote: Sat Feb 24, 2024 7:07 pm Let me guess.. chatgpt?
I can neither confirm nor deny that here.

It's kind of an involved script. Flow is:

Copy current wallpaper to variable
Close Variety wallpaper changer
Change wallpaper to all white
open animated .gif in Nomacs windowless mode.
Start .mp3
Wait for music to end
When music ends pause 2 seconds
close nomacs
restore wallpaper
restart Variety

I used my Stop_mpg123 shortcut to kill the music before it ran its course for an example video:

https://imgur.com/0iVoRvF

Just something I threw together for fun.
Image <-- Cick for sudo inxi --usb -Fxxxnmprz output, updated hourly!
User avatar
Koentje
Level 7
Level 7
Posts: 1581
Joined: Tue Jan 04, 2022 6:23 pm
Location: Netherlands

Re: Need edit of this .py script please! [SOLVED]

Post by Koentje »

Logansfury wrote: Sat Feb 24, 2024 10:28 pm
Koentje wrote: Sat Feb 24, 2024 7:07 pm Let me guess.. chatgpt?
I can neither confirm nor deny that here.

It's kind of an involved script. Flow is:

Copy current wallpaper to variable
Close Variety wallpaper changer
Change wallpaper to all white
needs a delay of 2 seconds.. ;)
open animated .gif in Nomacs windowless mode.
Start .mp3
Wait for music to end
When music ends pause 2 seconds
close nomacs
restore wallpaper
restart Variety

I used my Stop_mpg123 shortcut to kill the music before it ran its course for an example video:

https://imgur.com/0iVoRvF

Just something I threw together for fun.
But why in python? This can all be done in a bash script. Probably easyer for you to understand... i think.
Image
User avatar
Logansfury
Level 6
Level 6
Posts: 1237
Joined: Fri Oct 27, 2023 4:08 pm
Location: Las Vegas NV, USA

Re: Need edit of this .py script please! [SOLVED]

Post by Logansfury »

Koentje wrote: Sat Feb 24, 2024 10:34 pm But why in python? This can all be done in a bash script. Probably easyer for you to understand... i think.
I tried to get a bash script from the generator first but everything it put together failed. So I switched to python3 and it made a working starter script and I just built it up to the full script from there.

I left out a pause as well there was a 2 second wait where you suggested.
Image <-- Cick for sudo inxi --usb -Fxxxnmprz output, updated hourly!
User avatar
Koentje
Level 7
Level 7
Posts: 1581
Joined: Tue Jan 04, 2022 6:23 pm
Location: Netherlands

Re: Need edit of this .py script please! [SOLVED]

Post by Koentje »

Code: Select all

#!/bin/bash

wallpaper=$(dconf read /org/cinnamon/desktop/background/picture-uri)
wpoptions=$(dconf read /org/cinnamon/desktop/background/picture-options)

dconf write /org/cinnamon/desktop/background/picture-options "'none'"
dconf write /org/cinnamon/desktop/background/primary-color "'#eeeeeeeeecec'"
sleep 2

# Run nomacs command
# Run sound command

killall nomacs

dconf write /org/cinnamon/desktop/background/picture-options "$wpoptions"
dconf write /org/cinnamon/desktop/background/primary-color "$backcolor"
Don't know what the nomacs and sound commands are..
Image
User avatar
Logansfury
Level 6
Level 6
Posts: 1237
Joined: Fri Oct 27, 2023 4:08 pm
Location: Las Vegas NV, USA

Re: Need edit of this .py script please! [SOLVED]

Post by Logansfury »

Koentje wrote: Sat Feb 24, 2024 10:58 pm

Code: Select all

#!/bin/bash

wallpaper=$(dconf read /org/cinnamon/desktop/background/picture-uri)
wpoptions=$(dconf read /org/cinnamon/desktop/background/picture-options)

dconf write /org/cinnamon/desktop/background/picture-options "'none'"
dconf write /org/cinnamon/desktop/background/primary-color "'#eeeeeeeeecec'"
sleep 2

# Run nomacs command
# Run sound command

killall nomacs

dconf write /org/cinnamon/desktop/background/picture-options "$wpoptions"
dconf write /org/cinnamon/desktop/background/primary-color "$backcolor"
Don't know what the nomacs and sound commands are..
I definitely see what you mean about bash accomplishing it with a lot less code!
Image <-- Cick for sudo inxi --usb -Fxxxnmprz output, updated hourly!
Post Reply

Return to “Scripts & Bash”