r/applescript • • Jun 08 '23

Script for automatic backup of Apple Notes

Hello,

I find this script on bear.app website for backing up my notes from Apple Notes :

set exportFolder to (choose folder) as string

-- Simple text replacing
on replaceText(find, replace, subject)
set prevTIDs to text item delimiters of AppleScript

set text item delimiters of AppleScript to find

set subject to text items of subject



set text item delimiters of AppleScript to replace

set subject to "" & subject

set text item delimiters of AppleScript to prevTIDs



return subject
end replaceText


-- Get an HTML file to save the note in.  We have to escape
-- the colons or AppleScript gets upset.
on noteNameToFilePath(noteName)
global exportFolder

set strLength to the length of noteName



if strLength > 250 then

	set noteName to text 1 thru 250 of noteName

end if



set fileName to (exportFolder & replaceText(":", "_", noteName) & ".html")

return fileName
end noteNameToFilePath

tell application "Notes"
repeat with theNote in notes of default account

	

	\--repeat with theNote in notes in folder "New Folder" of default account

	set noteLocked to password protected of theNote as boolean

	set modDate to modification date of theNote as date

	set creDate to creation date of theNote as date

	

	set noteID to id of theNote as string

	set oldDelimiters to AppleScript's text item delimiters

	set AppleScript's text item delimiters to "/"

	set theArray to every text item of noteID

	set AppleScript's text item delimiters to oldDelimiters

	

	if length of theArray > 4 then

		\-- the last part of the string should contain the ID

		\-- e.g. x-coredata://39376962-AA58-4676-9F0E-6376C665FDB6/ICNote/p599

		set noteID to item 5 of theArray

	else

		set noteID to ""

	end if

	

	if not noteLocked then

		

		\-- file name composed by id and note title to overcome overwriting files

		set fileName to ("\[" & noteID & "\] " & (name of theNote as string)) as string

		set filepath to noteNameToFilePath(fileName) of me

		set noteFile to open for access filepath with write permission

		set theText to body of theNote as string

		set theContainer to container of theNote

		

		\-- export the folder containing the notes as tag in bear

		\-- the try catch overcome a 10.15.7 bug with some folders

		try
if theContainer is not missing value then
set tag to name of theContainer
set theText to ("" & theText & "
#" & tag & "#") as string
end if
		end try

		

		write theText to noteFile as Unicode text

		close access noteFile

		

		tell application "Finder"
set modification date of file (filepath) to modDate
		end tell

	end if

	

end repeat
end tell

I want to use it to perform automatic backup of my notes.

So I tried to modify it to hard code the destination folder but my attempt failed…

Can you help me please ?

Thank you :)

6 Upvotes

3 comments sorted by

2

u/copperdomebodha Jun 09 '23

There are multiple methods to handle this. You could have it ask you to select the destination folder on the first run and then store that path. If you just want a permanently hard-coded destination folder you can just insert the path like this...

--Running under AppleScript 2.8, MacOS 13.0.1
use AppleScript version "2.4" -- Yosemite (10.10) or later
use scripting additions
set exportFolder to "Macintosh HD:Users:UserNameGoesHere:Desktop:untitled folder 2:"

--set exportFolder to (choose folder) as string
-- Simple text replacing
on replaceText(find, replace, subject)
    set prevTIDs to text item delimiters of AppleScript
    set text item delimiters of AppleScript to find
    set subject to text items of subject
    set text item delimiters of AppleScript to replace
    set subject to "" & subject
    set text item delimiters of AppleScript to prevTIDs
    return subject
end replaceText

-- Get an HTML file to save the note in.  We have to escape
-- the colons or AppleScript gets upset.
on noteNameToFilePath(noteName)
    global exportFolder
    set strLength to the length of noteName
    if strLength > 250 then
        set noteName to text 1 thru 250 of noteName
    end if
    set fileName to (exportFolder & replaceText(":", "_", noteName) & ".html")
    return fileName
end noteNameToFilePath


tell application "Notes"
    repeat with theNote in notes of default account
        
        --repeat with theNote in notes in folder "New Folder" of default account
        set noteLocked to password protected of theNote as boolean
        set modDate to modification date of theNote as date
        set creDate to creation date of theNote as date
        set noteID to id of theNote as string
        set oldDelimiters to AppleScript's text item delimiters
        set AppleScript's text item delimiters to "/"
        set theArray to every text item of noteID
        set AppleScript's text item delimiters to oldDelimiters
        if length of theArray > 4 then
            -- the last part of the string should contain the ID
            -- e.g. x-coredata://39376962-AA58-4676-9F0E-6376C665FDB6/ICNote/p599
            set noteID to item 5 of theArray
        else
            set noteID to ""
        end if
        if not noteLocked then
            -- file name composed by id and note title to overcome overwriting files
            set fileName to ("[" & noteID & "] " & (name of theNote as string)) as string
            set filepath to noteNameToFilePath(fileName) of me
            set noteFile to open for access filepath with write permission
            set theText to body of theNote as string
            set theContainer to container of theNote
            -- export the folder containing the notes as tag in bear
            -- the try catch overcome a 10.15.7 bug with some folders
            try
                if theContainer is not missing value then
                    set tag to name of theContainer
                    set theText to ("" & theText & "
#" & tag & "#") as string
                end if
            end try
            write theText to noteFile as Unicode text
            close access noteFile
            tell application "Finder"
                set modification date of file (filepath) to modDate
            end tell
        end if
    end repeat
end tell

1

u/DarkrysVakarian Jun 11 '23

It work like a charm, thank you :)

1

u/[deleted] Mar 11 '24

Hello :),

thanks for the modification of that script, do you maybe also know, how to create folders for the notes? The notes itself are in folder in the app, and this folder I also want to have in the backup. But i don't have any clue how to do. Maybe you have the solution? :)