3

When you unplug an external monitor with a higher resolution that your macbook from your laptop, the windows mostly retain their width, but their size gets clipped to the (smaller) height of the macbook screen. When you plug the monitor back in, their size remains frustratingly small.

My question is: is there any way that I can iterate over all open windows, save their size, and restore them once the monitor gets plugged in again?

2 Answers 2

10

The following AppleScript show how to:

  • Loop through all windows
  • Retrieve and change window position
  • Retrieve and change window size

Code:

tell application "System Events"
    set theProcesses to application processes
    repeat with theProcess from 1 to count theProcesses
        tell process theProcess
            repeat with x from 1 to (count windows)
                set windowPosition to position of window x
                set windowSize to size of window x
                set position of window x to {0, 0}
                set size of window  to {100, 100}
            end repeat
        end tell
    end repeat
end tell

Note: The script requires access for assistive devices (AfAD):
"System Preferences" → "Universal Acces" → "Enable access for assistive devices"

EDIT (response to comment)

Enabling AfAD from AppleScipt might improve user experience, but don't do this every time the script is executed, only enable AfAD in case AfAD is disabled. Enabling features without informing the user is no good practice, prompt the user for permission to enable AfAD.

Example:

set AccesEnables to do shell script "[ -e \"/private/var/db/.AccessibilityAPIEnabled\" ] && echo \"Yes\" || echo \"No\""
if (AccesEnables is equal to "No") then
    set askUser to display dialog "This application requires access for assistive devices. Enable this feature?" default button 2
    set answer to button returned of askUser
    if answer is equal to "OK" then
        do shell script "touch /private/var/db/.AccessibilityAPIEnabled" with administrator privileges
    else
        close
    end if
end if
4
  • +1, but it might be worth showing how to programmatically turn on assistive access by asking for authorization (do shell script… with administrator privileges).
    – abarnert
    Jul 9, 2012 at 21:19
  • Too bad I can't +1 again… but "with administrator privileges" already pops up a dialog asking whether to give permission, so you don't want to pop up a dialog asking whether to pop up the dialog to ask for permission, unless you're a big fan of Windows Vista. :)
    – abarnert
    Jul 9, 2012 at 22:17
  • 1
    That's not completely true. The first dialog informs the user that additional features need to be enabled in order to use the application. The second dialog only asks for the password, but does not explain why. Using both dialogs, the user knows what's happening and can choose wether to enable the feature or not. Additionally, you can warm the user within the first dialog that administrator privileges are required and the next window will ask for the password. Permanently changing settings within informing the user is bad practice, that's why the additional dialog should be used.
    – Anne
    Jul 9, 2012 at 22:27
  • warm == warn && within == without :)
    – Anne
    Jul 10, 2012 at 9:02
3

The AppleScript solution—for which see Anne's answer—is by far the easiest.

If you want to give this to friends who can't be relied on to figure out how to enable assistive access, or distribute it more widely, just add this line:

do shell script ¬
  "touch /private/var/db/.AccessibilityAPIEnabled" ¬
  with administrator privileges

This will pop up the usual auth dialog and then use the privileges to turn on assistive access.

It is actually possible to do this without assistive access, but it requires using private functions within CoreGraphics/Quartz Window Services, namely CGSPrivate.h.

With public APIs, you can easily enumerate all of the windows:

CFArrayRef windows = 
  CGWindowListCopyWindowInfo(kCGWindowListOptionOnScreenOnly |
                             kCGWindowListExcludeDesktopElements,
                             kCGNullWindowID);

This returns an array of dictionaries, and each dictionary contains a kCGWindowBounds dictionary, which has Height, Width, X, and Y integer values.

But these public APIs are strictly read-only. To actually move the windows, you have to dip into CGSPrivate.h to do something like this:

CGSConnection conn = _CGSDefaultConnection();
for (NSDictionary *window in windows) {
  CGSWindow wid = (CGSWindow)[[window objectForKey:@"kCGWindowNumber"] intValue];
  CGRect bounds;
  CGRectMakeWithDictionaryRepresentation([window objectForKey:@"kCGWindowBounds"],
                                         &bounds);
  CGSMoveWindow(conn, wid, bounds.origin);
}

Obviously, this is pretty nasty, and you should only consider it if you really need to distribute an app that can't ask for assistive access.

You could also reverse-engineer the Window Server protocol and talk to it directly, but this is even nastier.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.