笔记全部导出为PDF

时间:2019-11-03 23:14:08

标签: applescript

能够将macOS Notes.app中的所有Notes导出为PDF。

执行错误:注释出现错误: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

执行错误:注释出现错误:AppleEvent处理程序失败。 (-10000)

1 个答案:

答案 0 :(得分:0)

其中存在一些问题:

  1. 您已经定位到Notes流程,因此将其包含在 click语句添加了另一个流程目标-使用一个或另一个,但如果 您可能会使用常规菜单进行很多菜单单击 目的处理程序;
  2. 导出菜单项使用椭圆(单个字符),而不是三个 期;
  3. 通过在系统事件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