Search code examples
macoswindowapplescriptmoveforeground

Is it possible to move windows in background (without stealing focus)


I'd like to write a script for an application that has several windows opened at the same time. Some of them are popping out to the foreground when they need the user to perform an action on them.

However, for every window in foreground, there are many others behind it (in background). I'd like to move specific background windows to a different position of the screen.

I'm wondering if this is possible with applescript but WITHOUT STEALING FOCUS; they shouldn't come to foreground when moved.


Solution

  • Open 2 windows (or more) in Safari and try this. It will move the 2nd window 5 pixels to the right. This code should work for your application as well as System Events knows about the windows and can reposition them of other applications. Note that windows also have a size property you may want to use.

    tell application "System Events"
        tell process "Safari"
            set theWindows to windows
            if (count of theWindows) is greater than or equal to 2 then
                set p to position of (item 2 of theWindows)
                set position of window 2 to {(item 1 of p) + 5, item 2 of p}
            end if
        end tell
    end tell