Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I minimize/maximize windows in macOS with the Cocoa API from a Python script?

How can I minimize/maximize windows in macOS from a Python script? On Windows, there's a win32 api (the ShowWindow() function) that can do this. I'd like the macOS equivalent. I'd like to have a script be able to find a window from its title, then minimize or maximize it.

Is this possible? I assume I need to use the pyobjc module for this.

like image 469
Al Sweigart Avatar asked Nov 10 '18 08:11

Al Sweigart


People also ask

How do you minimize a window on a Mac?

Minimize a window: Click the yellow minimize button in the top-left corner of the window, or press Command-M. You can set an option in Dock & Menu Bar preferences to have a window minimize when you double-click its title bar.

How do you maximize a window in Python?

We can maximize or minimize a browser window using Selenium webdriver in Python. We can use the method maxmize_window to maximize a browser. We can use the method minimize_window to minimize a browser. Finally, Again, to get the size of the browser, we can use the method get_window_size.


1 Answers

There are probably different ways to do it, out of which one is by enumerating the running applications and next is enumerating the windows inside the application.

I will show the app approach here

from AppKit import NSApplication, NSApp, NSWorkspace
from Quartz import kCGWindowListOptionOnScreenOnly, kCGNullWindowID, CGWindowListCopyWindowInfo

workspace = NSWorkspace.sharedWorkspace()
activeApps = workspace.runningApplications()
for app in activeApps:
    if app.isActive():
        options = kCGWindowListOptionOnScreenOnly
        windowList = CGWindowListCopyWindowInfo(options,
                                                kCGNullWindowID)
        for window in windowList:
            if window['kCGWindowOwnerName'] == app.localizedName():
                print(window.getKeys_)
                break
        break

This will find the current focused app, you can change the logic based on titles or whatever you want

After the app is found. You can minimize it using

app.hide()

And you can show the app again using

from Cocoa import NSApplicationActivateIgnoringOtherApps, NSApplicationActivateAllWindows
app.unhide()
app.activateWithOptions_(NSApplicationActivateIgnoringOtherApps)

# or 
app.activateWithOptions_(NSApplicationActivateIgnoringOtherApps | NSApplicationActivateAllWindows)

Lot of threads I had to refer to get to this solution

OS X: Move window from Python

How to list all windows from all workspaces in Python on Mac?

How to get Window reference (CGWindow, NSWindow or WindowRef) from CGWindowID in Swift?

"No such file: 'requirements.txt' error" while installing Quartz module

How to build Apple's Son of grab example?

How to get another application window's title, position and size in Mac OS without Accessibility API?

Get the title of the current active Window/Document in Mac OS X

-[NSRunningApplication activateWithOptions:] not working

How to start an app in the foreground on Mac OS X with Python?

set NSWindow focused

Activate a window using its Window ID

like image 115
Tarun Lalwani Avatar answered Sep 21 '22 08:09

Tarun Lalwani