Safari - AppleScript 不会移动到 bookdown 在线书籍的下一部分
Safari - AppleScript does not move to the next section of a bookdown online book
我正在编写一个脚本,将 bookdown
online book 的一部分打印为 PDF,然后转到下一部分,依此类推。
打印部分有效(关键代码来自this page):
tell application "Safari"
activate
tell application "System Events"
key code 35 using command down -- activate print menu item
end tell
delay 0.5
set i to 0
repeat while i < 15
set i to i + 1
delay 0.1
tell application "System Events"
key code 48 -- press tab 15 times
end tell
end repeat
tell application "System Events"
key code 49 -- press space
end tell
set i to 0
repeat while i < 2
set i to i + 1
delay 0.1
tell application "System Events"
key code 125 -- press down key twice
end tell
end repeat
tell application "System Events"
key code 36 -- enter
end tell
set i to 0
repeat while i < 16
set i to i + 1
delay 0.1
tell application "System Events"
key code 125 -- press tab to get to "save"
end tell
end repeat
tell application "System Events"
key code 36 -- enter to cleck on save
end tell
end tell
问题
现在我已经打印了当前部分并回到了 Safari,我可以手动单击右箭头并移至下一部分,但我无法让脚本执行此操作。
我尝试将以下内容添加到上面的脚本中:
tell application "System Events"
key code 124 -- right arrow to enter the next page
end tell
甚至“重新打开”Safari,但没有任何反应。
tell application "Safari"
activate
tell application "System Events"
key code 124 -- right arrow to move to the next section
end tell
end tell
如何让 AppleScript“翻页”并转到下一部分?
此外,欢迎提出改进脚本的建议!我想知道避免重复“制表符”15 次是否容易。我查看了辅助功能检查器,发现打印菜单中的“PDF”对应于 NSPopUpButtonCell
。我曾尝试使用 select NSPopUpButtonCell of its sheet
但它没有用。
I can click manually on the right arrow and move to the next section, but I can't manage to have the script to do that.
How can I have AppleScript "turn the page" and move to the next section?
如果您尝试以编程方式单击 右箭头,如下图所示,则以下 示例 AppleScript code 可以做到:
tell application "Safari" to ¬
tell document 1 to ¬
do JavaScript ¬
"document.getElementsByClassName('fa fa-angle-right')[0].click();"
备注:
这需要 允许来自 Apple Events 的 JavaScript 来检查隐藏的 开发 菜单。
要取消隐藏隐藏的 开发 菜单:
- Safari > 首选项... > 高级 > [√]在菜单栏显示开发菜单
更新地址:
Also, I welcome suggestions to improve the script! I wonder if it would be easy to avoid repeating "tab" 15 times.
这是我如何使用上面的 JavaScript 并编码以避免使用 key code
和/或 keystroke
系统事件 命令,尤其是 tabbing UI.
示例 AppleScript 代码:
tell application "System Events"
tell application process "Safari"
set frontmost to true
set i to 0
repeat until (its frontmost = true)
delay 0.1
set i to i + 1
if i ≥ 20 then return
end repeat
click menu item "Print…" of ¬
menu 1 of ¬
menu bar item "File" of ¬
menu bar 1
tell its front window
set i to 0
repeat until (exists menu button "PDF" of sheet 1)
delay 0.1
set i to i + 1
if i ≥ 20 then return
end repeat
click menu button "PDF" of sheet 1
set i to 0
repeat until (exists menu item "Save as PDF" of ¬
menu 1 of menu button "PDF" of sheet 1)
delay 0.1
set i to i + 1
if i ≥ 20 then return
end repeat
click menu item "Save as PDF" of ¬
menu 1 of ¬
menu button "PDF" of ¬
sheet 1
set i to 0
repeat until (exists button "Save" of sheet 1 of sheet 1)
delay 0.1
set i to i + 1
if i ≥ 20 then return
end repeat
click button "Save" of sheet 1 of sheet 1
set i to 0
repeat while (exists sheet 1)
delay 0.1
set i to i + 1
if i ≥ 100 then return
end repeat
end tell
end tell
end tell
tell application "Safari" to ¬
tell document 1 to ¬
do JavaScript ¬
"document.getElementsByClassName('fa fa-angle-right')[0].click();"
备注:
由于这种 AppleScript script 使用 UI Scripting,我以 repeat
loop 的形式包含了一个 error handling 来为目标等待最多两秒 UI element
可用于大多数目标,但最后一个 repeat
循环 等待时间更长,因为它必须等到 另存为 PDF 完成。可以使用具有适当 value 的简单 delay
command,但在repeat
loops 它不应该等待超过需要的时间。换句话说,如果我想减慢 script 处理各种事件的速度,我只会使用简单的 delay
command UI。是否需要调整值得怀疑,但显然as/if有必要
如果达到 超时,脚本 会在该点终止,不会出现任何错误消息。单行 if i ≥ 20 then return
statements 可以变成完整的 if
block 并通过 显示对话框、显示警报或显示通知 命令作为通缉
我正在编写一个脚本,将 bookdown
online book 的一部分打印为 PDF,然后转到下一部分,依此类推。
打印部分有效(关键代码来自this page):
tell application "Safari"
activate
tell application "System Events"
key code 35 using command down -- activate print menu item
end tell
delay 0.5
set i to 0
repeat while i < 15
set i to i + 1
delay 0.1
tell application "System Events"
key code 48 -- press tab 15 times
end tell
end repeat
tell application "System Events"
key code 49 -- press space
end tell
set i to 0
repeat while i < 2
set i to i + 1
delay 0.1
tell application "System Events"
key code 125 -- press down key twice
end tell
end repeat
tell application "System Events"
key code 36 -- enter
end tell
set i to 0
repeat while i < 16
set i to i + 1
delay 0.1
tell application "System Events"
key code 125 -- press tab to get to "save"
end tell
end repeat
tell application "System Events"
key code 36 -- enter to cleck on save
end tell
end tell
问题
现在我已经打印了当前部分并回到了 Safari,我可以手动单击右箭头并移至下一部分,但我无法让脚本执行此操作。
我尝试将以下内容添加到上面的脚本中:
tell application "System Events"
key code 124 -- right arrow to enter the next page
end tell
甚至“重新打开”Safari,但没有任何反应。
tell application "Safari"
activate
tell application "System Events"
key code 124 -- right arrow to move to the next section
end tell
end tell
如何让 AppleScript“翻页”并转到下一部分?
此外,欢迎提出改进脚本的建议!我想知道避免重复“制表符”15 次是否容易。我查看了辅助功能检查器,发现打印菜单中的“PDF”对应于 NSPopUpButtonCell
。我曾尝试使用 select NSPopUpButtonCell of its sheet
但它没有用。
I can click manually on the right arrow and move to the next section, but I can't manage to have the script to do that.
How can I have AppleScript "turn the page" and move to the next section?
如果您尝试以编程方式单击 右箭头,如下图所示,则以下 示例 AppleScript code 可以做到:
tell application "Safari" to ¬
tell document 1 to ¬
do JavaScript ¬
"document.getElementsByClassName('fa fa-angle-right')[0].click();"
备注:
这需要 允许来自 Apple Events 的 JavaScript 来检查隐藏的 开发 菜单。
要取消隐藏隐藏的 开发 菜单:
- Safari > 首选项... > 高级 > [√]在菜单栏显示开发菜单
更新地址:
Also, I welcome suggestions to improve the script! I wonder if it would be easy to avoid repeating "tab" 15 times.
这是我如何使用上面的 JavaScript 并编码以避免使用 key code
和/或 keystroke
系统事件 命令,尤其是 tabbing UI.
示例 AppleScript 代码:
tell application "System Events"
tell application process "Safari"
set frontmost to true
set i to 0
repeat until (its frontmost = true)
delay 0.1
set i to i + 1
if i ≥ 20 then return
end repeat
click menu item "Print…" of ¬
menu 1 of ¬
menu bar item "File" of ¬
menu bar 1
tell its front window
set i to 0
repeat until (exists menu button "PDF" of sheet 1)
delay 0.1
set i to i + 1
if i ≥ 20 then return
end repeat
click menu button "PDF" of sheet 1
set i to 0
repeat until (exists menu item "Save as PDF" of ¬
menu 1 of menu button "PDF" of sheet 1)
delay 0.1
set i to i + 1
if i ≥ 20 then return
end repeat
click menu item "Save as PDF" of ¬
menu 1 of ¬
menu button "PDF" of ¬
sheet 1
set i to 0
repeat until (exists button "Save" of sheet 1 of sheet 1)
delay 0.1
set i to i + 1
if i ≥ 20 then return
end repeat
click button "Save" of sheet 1 of sheet 1
set i to 0
repeat while (exists sheet 1)
delay 0.1
set i to i + 1
if i ≥ 100 then return
end repeat
end tell
end tell
end tell
tell application "Safari" to ¬
tell document 1 to ¬
do JavaScript ¬
"document.getElementsByClassName('fa fa-angle-right')[0].click();"
备注:
由于这种 AppleScript script 使用 UI Scripting,我以 repeat
loop 的形式包含了一个 error handling 来为目标等待最多两秒 UI element
可用于大多数目标,但最后一个 repeat
循环 等待时间更长,因为它必须等到 另存为 PDF 完成。可以使用具有适当 value 的简单 delay
command,但在repeat
loops 它不应该等待超过需要的时间。换句话说,如果我想减慢 script 处理各种事件的速度,我只会使用简单的 delay
command UI。是否需要调整值得怀疑,但显然as/if有必要
如果达到 超时,脚本 会在该点终止,不会出现任何错误消息。单行 if i ≥ 20 then return
statements 可以变成完整的 if
block 并通过 显示对话框、显示警报或显示通知 命令作为通缉