Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/macos/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Macos 使用AppleScript拆分全屏应用程序_Macos_Automation_Applescript_Automator_Macos Sierra - Fatal编程技术网

Macos 使用AppleScript拆分全屏应用程序

Macos 使用AppleScript拆分全屏应用程序,macos,automation,applescript,automator,macos-sierra,Macos,Automation,Applescript,Automator,Macos Sierra,我一直在使用Automator,它可以在全屏和窗口模式之间切换应用程序。我经常使用分割屏幕应用程序(在El Capitan中介绍),那么有没有办法修改此脚本以启用分割屏幕?我知道没有用于拆分的键盘快捷键,因此这绝对是一次冒险。我成功地拼凑了一些东西,使用了我在中找到的AppleScript/python东西。它做了以下工作: 从系统事件中提取打开的应用程序窗口列表,并允许用户选择一个或两个(全屏或分屏) 发射“任务控制” GUI为Dock编写脚本,以查找对任务控制中我们需要访问的各种窗口按钮和空

我一直在使用Automator,它可以在全屏和窗口模式之间切换应用程序。我经常使用分割屏幕应用程序(在El Capitan中介绍),那么有没有办法修改此脚本以启用分割屏幕?我知道没有用于拆分的键盘快捷键,因此这绝对是一次冒险。

我成功地拼凑了一些东西,使用了我在中找到的AppleScript/python东西。它做了以下工作:

  • 从系统事件中提取打开的应用程序窗口列表,并允许用户选择一个或两个(全屏或分屏)
  • 发射“任务控制”
  • GUI为Dock编写脚本,以查找对任务控制中我们需要访问的各种窗口按钮和空间的引用
  • 以编程方式拖动按钮以创建新的全屏或分屏空间
  • 单击新创建的空间以激活它
  • 你也许可以在所有的半秒延迟上减少一些时间,但是任务控制中心期待着人与人之间的互动,并且处理事情很懒散。它将错过来得太快的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
    
    (*收集打开的应用程序窗口的名称*)
    告诉应用程序“系统事件”
    将windowNames设置为{}
    将VisibleProcess设置为visible为true的每个进程
    在VisibleProcess中重复此过程
    将windowNames设置为windowNames&(此流程中角色描述为“标准窗口”的每个窗口的名称)
    结束重复
    结束语
    (*全屏选择1个名称,分屏选择2个名称*)
    设置selectedItems以从标题为“拆分”的列表窗口名中进行选择,并提示“选择要添加到拆分视图的项目”。允许多选,但不允许空选
    如果selectedItems为false或(selectedItems计数)>2,则返回
    将selectedWindow1设置为selectedItems的项目1
    如果(selectedItems计数)=2,则
    将selectedWindow2设置为selectedItems的项目2
    如果结束
    告诉应用程序“任务控制”
    发射
    结束语
    (* 
    dock具有一组用于任务控制的嵌套UI元素,具有以下结构:
    “任务控制”的第1组(基本容器)
    第1组、第2组……(每个桌面的组)
    每个桌面上打开窗口的按钮
    “空格栏”组
    单个按钮(添加新空间的“+”按钮)
    单子
    桌面和任何已创建空间的按钮
    *)
    告诉应用程序“系统事件”
    告诉流程“码头”组“任务控制”组1
    告诉组“空格栏”的列表1
    将{p,s}设置为最后一个UI元素的{position,size}
    将XTarget设置为(p的第1项)+(s的第1项)+100
    将Y目标设置为(p的第2项)+(s的第2项)+100
    结束语
    告诉第一组
    将viewButton1设置为按钮选择窗口1
    设置{x,y}以获取viewButton1的位置
    my mouseDrag(x,y,x目标,y目标,0.5)
    结束语
    告诉组“空格栏”的列表1
    将{p,s}设置为最后一个UI元素的{position,size}
    将X目标设置为(p的第1项)+(s的第1项)+10
    将Y目标设置为(p的第2项)+(s的第2项)+10
    结束语
    尝试
    告诉第一组
    将viewButton2设置为按钮选择窗口2
    设置{x,y}以获取viewButton2的位置
    my mouseDrag(x,y,x目标,y目标,0.5)
    结束语
    结束尝试
    告诉组“空格栏”的列表1
    延迟0.5
    将lastUI设置为lastUI元素
    单击最后一个用户界面
    结束语
    结束语
    结束语
    在mouseDrag上(xDown、yDown、xUp、yUp、delayTime)
    执行shell脚本“
    
    /usr/bin/python很有趣;但是,如果我手动单击并按住全屏按钮以显示分屏视图,那么它的功能就没有什么不同了。请不要误会,我看到了它的一些潜在用途,例如,作为键盘快捷键,我制作了一个,但对代码进行了一些修改。我添加了
    将targetApp设置为(最前面为真的每个申请流程的名称)作为文本
    告诉process targetApp
    。如果代码在最初出现时也可以在拆分屏幕视图的另一侧选择给定的应用程序/窗口,那就太好了。你认为你可以编写类似的代码吗?我想过,但机制是完全不透明的,我找不到方法来处理它它。我的意思是,我可以看到它在做什么-它是一个标准的分割视图,一边选择窗口,另一边或多或少是一个标准的集合视图-但我不知道到底是什么呈现分割视图。我能看到的最好的可能性是(不知何故)获取集合列表中显示的视图列表,并以编程方式单击我们想要的视图,但到目前为止,我还无法实现这一点。不过,我仍在玩它;如果我找到了什么,我会添加它。是的,我也看过了它,并得出结论,它可能需要AppleScriptObjC来完成,而我只是不了解它这就足够了。不管怎样,你的答案是+1。@user3439894:我对ObjC比较在行,我还没有找到任何相关的钩子。我发现它是由任务控制来处理的,但MC不可编写脚本,它不接受拖拽,没有命令行界面,我还没有找到一个可以访问的框架。我甚至一直在探索空格、曝光和仪表板都没有用。这很烦人。如果我能算出ASOC,我会的,但如果你看到我遗漏了什么,请告诉我;我现在很好奇。啊,我想我知道了。为了清楚起见,我会把它放在一个新的答案中。(顺便说一句,我会重写我原来的答案)