首页 > 解决方案 > 笔记全部导出为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)

标签: applescript

解决方案


里面有几个问题:

  1. 您已经以 Notes 进程为目标,因此在 click 语句中包含该进程目标是添加另一个进程目标 - 使用其中一个,但如果您正在执行大量菜单点击,您可能会考虑使用通用处理程序;
  2. 导出菜单项使用椭圆(单个字符),而不是三个句点;
  3. 通过在 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

推荐阅读