Skip to content

Instantly share code, notes, and snippets.

@GitHubIsToxic
Created August 9, 2024 20:32
Show Gist options
  • Save GitHubIsToxic/dfa0804df4510e27ca8eaafc689626cc to your computer and use it in GitHub Desktop.
Save GitHubIsToxic/dfa0804df4510e27ca8eaafc689626cc to your computer and use it in GitHub Desktop.
Python 3 script to restart Nemo file manager window instances (e.g. if plugins crash or if Nemo becomes sluggish)
#!/usr/bin/python3
import os
import time
import signal
import platform
import subprocess
restartDesktop = True
# get system name (needed to parse wmctrl output)
sysname = platform.node()
nemoPID = None
nemoInstances = []
# get list of windows
res = subprocess.check_output( 'wmctrl -l -p -x -G'.split() ).decode("utf-8").strip()
for line in res.split('\n'):
# split line to words
words = line.split()
# parse
win = words[0]
desktop = words[1]
pid = int( words[2] )
#print( words )
posx = int( words[3] )
posy = int( words[4] )
sizex = int( words[5] )
sizey = int( words[6] )
xclass = words[7]
client = words[8]
# nemo-desktop
if xclass == 'nemo-desktop.Nemo-desktop':
pass
# nemo processes
if xclass == 'nemo.Nemo':
nemoPID = pid
# get title from line
title = line[ line.index(sysname)+len(sysname) :]
# get path from title
if ' - ' in title:
path = title[ title.index(' - ')+3 :]
print( xclass + '\t' + path )
# add to list
if os.path.exists( path ):
nemoInstances.append( [path, posx, posy, sizex, sizey] )
# relaunch nemo
if nemoPID:
# kill nemo-desktop
if restartDesktop:
#os.kill( nemoPID, signal.SIGKILL )
os.system('nemo-desktop -q')
# kill nemo
#os.kill( nemoPID, signal.SIGKILL )
os.system('nemo -q')
# kill RabbitVCS
os.system('pkill -f checkerservice.py')
# wait a bit
time.sleep(1)
# restart nemo-desktop
if restartDesktop:
os.system('nemo-desktop & disown')
# restart new instances
for instance in nemoInstances:
path,x,y,w,h = instance
os.system('nemo --geometry=%ix%i+%i+%i "%s" & disown' % (w,h,x,y,path) )
# END OF SCRIPT
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment