如何使Apple Script仅在特定的显示器/监视器中单击



我正在尝试编程一个Apple Script,以便在我的第二个监视器上对应用程序执行一系列单击,但我只能在我的主监视器上进行单击。有人能教我如何实现这种行为吗?

参考您的评论。。。

在第三次显示的窗口上,我需要一个脚本:1。单击";齿轮图标";在浏览器的左下角。2.选择";启用补丁合并";。3.禁用点击";发送";4.禁用点击";"音频效果";

下面显示的示例AppleScript代码是在Script Editor中使用Logic Pro 10.6.3macOS Catalina下使用语言和;系统首选项中的区域设置设置为英语(美国)-主要,适用于我,没有问题1

  • 1 系统首选项>安全性&隐私>隐私已根据需要设置/解决

还请注意,示例AppleScript代码假设加载修补程序时合并以下内容:未显示,且发送音频效果已启用。

也只有一个Logic Pro窗口打开。

示例AppleScript代码的第一个中,我在前两个perform action "AXPress" of ¬事件之间经历了约5秒的延迟,并包含了实例AppleScript代码的第二个

示例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脚本,并且可能是kludgy,并且由于多种不同原因容易出错。



带错误处理的更新示例

这是一个示例,基于上面代码的第二个块,该块使用额外的错误处理进行编码,因为它会检查是否显示Library,如果没有,则在继续操作之前显示它,以及其他包含的error handling。以下示例AppleScript代码

也被标记化,因此设置第一组中属性允许使用美国英语以外的语言,方法是根据情况调整其的值em>代码基于这些特性的<em]值>工作。

示例 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中的Library未显示,则必须将焦点转移到Logic-Pro X用程序。这个转变非常短暂,在我的测试中没有引起任何问题。如果已经显示焦点,则焦点将停留在执行脚本时的位置。



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

最新更新