Duplicate and Rename an Apple Note?

Is there a Keyboard Maestro macro or script that reliably duplicates and renames an Apple note?

I have a note that is a template for some reoccurring tasks. I want to craft a new one on demand with a new title. I have a macro that I execute which does this. The steps to duplicate the note (after Apple Notes is brought to the front) under the macro is:

Find and Duplicate Note

I have not found an accurate way to identify, select, and rename it though. Apple Notes does not have keyboard shortcuts to navigate between its panels (that I have found). I haven't found a solution via AppleScript (e.g. "duplicate note "TemplateNote" at folder "SomeFolder" with properties {name:"New Title"...") and Shortcuts either. I'm currently trying "find and click image", which has proven unreliable.

I'd appreciate learning what you'd recommend solving this using a simple, reliable way.

1 Like

This should work for you...

-Chris

AppleScript (click to expand/collapse)
------------------------------------------------------------
# Author   : Chris Thomerson (@cdthomer)
#
# Version  : 1.0.0 (Initial script)
# History  : 1.0.0 (Initial script)
#
# Created  : Friday, February 10, 2023
# Modified : Friday, February 10, 2023
# macOS    : 12.6.3 (Monterey)
# Tags     : Notes
#
# PURPOSE
# Duplicate a note
#
# DISCLAIMER
# Permission to use, copy, modify, and/or distribute this
# software for any purpose with or without fee is hereby
# granted.
#
# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS
# ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO
# EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
# INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
# WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
# TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE
# USE OR PERFORMANCE OF THIS SOFTWARE.
------------------------------------------------------------

# set template title to variable
set noteToDuplicate to ".PROJECT TEMPLATE"

# prompt for new title
set newTitle to the text returned of (display dialog ¬
	"Insert your new title below." buttons {"Continue", "Cancel"} ¬
	default button 1 cancel button 2 ¬
	default answer "" with title "New title" with icon note)

tell application "Notes"
	
	# set to variable the content of the note to be duplicated
	tell (first note whose name = noteToDuplicate)
		set templateBody to its body
	end tell
	
	# prepend to variable the new title
	set newNoteBody to newTitle & templateBody
	
	# create new note with new title and template content
	make note with properties {body:newNoteBody}
	
end tell
DISCLAIMER (click to expand/collapse)

I put this together in about 3 minutes, and while it works for me, I have never actually used AppleScript for Apple Notes before... so I can’t guarantee it will work for you, or will continue to work in the future. :sweat_smile:

3 Likes

Thank you. This works @cdthomer ! I'll need to play with the title formatting a little as it presents like normal text, but this is perfect :slight_smile:

1 Like

The trick in AppleScript is that when you create a thing the result is a reference to that new thing -- you can store that reference in a variable and use it later on.

Unfortunately it looks like you can't duplicate a note with AppleScript. But here's a variant on Chris's script that retains the entire body of the template note, renaming the new one but without changing the first line. As written it assumes your template is in a folder called "Test Folder" and puts the new note in the same place. No whizzy dialog like Chris provides but it's easy enough to add one, or feed a variable in from KM:

tell application "Notes"
	set newNote to make new note at folder "Test Folder" with properties {body:body of note ".PROJECT TEMPLATE" of folder "Test Folder"}
	set name of newNote to "My New Note"
end tell

Don't take that fact entirely for granted – on occasion the convention is not followed by some errant developer.