0

I have simple script that launches Windows File Explorer

import subprocess
subprocess.call(["start", "explorer.exe"],shell=True)

I'd like to check if Windows File Explorer is already opened so that I don't open another instance. How can I do it? Solutions without external libraries are preferred.

Wilhelm Olejnik
  • 2,382
  • 3
  • 14
  • 21

1 Answers1

0

Found the solution

import win32gui

explorerWindows = []

def handler( hwnd, list ):
  # only explorer windows have class 'CabinetWClass'
  if 'CabinetWClass' in win32gui.GetClassName(hwnd):
    list.append(hwnd)

win32gui.EnumWindows(handler, explorerWindows)

explorerOpened = len(explorerWindows) > 0

EDIT: easier method

import win32gui
explorerOpened = win32gui.FindWindow('CabinetWClass', None) != 0
Wilhelm Olejnik
  • 2,382
  • 3
  • 14
  • 21
  • Can you also explain how/why this works? For example, what is `CabinetWClass`? (Please do not answer in comments but edit your answer) – wovano May 04 '22 at 05:46
  • (And maybe add a reference to documentation where you found this) – wovano May 04 '22 at 06:08
  • NB: Interesting related article: [Why does the class name for Explorer change depending on whether you open it with /e?](https://devblogs.microsoft.com/oldnewthing/20150619-00/?p=45341) – wovano May 04 '22 at 06:12