How can I send the currently active window to the back?

Is there a shortcut or such to send the currently active window to the back of the screen while still keeping it open (so that it ends up "beneath" all other open windows)?


Solution 1:

Short answer: No, Apple does not provide an API that allows you to alter an application window index except by bringing a window to the front (index 1). There are no elegant solutions. But:

Long answer: Yes, You can use AppleScript to effectively move a window to the back of a stack of AppleScript-able windows by collecting a list of windows, then rapidly moving each one (except the frontmost) to the front, leaving the chosen window at the back.

Here is an example of an AppleScript which sends the frontmost Finder Window to the back.

tell application "Finder"
    set winList to every window whose visible is true
    if not winList = {} then
        repeat with oWin in (items 1 through -2 of reverse of winList)
            set index of oWin to 1
        end repeat
    end if
end tell

This script is based on a solution by JMichaelTX discussed on the KeyboardMaestro forum.

The reshuffling is incredibly fast, but in the Script Editor you can inspect how it works, leaving "506" on the bottom and popping "502-505" up in a way that preserves their order:

get every window whose visible = true
    --> {Finder window id 506, Finder window id 505, Finder window id 504, Finder window id 503, Finder window id 502}
set index of Finder window id 502 to 1
    --> 1
set index of Finder window id 503 to 1
    --> 1
set index of Finder window id 504 to 1
    --> 1
set index of Finder window id 505 to 1
    --> 1

You can map this AppleScript to a service or keyboard shortcut, either by a) putting the script into a Run AppleScript action inside an Automator service, or b) using a launcher (Alfred, Keyboard Maestro etc.). For details on Applescript keyboard assignment see: How do I assign a keyboard shortcut to an AppleScript I wrote?

This may satisfy OPs needs -- it is unclear from the original post. This solution can also be extended to multiple AppleScript-able applications. I am not, however, aware of a solution to make it work effectively with a mix of scriptable and non-scriptable windows, due to the fact that in AppleScript, System Events addressable windows for non-scriptable applications do not have a set index verb/property. There exists an "AXRaise" action for some windows when assistive devices / accessibility is turned on, but this also does not apply to all windows. Elaborate hacks exist that involve walking through each active application and show/hiding or minimizing/restoring windows, but they are a mess.

Related discussions:

  • How to set a window's index to last (furthest back) in applescript
  • Applescript - Bring window to foreground
  • Is there a way to iterate over all open windows in Mac OS X?

Solution 2:

Your question isn't very clear, but if what you want is a shortcut to cycle through all windows of the currently active app, you should be able to do this with the Command~ keyboard shortcut.

The ~ key is usually located at left of the 1 key.

Just to clarify, what the Command~ keyboard shortcut does is cycle through all windows of the current app you're in. For example, if you're using MS Word and have three documents open, it will cycle through those three documents.

However, the Command~ keyboard shortcut does not cycle through Tabs. For example, if you have Safari open with three windows and each window has five tabs open, using this shortcut will cycle through the three windows, not through the individual tabs.

Another option that may achieve what you want is the CommandM keyboard shortcut. However this minimises the currently active window to the Dock, so I suspect that's not what you want.

If I've misunderstood your question totally, please clarify.