new fixes that worked
nice! it is much better now... getting there.
This commit is contained in:
@@ -1,27 +1,49 @@
|
||||
#!/usr/bin/env python3
|
||||
"""
|
||||
Minimize All Windows - Python Version
|
||||
Minimize All Windows - Python Version (Fixed)
|
||||
Requires: pip3 install pyobjc-framework-Cocoa pyobjc-framework-Quartz
|
||||
|
||||
This uses native macOS APIs which should be more reliable than AppleScript.
|
||||
"""
|
||||
|
||||
import time
|
||||
from Cocoa import NSWorkspace, NSRunningApplication
|
||||
from Cocoa import NSWorkspace
|
||||
from Quartz import (
|
||||
CGWindowListCopyWindowInfo,
|
||||
kCGWindowListOptionOnScreenOnly,
|
||||
kCGNullWindowID
|
||||
kCGNullWindowID,
|
||||
kCGWindowListExcludeDesktopElements
|
||||
)
|
||||
import subprocess
|
||||
|
||||
def get_all_windows():
|
||||
"""Get all visible windows from all apps"""
|
||||
|
||||
def get_apps_with_visible_windows():
|
||||
"""Get set of app names that have actual visible on-screen windows"""
|
||||
window_list = CGWindowListCopyWindowInfo(
|
||||
kCGWindowListOptionOnScreenOnly,
|
||||
kCGWindowListOptionOnScreenOnly | kCGWindowListExcludeDesktopElements,
|
||||
kCGNullWindowID
|
||||
)
|
||||
return window_list
|
||||
|
||||
apps_with_windows = set()
|
||||
|
||||
for window in window_list:
|
||||
# Get the owning application name
|
||||
owner_name = window.get('kCGWindowOwnerName', '')
|
||||
|
||||
# Check if window is actually visible and has size
|
||||
# Layer 0 = normal windows, skip menu bar items etc.
|
||||
layer = window.get('kCGWindowLayer', 0)
|
||||
bounds = window.get('kCGWindowBounds', {})
|
||||
width = bounds.get('Width', 0)
|
||||
height = bounds.get('Height', 0)
|
||||
|
||||
# Only count windows that are on the normal layer and have reasonable size
|
||||
# (filters out menu bar, dock, status items, etc.)
|
||||
if layer == 0 and width > 50 and height > 50 and owner_name:
|
||||
apps_with_windows.add(owner_name)
|
||||
|
||||
return apps_with_windows
|
||||
|
||||
|
||||
def minimize_window_via_applescript(app_name):
|
||||
"""Use AppleScript to minimize the frontmost window of an app"""
|
||||
@@ -45,12 +67,62 @@ def minimize_window_via_applescript(app_name):
|
||||
print(f"Failed to minimize {app_name}: {e}")
|
||||
return False
|
||||
|
||||
|
||||
def minimize_finder_windows():
|
||||
"""Minimize all Finder windows using Finder's native scripting"""
|
||||
# First, get count of Finder windows to know if we need to do anything
|
||||
count_script = '''
|
||||
tell application "Finder"
|
||||
return count of Finder windows
|
||||
end tell
|
||||
'''
|
||||
try:
|
||||
result = subprocess.run(
|
||||
['osascript', '-e', count_script],
|
||||
capture_output=True,
|
||||
text=True,
|
||||
timeout=5
|
||||
)
|
||||
window_count = int(result.stdout.strip()) if result.stdout.strip() else 0
|
||||
|
||||
if window_count == 0:
|
||||
print("Finder: No windows to minimize")
|
||||
return
|
||||
|
||||
print(f"Finder: Minimizing {window_count} window(s)")
|
||||
|
||||
# Minimize each window one by one (more reliable than batch)
|
||||
minimize_script = '''
|
||||
tell application "Finder"
|
||||
repeat with aWindow in (every Finder window)
|
||||
try
|
||||
set miniaturized of aWindow to true
|
||||
delay 0.2
|
||||
end try
|
||||
end repeat
|
||||
end tell
|
||||
'''
|
||||
subprocess.run(['osascript', '-e', minimize_script], timeout=10, check=False)
|
||||
|
||||
except Exception as e:
|
||||
print(f"Failed to minimize Finder windows: {e}")
|
||||
|
||||
|
||||
def main():
|
||||
# Apps to skip
|
||||
skip_apps = {'Finder', 'SystemUIServer', 'Dock', 'Electron', 'MSTeams', 'nxdock'}
|
||||
# Apps to skip - these either don't have minimizable windows or cause issues
|
||||
skip_apps = {'SystemUIServer', 'Dock', 'Electron', 'MSTeams', 'nxdock',
|
||||
'Control Center', 'Notification Center', 'Spotlight'}
|
||||
skip_keywords = ['Helper', 'Agent']
|
||||
|
||||
# Get all running applications
|
||||
# FIX #1: Get only apps that actually have visible windows on screen
|
||||
apps_with_windows = get_apps_with_visible_windows()
|
||||
print(f"Apps with visible windows: {apps_with_windows}")
|
||||
|
||||
# FIX #2: Handle Finder separately and skip it in main loop
|
||||
# Remove Finder from the set - we'll handle it specially
|
||||
apps_with_windows.discard('Finder')
|
||||
|
||||
# Get all running applications (to get proper app objects)
|
||||
workspace = NSWorkspace.sharedWorkspace()
|
||||
running_apps = workspace.runningApplications()
|
||||
|
||||
@@ -62,6 +134,10 @@ def main():
|
||||
# Skip if already processed
|
||||
if app_name in processed_apps:
|
||||
continue
|
||||
|
||||
# FIX #1: Skip if this app doesn't have visible windows
|
||||
if app_name not in apps_with_windows:
|
||||
continue
|
||||
|
||||
# Skip system apps and problematic apps
|
||||
if app_name in skip_apps:
|
||||
@@ -80,21 +156,12 @@ def main():
|
||||
processed_apps.add(app_name)
|
||||
time.sleep(0.3)
|
||||
|
||||
# Handle Finder windows separately
|
||||
print("Processing: Finder windows")
|
||||
finder_script = '''
|
||||
tell application "Finder"
|
||||
try
|
||||
set allWindows to every Finder window
|
||||
repeat with aWindow in allWindows
|
||||
set miniaturized of aWindow to true
|
||||
end repeat
|
||||
end try
|
||||
end tell
|
||||
'''
|
||||
subprocess.run(['osascript', '-e', finder_script], check=False)
|
||||
# FIX #2: Handle Finder windows separately using Finder's native scripting
|
||||
# This is more reliable than using Cmd+M
|
||||
minimize_finder_windows()
|
||||
|
||||
print("Done minimizing all windows")
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
main()
|
||||
|
||||
Reference in New Issue
Block a user