Script to remove/find&replace multiple words from all notes

Hi,
Hope someone can provide a script for this.
Been on this now for like 2 days,I have searched through many potential scripts and tried some but with no luck so far.

Say I have the words “Bli” and “Blu” and I would like to remove those words, or replace them with “”, in all notes. What would the script be to make that happen? And if possible as an option, all notes in a specific folder.

Hope someone can provide a solution to this.
Thanks upfront.

Hello, ttester14,

On the Mac 10.15 or later you can replace the body of notes of chosen folder this way:


use AppleScript version "2.4" -- Yosemite (10.10) or later
use framework "Foundation"
use scripting additions

tell application "Notes"
	-- Determine the chosen folder
	-- important: `selection` property not compatible before MacOS 10.15
	set noteID to «class seld» of (selection as record)
	set noteContainerID to «class seld» of ((container of note id noteID) as record)
	--  repeat loop to clean the rich content body of each note
	repeat with nextNote in (notes of folder id noteContainerID)
		set theBody to body of nextNote
		set body of nextNote to my findReplace({"Bli", "Blu"}, "", theBody)
	end repeat
end tell

on findReplace(findText, replaceText, sourceText)
	set findText to current application's NSArray's arrayWithArray:findText
	set sortedList to findText's sortedArrayUsingDescriptors:{current application's NSSortDescriptor's sortDescriptorWithKey:"length" ascending:false}
	set findText to sortedList as list
	set ASTID to AppleScript's text item delimiters
	set AppleScript's text item delimiters to findText
	set sourceText to text items of sourceText
	set AppleScript's text item delimiters to replaceText
	set sourceText to "" & sourceText
	set AppleScript's text item delimiters to ASTID
	return sourceText
end findReplace

:slight_smile: Thanks, definitely heading in the right direction :slight_smile:

Here’s my situation, I was fixing someone’s laptop and exported all the notes as txt files.
They contain html as notes formats in html apparently.
When importing the files they are showing all the html code instead of formatting the notes like they were. E.g. plenty of divs, all headers are

&

, etc.
So I am okay for the notes no to have any formatting as I really haven’t found away to import them and keep the formatting. At the moment when I replace the suggested words with
and
it automatically changes it to div and /div and I ended up with a lot of <><><><><> as everything in between was replaced by the “”.

I did try playing around with it several times, deleting all the notes and importing again, modifying the script and running it again, so can continue doing that till I have a working script.

So 2 questions which should make it work:

  1. how would it work if I would like to remove e.g.

    &
    , or just < and > (tried putting them it as “<>” but no luck)

  2. how can I keep the line and paragraph breaks?
    When I ran the snipped it removed whatever was in the array, except for the < and > as mentioned above and all enters and paragraph breaks were removed. So everything is now one continuous text.
    Would be great to keep those breaks.

Thanks again!
Appreciated

The approach you used to save and restore notes is completely destructive to them. And this is because Notes.app stores its notes not as a text files, but as a database - the NoteStore.sqlite file located in the ~/Library/Group Containers/group.com.apple.notes/ folder.

You should carefully read the Where Notes Data is Stored Locally on Mac (clickable) in order to avoid wrong actions for backing up notes.