首页 > 解决方案 > 如何让 Apple Script 仅在特定的显示器/监视器中单击?

问题描述

我正在尝试编写一个 Apple 脚本以在我的第二台显示器上对应用程序执行一系列点击,但我只能让点击在我的主显示器上工作。有人可以指导我如何实现这种行为吗?

标签: macosscriptingapplescript

解决方案


参考您的评论...

在我的第三个显示器窗口中的所有 Logic Pro X 中,我需要一个脚本来: 1. 单击浏览器左下方的“齿轮图标”。2. 选择“启用补丁合并”。3.禁用-单击“发送” 4.禁用-单击“音频效果”

示例 AppleScript 代码(如下所示)在macOS Catalina下使用Logic Pro 10.6.3在脚本编辑器中进行了测试,系统偏好设置中的语言和区域设置设置为英语(美国) - 主要并且为我工作,没有问题1

  • 1 假设系统偏好设置>安全和隐私>隐私中的必要和适当设置已根据需要进行设置/解决。

另请注意,示例 AppleScript 代码假定加载 Patch 时的 Merge 以下:未显示并且Sends and Audio Effects启用

也只打开了一个Logic Pro 窗口

在第一个示例AppleScript代码中,我在前两个事件之间经历了约 5 秒的延迟,并且包含了第二个示例AppleScript代码,它可以解决这个已知的约 5 秒延迟问题,在某些情况下会发生这种情况,以防您也体验一下。 perform action "AXPress" of ¬


示例 AppleScript 代码

tell application "System Events"
    tell application process "Logic Pro X"
        perform action "AXPress" of ¬
            pop up button 1 of ¬
            group 1 of group 2 of window 1
        perform action "AXPress" of ¬
            menu item "Enable Patch Merging" of ¬
            menu 1 of pop up button 1 of ¬
            group 1 of group 2 of window 1
        perform action "AXPress" of ¬
            checkbox "Sends" of ¬
            group 1 of group 1 of group 2 of window 1
        perform action "AXPress" of ¬
            checkbox "Audio Effects" of ¬
            group 1 of group 1 of group 2 of window 1
    end tell
end tell

如果您遇到约 5 秒的延迟,请尝试以下操作...

示例 AppleScript 代码

ignoring application responses
    tell application "System Events" to ¬
        perform action "AXPress" of ¬
            pop up button 1 of group 1 of group 2 of ¬
            window 1 of application process "Logic Pro X"
end ignoring

delay 0.1
do shell script "killall 'System Events'"
delay 0.2

tell application "System Events"
    run
    delay 0.2
    tell application process "Logic Pro X"
        perform action "AXPress" of ¬
            menu item "Enable Patch Merging" of ¬
            menu 1 of pop up button 1 of ¬
            group 1 of group 2 of window 1
        perform action "AXPress" of ¬
            checkbox "Sends" of ¬
            group 1 of group 1 of group 2 of window 1
        perform action "AXPress" of ¬
            checkbox "Audio Effects" of ¬
            group 1 of group 1 of group 2 of window 1
    end tell
end tell

笔记:

虽然此处显示的示例 AppleScript 代码在指定的条件下对我有用,但是,它可能需要根据您的条件、macOS的版本进行调整,并且可能需要添加一些错误处理或在某些事件之间使用delay 命令。

这是一个最简单的示例,应该添加额外的代码来检查各种UI 元素的状态,以允许没有问题地出现想要的结果。例如,在执行操作之前检查是否启用了发送音频效果等。

示例 AppleScript 代码使用UI 脚本,并且由于许多不同的原因可能很笨拙并且容易出错。



更新了错误处理示例

这是一个示例,基于上面的第二个代码它使用额外的错误处理进行编码,因为它检查是否正在显示,如果没有在继续之前显示它,以及其他包含的错误处理。以下示例AppleScript代码也被标记化,因此设置第一组中的属性允许通过适当调整它们的值来使用美国英语以外的语言,从而允许剩余代码基于该值工作 那些属性

示例 AppleScript 代码

--  # User adjustable properties, the names 
--  # as they appear on the UI elements.

property |Enable Patch Merging| : "Enable Patch Merging"
property |Sends| : "Sends"
property |Audio Effects| : "Audio Effects"
property menuName : "View"
property menuCommand : "Show Library"
property windowName : "Untitled - Tracks"


--  ##############################################
--  # Do not modify code below unless necessary. #
--  ##############################################

property |Logic Pro| : name of application id "com.apple.logic10"
property |System Events| : name of application id "com.apple.systemevents"
property appName : missing value
property restoreFrontmost : false

tell application id "com.apple.systemevents"
    set appName to name of first process whose frontmost is true and background only is false
    tell application process |Logic Pro|
        if not (exists pop up button 1 of group 1 of group 2 of window windowName) then
            set restoreFrontmost to true
            perform action "AXRaise" of window windowName
            set frontmost to true
            perform action "AXPress" of menu bar item menuName of menu bar 1
            delay 0.2
            perform action "AXPress" of menu item menuCommand of menu menuName of menu bar item menuName of menu bar 1
        end if
        repeat until exists pop up button 1 of group 1 of group 2 of window windowName
            delay 0.01
        end repeat
        if not (exists checkbox |Sends| of group 1 of group 1 of group 2 of window windowName) then
            ignoring application responses
                perform action "AXPress" of pop up button 1 of group 1 of group 2 of window windowName
            end ignoring
        end if
    end tell
end tell

delay 0.1
do shell script "killall " & quoted form of |System Events|
delay 0.2

tell application id "com.apple.systemevents"
    run
    delay 0.2
    tell application process |Logic Pro|
        if not (exists checkbox |Sends| of group 1 of group 1 of group 2 of window windowName) then
            perform action "AXPress" of menu item "Enable Patch Merging" of menu 1 of pop up button 1 of group 1 of group 2 of window windowName
            delay 0.2
        end if
        if the value of checkbox |Sends| of group 1 of group 1 of group 2 of window windowName as boolean is true then
            perform action "AXPress" of checkbox |Sends| of group 1 of group 1 of group 2 of window windowName
        end if
        if the value of checkbox |Audio Effects| of group 1 of group 1 of group 2 of window windowName as boolean is true then
            perform action "AXPress" of checkbox |Audio Effects| of group 1 of group 1 of group 2 of window windowName
        end if
    end tell
end tell

if restoreFrontmost is true then tell application appName to activate

笔记:

测试是 在辅助显示器上使用Logic Pro X完成的,而脚本是从显示器执行的。

如果Logic Pro X中的资源未显示,则必须将焦点转移到Logic Pro X ,但它会恢复到焦点转移之前最前面的应用程序。这种转变非常短暂,在我的测试中没有引起任何问题。如果已经显示焦点停留在脚本执行时的位置。



注意:示例 AppleScript 代码就是这样,并且没有任何包含的错误处理,不包含任何可能适当的额外错误处理。用户有责任根据需要或需要添加任何错误处理。查看AppleScript Language Guide中的try 语句error 语句。另请参阅处理错误。此外,在适当的情况下,可能需要在事件之间使用延迟命令,例如,使用延迟 delay 0.5适当设置。


推荐阅读