Pages

Monday, December 18

Deleting Duplicate Notes in Notes.app using AppleScript

I found myself digging through my Notes.app the other day and, lo and behold, there was a whole bunch of duplicate notes hanging around. Pretty sure I goofed and imported them twice or something along those lines.

To fix the mess, I whipped up a quick AppleScript that zapped the duplicates just by looking at the note titles. Since the folder wasn't a big mess and I was dealing with a pretty neat pile, I was cool with the script going on a deleting spree. Just to be safe, though, I did a quick sweep through the "Recently Deleted" folder afterward to make sure nothing important got caught in the crossfire.

Use with caution:

tell application "Notes"

set targetFolder to "Target" -- Change this to your folder name

if not (exists folder targetFolder) then

display alert "Folder not found"

return

end if

set theNotes to notes of folder targetFolder

set noteNames to {}

set duplicates to {}

repeat with aNote in theNotes

set noteName to name of aNote

if noteNames contains noteName then

set end of duplicates to aNote

else

set end of noteNames to noteName

end if

end repeat

repeat with duplicateNote in duplicates

delete duplicateNote

end repeat

if length of duplicates is 0 then

display dialog "No duplicates found"

else

display dialog (length of duplicates) & " duplicates deleted."

end if

end tell




Please leave comments below.

No comments: