Customize script - help

Hi,

I’m in no way an expert or even a novice when it comes to apple script.
I know how to copy scripts and paste them and let them run hahaha

So searching for a way to export my notes from Notes app inside Catalina, I stumbled across this option:
https://ishouldgotosleep.com/how-to-backup-apple-notes-from-your-mac/

So far so good, but I wanted to customize it and here’s what I would like to change:
The original script asks me for a location to save the HTML files. I would like to set a default location and also have the script create a new folder inside that default location and rename it using a timestamp.

For example let’s say I have a folder on my Desktop called “Notes Backups”. So I wan’t the script to create a new folder inside “Notes Backups” and then rename that new folder using the time of when the script ran, for example it would be called “Mon-Dec20-10:54:11” (or something like that, whatever the format of the timestamp is). In the end I would end up with this:

-Desktop
- Notes Backups
- Mon-Dec20-10:54:11
-note1.html
-note2.html
-note3.html

Hope it makes sense and hope someone can give me a hand :slight_smile:

Here’s the code anyway:

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
        
        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
            
            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
            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

Really appreciate any help you guys can give me! :smiley:

Model: MacBook Pro 13" Mid 2012
AppleScript: 2.11
Browser: Safari 605.1.15
Operating System: macOS 10.15

If your script works without errors, then you just need to replace the 1st line of your script with the following:


set desktopFolder to (path to desktop folder) as text
set notesBackupsFolder to desktopFolder & "Notes Backups:"
set currentDate to (current date) as text

tell application "Finder"
	try
		folder notesBackupsFolder
	on error
		make new folder at desktop with properties {name:"Notes Backups"}
	end try
	set exportFolder to make new folder at folder notesBackupsFolder with properties {name:currentDate}
end tell

Or, choosing other root folder instead of desktop folder:


set rootFolder to (choose folder) as text
set notesBackupsFolder to rootFolder & "Notes Backups:"
set currentDate to (current date) as text

tell application "Finder"
	try
		folder notesBackupsFolder
	on error
		make new folder at folder rootFolder with properties {name:"Notes Backups"}
	end try
	set exportFolder to make new folder at folder notesBackupsFolder with properties {name:currentDate}
end tell

Hi!

Thanks for your time and help :slight_smile:

Unfortunately, it’s showing me an error, now that I tried both of your options.
Here’s what it shows me:

Maybe while you look at that issue, how can I define a specific folder on your second example without Finder asking me to pick a folder? Let’s say I want to include the path on the code so when I run the script it does everything automatically. For example, the path is this:
/Users/johndoe/Backups

Thank you :slight_smile:

I forgot to coerce exportFoder (Finder reference) to the text (that is, HFS path)


set rootFolder to "" & (path to home folder) & "Backups:" -- EDITED
set notesBackupsFolder to rootFolder & "Notes Backups:"
set currentDate to (current date) as text

tell application "Finder"
	try
		folder notesBackupsFolder
	on error
		make new folder at folder rootFolder with properties {name:"Notes Backups"}
	end try
	set exportFolder to make new folder at folder notesBackupsFolder with properties {name:currentDate}
	set exportFolder to exportFolder as text -- THIS, ADDED
end tell

Amazing! It works as expected!! :smiley:
Thank you SO MUCH for your time and help!

I wish you a great week and happy holidays!