applescript - 笔记全部导出为PDF
问题描述
能够将 macOS Notes.app 中的所有笔记导出为 PDF。
执行错误:Notes 出错:AppleEvent 处理程序失败。(-10000)
多个脚本,最新如下。
tell application "Notes"
activate
repeat with theFolder in every folder
repeat with theNote in every note of theFolder
tell application "System Events"
tell process "Notes"
set dockPrefs to dock preferences
set appearancePrefs to appearance preferences
delay 1
display dialog "Foo"
tell menu bar 1 of process "Notes"
click menu bar item "File"
click menu item "Export as PDF..." of menu "File" of menu bar of process "Notes"
end tell
click button "Save" of sheet 1 of window "Notes" of process "Notes"
delay 1
key code 125
end tell
end tell
end repeat
end repeat
end tell
执行错误:Notes 出错:AppleEvent 处理程序失败。(-10000)
解决方案
里面有几个问题:
- 您已经以 Notes 进程为目标,因此在 click 语句中包含该进程目标是添加另一个进程目标 - 使用其中一个,但如果您正在执行大量菜单点击,您可能会考虑使用通用处理程序;
- 导出菜单项使用椭圆(单个字符),而不是三个句点;
- 通过在 System Events tell 语句中放置一个
display dialog
语句,您将焦点从应用程序移开。
另请注意,已选择文本字段并且保存按钮是工作表中的默认按钮,因此您可以使用击键而不是尝试单击 UI 元素。一个清理过的示例(在 Mojave 中测试)看起来像:
tell application "Notes"
launch -- seems to work better than 'activate'
repeat with aFolder in folders
repeat with aNote in notes of aFolder
set noteName to (name of aNote)
try -- keep the name a reasonable length
set noteName to text 1 thru 20 of noteName
end try
tell (current date) to set timeStamp to text 2 thru -1 of (get (1000000 + (its hours) * 10000 + (its minutes) * 100 + (its seconds)) as text) -- hhmmss
tell application "System Events"
#display dialog noteName -- testing?
tell process "Notes"
set frontmost to true -- retarget the Notes app
delay 0.5
click menu item "Export as PDF…" of menu "File" of menu bar item "File" of menu bar 1
repeat until exists sheet 1 of window 1 -- wait for the sheet
delay 0.02
end repeat
end tell
keystroke noteName & "_" & timeStamp -- update the name, trying to avoid duplicates
delay 0.5
keystroke return -- dismiss the sheet
delay 0.5
key code 125
end tell
end repeat
end repeat
end tell
推荐阅读
- java - 元素未添加到 ArrayList Java - Android
- python - 将数据框插入具有不同列名的数据库表
- php - yii2 bootstrap 4 没有在 ajax 上突出显示错误
- javascript - Chrome 扩展缺少 Set-Cookie 标头
- amazon-rds - 带有 AWS RDS 的 Spring Cloud AWS
- python-3.x - 我正在尝试在 python 上制作一个票价计算器,此代码部分在打印部分之后不运行
- android - 如何在 Azure 构建管道中忽略 Android 构建变体?
- python - 将列的值与变量进行比较并创建新列
- android - 将主题设置为片段
- git - 开发人员提交时如何在我的服务器中触发