11

I have been using this script in Automator, which toggles apps between full-screen and windowed mode. I am a frequent user of split-screen applications (introduced in El Capitan), so is there any way to modify this script to enable split-screen? I know there's no keyboard shortcut for splitting, so this is definitely a shot in the dark.

2 Answers 2

2

I managed to cobble something together, working off an AppleScript/python thing that I found in this MacScripter post. It does the following:

  1. Pulls a list of open application windows from System Events, and allows the user to select one or two (for full screen or split screen)
  2. Launches "Mission Control"
  3. GUI-scripts the Dock to find references to the various window-buttons and spaces in Mission Control that we need to access
  4. Programmatically drags the buttons around to create a new fullscreen or splitscreen space
  5. Clicks on the newly created space to activate it

You might be able to trim some time down on all the half-second delays, but Mission Control is expecting human interaction and handles things lazily. It will miss GUI requests that come too fast.

(* collect names of open app windows *)
tell application "System Events"
    set windowNames to {}
    set theVisibleProcesses to every process whose visible is true
    repeat with thisProcess in theVisibleProcesses
        set windowNames to windowNames & (name of every window of thisProcess whose role description is "standard window")
    end repeat
end tell

(* choose 1 name for fullscreen, two names for split screen *)
set selectedItems to choose from list windowNames with title "Split It" with prompt "Choose items to add to split view." with multiple selections allowed without empty selection allowed
if selectedItems is false or (count of selectedItems) > 2 then return

set selectedWindow1 to item 1 of selectedItems
if (count of selectedItems) = 2 then
    set selectedWindow2 to item 2 of selectedItems
end if

tell application "Mission Control"
    launch
end tell

(* 
The dock has a set of nested UI elements for Mission Control, with the following structure:
    "Mission Control"'s group 1 (the base container)
        group 1, group 2, .... (groups for each desktop)
            buttons for open windows on each desktop
        group "Spaces Bar" 
            a single button (the '+' buttan to add a new space) 
            a list 
                buttons for the desktops and any already-made spaces 
*)

tell application "System Events"
    tell process "Dock"'s group "Mission Control"'s group 1
        tell group "Spaces Bar"'s list 1
            set {p, s} to {position, size} of last UI element
            set XTarget to (item 1 of p) + (item 1 of s) + 100
            set YTarget to (item 2 of p) + (item 2 of s) + 100
        end tell
        tell group 1
            set viewButton1 to button selectedWindow1
            set {x, y} to get viewButton1's position
            my mouseDrag(x, y, XTarget, YTarget, 0.5)
        end tell
        tell group "Spaces Bar"'s list 1
            set {p, s} to {position, size} of last UI element
            set XTarget to (item 1 of p) + (item 1 of s) + 10
            set YTarget to (item 2 of p) + (item 2 of s) + 10
        end tell
        try
            tell group 1
                set viewButton2 to button selectedWindow2
                set {x, y} to get viewButton2's position
                my mouseDrag(x, y, XTarget, YTarget, 0.5)
            end tell
        end try
        tell group "Spaces Bar"'s list 1
            delay 0.5
            set lastUI to last UI element
            click lastUI
        end tell
    end tell
end tell

on mouseDrag(xDown, yDown, xUp, yUp, delayTime)
    do shell script "

/usr/bin/python <<END

from Quartz.CoreGraphics import CGEventCreateMouseEvent
from Quartz.CoreGraphics import CGEventCreate
from Quartz.CoreGraphics import CGEventPost
from Quartz.CoreGraphics import kCGEventLeftMouseDown
from Quartz.CoreGraphics import kCGEventLeftMouseUp
from Quartz.CoreGraphics import kCGMouseButtonLeft
from Quartz.CoreGraphics import kCGHIDEventTap
from Quartz.CoreGraphics import kCGEventLeftMouseDragged
from Quartz.CoreGraphics import kCGEventMouseMoved
import time

def mouseEvent(type, posx, posy):
          theEvent = CGEventCreateMouseEvent(None, type, (posx,posy), kCGMouseButtonLeft)
          CGEventPost(kCGHIDEventTap, theEvent)

def mousemove(posx,posy):
          mouseEvent(kCGEventMouseMoved, posx,posy);

def mousedrag(posx,posy):
          mouseEvent(kCGEventLeftMouseDragged, posx, posy);

def mousedown(posxdown,posydown):
          mouseEvent(kCGEventLeftMouseDown, posxdown,posydown);

def mouseup(posxup,posyup):
      mouseEvent(kCGEventLeftMouseUp, posxup,posyup);

ourEvent = CGEventCreate(None);

mousemove(" & xDown & "," & yDown & ");
mousedown(" & xDown & "," & yDown & ");
time.sleep(" & (delayTime as text) & ");
mousedrag(" & xUp & "," & yUp & ");
time.sleep(" & (delayTime as text) & ");
mouseup(" & xUp & "," & yUp & ");

END"
end mouseDrag
12
  • Interesting; however, it doesn't apear to be doing anything different than if I click-hold the full screen button manually to bring up split screen view. Don't get me wrong, I see some potencial use for it, e.g. as a keyboard shortcut and I've made one, but modded the code a bit. I added set targetApp to (name of every application process whose frontmost is true) as text and tell process targetApp. What would be great is if the code could also select a given app/window in the other side of the split screen view when it initially appears. Do you think you could code something like that? Jul 4, 2019 at 18:16
  • I thought about that, but the mechanism is completely opaque, and I can't see a way to get a handle on it. I mean, I can see what it's doing — it's a standard split view with the window selected on one side and a more-or-less standard collection view on the other — but I don't know exactly what is presenting the split view. The best possibility I can see is to (somehow) get a list of the views presented in the collection list and programmatically click on the one we want, but so far I haven't been able to make that work. Still playing with it, though; if I figure something out I'll add it. Jul 4, 2019 at 20:42
  • Yeah, I've looked at it too and came to the conclusion it will probably take AppleScriptObjC to do it, and I'm just not up on it enough. Anyway +1 for your answer. Jul 4, 2019 at 21:55
  • @user3439894: I'm relatively good with ObjC, and I haven't yet found any hooks for this. I've figured out that it's handled by Mission Control, but MC isn't scriptable, it doesn't accept drags, there's no command-line interface, and I haven't yet found a framework that I can access. I've even been poking under Spaces, Expose, and Dashboard to no avail. It's annoying. If I could figure ASOC I would have, but if you see something I've missed, let me know; I'm curious now. Jul 5, 2019 at 5:50
  • 1
    @MartinBraun: Change the line that starts set selectedItems to choose from list... to set the file names directly, like so: set selectedItems to {"app name 1", "app name 1"}. Then add a couple of lines to launch the app, e.g. tell application (item 1 of selectedItems) to launch. Jun 18 at 4:28
1

I agree with Ted Wrigley's answer above, but I would make the following changes:

(...)

tell application "System Events"
tell process "Dock"'s group "Mission Control"'s group 1
    tell group "Spaces Bar"'s list 1
        set countSpaces to count of UI elements
        set {p, s} to {position, size} of last UI element
        set XTarget to (item 1 of p) + (item 1 of s) + (100 * countSpaces)
        set YTarget to (item 2 of p) + (item 2 of s) + 100
    end tell
 
(...)

If you want to run this script multiple times, you need to count the number of open Spaces to correctly find the right spot to drag the window to. Otherwise, the windows are always are always dragged to the 2nd position of the Spaces Bar.

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.