Search code examples
applescript

AppleScript to raise a window in another space/desktop


I am trying to switch between windows in different desktops with AppleScript. I have 2 Chrome windows opened in 2 different desktops (Desktop 2 & 3). I am able to see properties and indices of both windows with tell application "Google Chrome" to get properties of windows but I can only raise the one in the desktop that I am in (Desktop 2) with this code:

tell application "System Events" to tell process "Google Chrome"
    perform action "AXRaise" of window 1
end tell

Output from tell application "Google Chrome" to get properties of windows:

{{closeable:true, zoomed:true, active tab index:1, class:window, index:1, visible:true, name:"YouTube", miniaturizable:true, mode:"normal", active tab:tab id 477 of window id 476 of application "Google Chrome", id:476, miniaturized:false, resizable:true, bounds:{1280, -255, 3200, 800}, zoomable:true}, {closeable:true, zoomed:true, active tab index:1, class:window, index:2, visible:true, name:"Facebook", miniaturizable:true, mode:"normal", active tab:tab id 482 of window id 481 of application "Google Chrome", id:481, miniaturized:false, resizable:true, bounds:{1280, -255, 3200, 800}, zoomable:true}}

Is there a way to raise the window in the other space directly? Thank you.


Solution

  • I'm offering this as a workaround as it does work in a simple two normal window configuration where there is one window on each Desktop, and is executed from Desktop 1.

    Example AppleScript code:

    tell application "Google Chrome"
        set minimized of window 2 to true
        set minimized of window 2 to false
    end tell
    

    Note that this does cause seeing the window from Desktop 2 be minimized and restored as if on Desktop 1 but window 2 is then frontmost on Desktop 2 without having switched Spaces.

    The was tested in a single display multiple Desktop scenario under macOS Catalina. Other versions of macOS and or Display configuration may present differing results.