Get contents of Apple Notes and copy to clipboard

This macro makes a new task in 2Do using a URL.

At the end of the URL in the last action, it says "&note=xx". I need xx to be the contents of a note stored in Apple Notes.

To do this I was thinking I could copy the contents of the note to the clipboard and then use something like "&note=%Variable%Clipboard%" in the URL.

Is there a way to do this?

Copy edits.kmmacros (3.5 KB)

It's probably doable, but your macro needs some way to know which specific note to use, and will need to encode the text of the note for use in a URL (i.e. spaces become %20, etc.). Luckily, Notes is quite AppleScriptable, so it was relatively easy to cobble this together:

Download Macro(s): get Note to URL text.kmmacros (5.7 KB)

Macro screenshot

Macro notes
  • Macros are always disabled when imported into the Keyboard Maestro Editor.
    • The user must ensure the macro is enabled.
    • The user must also ensure the macro's parent macro-group is enabled.
System information
  • macOS 14.4.1
  • Keyboard Maestro v11.0.2

Here's a demo note I created in Notes:

And here's how that looks as a variable in Keyboard Maestro after selecting that note when running the above macro:

Demo%20Note%0A%0AThis%20is%20a%20note%20demonstrating%20how%20to%20get%20a%20note%27s%20contents%20into%20a%20URL%2Dsafe%20formatted%20variable%20in%20Keyboard%20Maestro%2E%0A%0AIt%20has%20no%20purpose%20beyond%20that%2E

The macro saves that to a variable named local_theNoteText, so you'd just need to add this at the end of your URL (replacing the 'xx'):

`...&note=%Variable%local_theNoteText%

Some caveats/notes about this macro:

  • I don't know how you want to select which note to use, so I asked ChatGPT to write an AppleScript to return a list of all note titles, formatted as title1|title2|etc, which is what creates a pop-up menu in Keyboard Maestro's Prompt for User Input action. You can replace or remove the first two steps if you wish. The second AppleScript is the one that actually retrieves the note.

  • If you do change the way in which you select the note to use, you'll need to modify that second script to reflect any changes to the variable name: Modify the line that begins set theNoteName... to reflect the new variable name, replacing Local_Which Note.

  • The note's title is included in the formatted URL. You may or may not want that. If you don't want it, you could use a regular expression search in Keyboard Maestro to remove anything up to and including the first %0A, which is the title. (Most notes have two of those in a row after the title, which you could also search for, but some don't, so I think ^.*?%0A[%0A]? would work to always select just the title and any following line feeds.

  • As disclosed above, ChatGPT wrote the first script, not me. Any errors are its fault, not mine :).

-rob.

Many thanks for taking time to do that.

The contents of my note is quite long and complex with bullet points etc. Assume that will tricky to encode as URL.

Let's say note is called 'Demo Note'. Is there anyway to find note called 'Demo Note', copy the note's contents to clipboard, then use the clipboard in the URL like this:

twodo://x-callback-url/add?task=Do%20edits&type=0&type=0&forlist=Reports&forParentName=%Variable%CurrentReport2Do%&note=clipboard

You can do all of that, but the problem is that it will break in the URL scheme: You have to encode everything within the note's body into URL-safe form in order for it to be processed with the URL. The entire contents of the note have to be in the URL, because that's the only place the data can be accessed.

Another method of doing what you want—though I've never used TwoDo so I don't know for sure—would be to use the URL to create the new to-do, then just use copy and paste actions in Keyboard Maestro to copy from Notes, switch to TwoDo, and paste into the new to-do. Would that work?

-rob.

As an alternative to @griffman's copy'n'paste suggestion -- do you actually need the contents of the Note is the TwoDo entry? Could you not just deep-link to the original Note instead?

If you do want to try and get the entire contents of the Note in the URL, try running KM's "Filter" action on the clipboard contents first, using the "Percent code for URL" option.

That's what the macro I provided does, so I don't know if he's tried it and it failed, or he hasn't tried it yet.

-rob.

1 Like

Apologies -- I should have looked closer at what you were doing.

To make amends, you can do your first 3 actions in a one-liner "Execute AppleScript" action if you don't mind the standard OS dialog:

tell application "Notes" to return plaintext of note (item 1 of (choose from list (get name of every note) with title "Pick a Note…" without multiple selections allowed and empty selection allowed))

It does suffer from the same problem -- referencing a Note by name when names aren't unique. It might be better to return a list of id:name pairs, munge those into a "friendly values" KM "Prompt with list" action, then grab the chosen Note by id. I guess a similar could be used for deep-links.

1 Like

I use the clipboard in a different 2Do URL. I run the URL in iOS Shortcuts. The URL copies the contents of a WhatsApp message and pastes it in 2Do. The issue for me is I don't know how to find then copy the contents of an Apple Note. I need AppleScript code that simply does this:

Find note 'Demo Note'
Copy contents of note to clipboard

I am not an AppleScript expert—by any stretch!—but I don't think you can use AppleScript to copy the note's visibly-formatted contents. To do that, you'd need to actually open Notes and find the note (using ⌘⌥F and entering the note's unique title, I guess). Once found, it looks like four presses of the Tab key will get to the note's body, where you can select the contents of the note (⌘A to select all), then copy it (⌘C).

In AppleScript, it seems possible to get either the unformatted text, as I did in the included macro above, or the HTML formatted text, which will produce what you see visually in an app that expects to receive HTML and display its output. For instance, here's the script to do that for my demo note:

set theNoteName to "Demo Note"

tell application "Notes"
	get body of note theNoteName
end tell

That returns this:

<div><h1>Demo Note</h1></div>
<div><br></div>
<div>This is a note demonstrating how to get a note's contents into a URL-safe formatted variable in Keyboard Maestro.</div>
<div><br></div>
<div>It has no purpose beyond that.</div>

`
Without being a 2Do user, or for that matter a Shortcuts user, I have no idea what you actually need to do to accomplish your task. But somewhere between the provided macro and the two methods described here, I think lies the answer.

Hope that helps;
-rob.

1 Like

If @griffman's suggestion of using HTML does what you want, include loading it on to the clipboard in the AppleScript:

tell application "Notes"
   set the clipboard to (get body of note "Demo Note")
end tell

If you do need the rich text(?) version you can open the correct note with

tell application "Notes"
	show note "Demo Note"
end tell

...which will also bring Notes to the front. You should then be able to use KM to get to the right pane so you can copy everything -- try a "Keystroke: Tab key" action until the "Paste" menu item is enabled, then "Select All" and "Copy".

1 Like