8bit avatar

PRACTICAL PARANOID

It's in that place where I put that thing that time


2 min | mac

Export Apple Notes to PDF via Apple Script

Pixelated scene from the movie Johnny Mnemonic with Johnny dialing into the internet through his VR visor

Apple don't make it easy to export and backup all your Notes, unless of course you want to export each note to PDF one by one. So I decided to write a quick and dirty Apple Script that will export all my Apple Notes to PDF in one go.

Before we can run the script, we will need to grant Assistive Access to the Apple Script Editor, this is because we will use accessibility automation to automatically click on the macOS interface to execture the various actions.

Head over to Systems Settings > Privacy & Security > Accessibility and enable Script Editor from the list. If you don’t see Script Editor in the list you can add it manually, using the plus button, or you can execute the script first (which will throw an error).

A screenshot of macOS Systems Settings showing the Script Editor permission toggle within the Accessibility submenu of the Privacy & Security section

Once done, open Script Editor and paste and execute the following script:

-- Path where the notes will be exported
set destinationPath to "~/Desktop/Notes"

-- We are storing the path into the clipboard
-- to fill in the path in the export modal
-- later on in the process
set the clipboard to destinationPath

-- We are creating the destination path
do shell script "mkdir -p " & destinationPath

tell application "Notes"
    -- Open the Notes app
	activate

    -- Create a list with every note
	set theNoteList to every note

    -- Loop through each note
	repeat with theNote in theNoteList
		delay 0.5
		-- Open the note
        show theNote
		
		tell application "System Events"
            -- Click on the Export to PDF
			click menu item "Export as PDF…" of menu "File" of menu bar 1 of application process "Notes"
			delay 0.5
			
            -- Set the destination path and export the note
			tell sheet 1 of window 1
				keystroke "G" using {command down, shift down}
				delay 0.5
				
				keystroke "v" using {command down}
				delay 0.5
				
				keystroke return
				delay 0.5
				
				keystroke return
				delay 0.5
			end tell
		end tell
		
	end repeat
end tell

As mentioned before, this is a quick and dirty script so no judgement on the cowboy-sh 🤠 delays please!

They are used to give each action enough time to execute. If you have very large notes in your library, that will take extra time to export, you may have to slightly increase the delay (1 sec will do).

That’s all folks 👋 once the script completes its run you will find all your Apple Notes exported to PDF in your destination folder 😀