使用 AppleScript 检查滚动是否完成
Check whether scroll is finished using AppleScript
我有一个 AppleScript 脚本来打开邮件应用程序,打开一封邮件,向下滚动并截取整个邮件正文。我通过使用 keystroke 121 (page down) 滚动页面截取屏幕截图。我不知道要重复循环多少时间才能到达页面末尾。无论如何我们可以使用 AppleScript 确定滚动是否已到达页面末尾?
这是我向下翻页和截图的代码片段:
open each_message
tell application "System Events"
delay 5
keystroke "f" using {command down, control down}
delay 3
key code 20 using {command down, shift down}
delay 5
key code 121
delay 5
key code 20 using {command down, shift down}
delay 5
keystroke "f" using {command down, control down}
end tell
这是一个处理程序,它 returns 在最前面的消息查看器中查看的消息的当前垂直滚动条位置。它对您打开了哪些面板以及查看器的方向做出了一系列假设,但它应该为您提供一些有用的东西:
set sp to my findMailScrollPosition()
on findMailScrollPosition()
tell application "System Events"
tell process "Mail"
tell window 1
tell splitter group 1
tell splitter group 1
tell scroll area 2
tell (first scroll bar whose orientation is "AXVerticalOrientation")
-- this will return a value between 0 and 1 (top to bottom)
return value
end tell
end tell
end tell
end tell
end tell
end tell
end tell
end findMailScrollPosition
阅读您的问题和随后的评论后...
Is there anyway we can identify if the scroll has reached the page end using AppleScript?
是
For some emails, it's fine. For some, it is duplicating the last page, for some, the screenshots are incomplete. (From comments under the question.)
这是不同长度文档的本质,按下 Page Down key,以及为什么可能使用 像 Snagit 这样的第三方应用程序 是更好的选择。
都说了...
下面显示的示例AppleScript代码在[=92=中进行了测试macOS Catalina 下的 ]Script Editor 以及 System Preferences 中的 Language & Region 设置到 英语(美国)— 小学 并为我工作没有问题1.
- 1 在 系统偏好设置 > 安全和隐私[= 中假定必要且适当的设置124=] > 隐私 已根据需要 set/addressed。
由于您没有包含脚本的完整代码,以下示例 AppleScript code 更像是一个 概念验证 。它也 may/will 有额外的屏幕截图,但是,它确保页面在开始截屏之前一直到顶部,并确保在适当停止的基础上也截取整个最后一页的屏幕截图。
出于测试目的,我首先打开了一封单独的电子邮件 window,假设 open each_message
位于 代码 你包含的是或代表。
示例 AppleScript 代码:
tell application "Mail" to activate
delay 1
tell application "System Events"
keystroke "f" using {command down, control down}
delay 1
repeat while my getEmailFullScreenWindowScrollPosition() > 0.0
key code 116
delay 0.1
end repeat
delay 1
repeat while my getEmailFullScreenWindowScrollPosition() < 1.0
key code 20 using {command down, shift down}
delay 1
key code 121
delay 1
end repeat
key code 121
delay 1
key code 20 using {command down, shift down}
delay 1
keystroke "f" using {command down, control down}
end tell
to getEmailFullScreenWindowScrollPosition()
tell application "System Events" to ¬
return value of ¬
scroll bar 1 of ¬
scroll area 1 of ¬
window 1 of ¬
process "Mail"
end getEmailFullScreenWindowScrollPosition
备注:
在 Mail 在 macOS Catalina 我的系统上没有 水平滚动条 当一个打开的电子邮件 window 处于 全屏 查看,以及为什么处理程序 指向 scroll bar 1
。但是,如果您确实需要查询它的 orientation
,那么对于 处理程序 使用:
to getEmailFullScreenWindowScrollPosition()
tell application "System Events" to ¬
return value of ¬
first scroll bar of ¬
scroll area 1 of ¬
window 1 of ¬
process "Mail" whose ¬
orientation is ¬
"AXVerticalOrientation"
end getEmailFullScreenWindowScrollPosition
在测试 getEmailFullScreenWindowScrollPosition() handler 时,它会报告 1.0
当还有一些页面剩余但没有显示在屏幕上,所以我在第二个 repeat
loop 之后添加了额外的 code 行,以确保它全部被捕获。
测试电子邮件的内容到页面的最后,可能并不代表大多数电子邮件。如果您发现电子邮件末尾有大量白色 space,那么您可能需要删除第二个 repeat
[=] 之后的 code 行58=]循环:
key code 121
delay 1
key code 20 using {command down, shift down}
另请注意,除了 delay
命令 中的一个,我最终对所有命令都使用了 delay 1
,并且它在测试中对我来说效果很好,但是, 根据您的系统需要进行调整。
由于这只是作为概念验证呈现的,我会留给您调整代码以适应您的需求。
请注意,我不隶属于 Snagit 的开发人员,并且提到它是一个可能的选项,可以在单张截图。
注意:示例 AppleScript code 就是这样,没有包含任何 错误处理 不包含任何额外的 错误处理 可能是适当的。用户有责任根据需要或需要添加任何 错误处理 。查看 try statement and error statement in the AppleScript Language Guide. See also, Working with Errors. Additionally, the use of the delay 命令 在适当的事件之间可能是必要的,例如delay 0.5
,适当设置延迟的值。
我有一个 AppleScript 脚本来打开邮件应用程序,打开一封邮件,向下滚动并截取整个邮件正文。我通过使用 keystroke 121 (page down) 滚动页面截取屏幕截图。我不知道要重复循环多少时间才能到达页面末尾。无论如何我们可以使用 AppleScript 确定滚动是否已到达页面末尾?
这是我向下翻页和截图的代码片段:
open each_message
tell application "System Events"
delay 5
keystroke "f" using {command down, control down}
delay 3
key code 20 using {command down, shift down}
delay 5
key code 121
delay 5
key code 20 using {command down, shift down}
delay 5
keystroke "f" using {command down, control down}
end tell
这是一个处理程序,它 returns 在最前面的消息查看器中查看的消息的当前垂直滚动条位置。它对您打开了哪些面板以及查看器的方向做出了一系列假设,但它应该为您提供一些有用的东西:
set sp to my findMailScrollPosition()
on findMailScrollPosition()
tell application "System Events"
tell process "Mail"
tell window 1
tell splitter group 1
tell splitter group 1
tell scroll area 2
tell (first scroll bar whose orientation is "AXVerticalOrientation")
-- this will return a value between 0 and 1 (top to bottom)
return value
end tell
end tell
end tell
end tell
end tell
end tell
end tell
end findMailScrollPosition
阅读您的问题和随后的评论后...
Is there anyway we can identify if the scroll has reached the page end using AppleScript?
是
For some emails, it's fine. For some, it is duplicating the last page, for some, the screenshots are incomplete. (From comments under the question.)
这是不同长度文档的本质,按下 Page Down key,以及为什么可能使用 像 Snagit 这样的第三方应用程序 是更好的选择。
都说了...
下面显示的示例AppleScript代码在[=92=中进行了测试macOS Catalina 下的 ]Script Editor 以及 System Preferences 中的 Language & Region 设置到 英语(美国)— 小学 并为我工作没有问题1.
- 1 在 系统偏好设置 > 安全和隐私[= 中假定必要且适当的设置124=] > 隐私 已根据需要 set/addressed。
由于您没有包含脚本的完整代码,以下示例 AppleScript code 更像是一个 概念验证 。它也 may/will 有额外的屏幕截图,但是,它确保页面在开始截屏之前一直到顶部,并确保在适当停止的基础上也截取整个最后一页的屏幕截图。
出于测试目的,我首先打开了一封单独的电子邮件 window,假设 open each_message
位于 代码 你包含的是或代表。
示例 AppleScript 代码:
tell application "Mail" to activate
delay 1
tell application "System Events"
keystroke "f" using {command down, control down}
delay 1
repeat while my getEmailFullScreenWindowScrollPosition() > 0.0
key code 116
delay 0.1
end repeat
delay 1
repeat while my getEmailFullScreenWindowScrollPosition() < 1.0
key code 20 using {command down, shift down}
delay 1
key code 121
delay 1
end repeat
key code 121
delay 1
key code 20 using {command down, shift down}
delay 1
keystroke "f" using {command down, control down}
end tell
to getEmailFullScreenWindowScrollPosition()
tell application "System Events" to ¬
return value of ¬
scroll bar 1 of ¬
scroll area 1 of ¬
window 1 of ¬
process "Mail"
end getEmailFullScreenWindowScrollPosition
备注:
在 Mail 在 macOS Catalina 我的系统上没有 水平滚动条 当一个打开的电子邮件 window 处于 全屏 查看,以及为什么处理程序 指向 scroll bar 1
。但是,如果您确实需要查询它的 orientation
,那么对于 处理程序 使用:
to getEmailFullScreenWindowScrollPosition()
tell application "System Events" to ¬
return value of ¬
first scroll bar of ¬
scroll area 1 of ¬
window 1 of ¬
process "Mail" whose ¬
orientation is ¬
"AXVerticalOrientation"
end getEmailFullScreenWindowScrollPosition
在测试 getEmailFullScreenWindowScrollPosition() handler 时,它会报告 1.0
当还有一些页面剩余但没有显示在屏幕上,所以我在第二个 repeat
loop 之后添加了额外的 code 行,以确保它全部被捕获。
测试电子邮件的内容到页面的最后,可能并不代表大多数电子邮件。如果您发现电子邮件末尾有大量白色 space,那么您可能需要删除第二个 repeat
[=] 之后的 code 行58=]循环:
key code 121
delay 1
key code 20 using {command down, shift down}
另请注意,除了 delay
命令 中的一个,我最终对所有命令都使用了 delay 1
,并且它在测试中对我来说效果很好,但是, 根据您的系统需要进行调整。
由于这只是作为概念验证呈现的,我会留给您调整代码以适应您的需求。
请注意,我不隶属于 Snagit 的开发人员,并且提到它是一个可能的选项,可以在单张截图。
注意:示例 AppleScript code 就是这样,没有包含任何 错误处理 不包含任何额外的 错误处理 可能是适当的。用户有责任根据需要或需要添加任何 错误处理 。查看 try statement and error statement in the AppleScript Language Guide. See also, Working with Errors. Additionally, the use of the delay 命令 在适当的事件之间可能是必要的,例如delay 0.5
,适当设置延迟的值。