使用 AppleScript 拆分全屏应用程序

2023-12-28

我一直在使用这个脚本 https://gist.github.com/dsummersl/4175461在 Automator 中,它可以在全屏和窗口模式之间切换应用程序。我经常使用分屏应用程序(El Capitan 中引入),那么有什么方法可以修改此脚本以启用分屏功能?我知道没有用于分割的键盘快捷键,所以这绝对是一个盲目的尝试。


我设法将一些东西拼凑在一起,处理我在中找到的 AppleScript/python 东西这篇 MacScripter 帖子 https://macscripter.net/viewtopic.php?id=46154。它执行以下操作:

  1. 从系统事件中提取打开的应用程序窗口列表,并允许用户选择一个或两个(全屏或分屏)
  2. 启动“任务控制”
  3. GUI 脚本 Dock 以查找对任务控制中我们需要访问的各种窗口按钮和空间的引用
  4. 以编程方式拖动按钮以创建新的全屏或分屏空间
  5. 单击新创建的空间将其激活

您也许可以减少所有半秒延迟的时间,但任务控制中心期待人类交互并懒惰地处理事情。它会错过来得太快的 GUI 请求。

(* 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
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

使用 AppleScript 拆分全屏应用程序 的相关文章

随机推荐