10

I would like to create an AppleScript (or a command from Terminal) that opens a specific note from Apple's Notes application, preferably in a separate window. To be clear, I don't want to create a new note; I want to open an existing one. Is there a way to do this?

After the note is opened, one way to open it in a separate window might be to trigger the keyboard shortcut ⌘1 which will open the note in a separate window. Can I use AppleScript to trigger keyboard shortcuts?

Note that you can assign custom keyboard shortcuts to any item in an application's menu bar (see see this apple page for more info). I assigned ⌘1 to Window > Float Selected Note.

EDIT: I figured out how to do it with UI scripting, but I have to wait for each command to be executed individually, rather than the note opening in a separate window immediately. I would still prefer a method that is quicker, so consider this question still unanswered and provide any suggestions you have.

First, open the note and click the Add people button enter image description here and click on the title of your note. Then, click on copy link. ensure that the permission it set to Only people you invite can make changes. Finally, click Share. Don't worry; unless you start adding contacts, you haven't actually shared the note with anyone. Even if they have the link, other people can't access it unless they're signed in to your iCloud account.

Next, paste the following code into script editor and also paste your link in the section indicated below

set Time0ut to 10
set Timestamp to current date
set debug to false

#####################################################

if application "Notes" is running then quit application "Notes"

repeat until application "Notes" is not running

    if application "Notes" is running then quit application "Notes"


    if application "Notes" is running then
        if debug is true then say "Failed to quit"
        delay 1
    end if

    if (((current date) - Timestamp)) ≥ Time0ut then
        if debug is true then say "Timeout"
        return
    end if

end repeat

open location "https://www.icloud.com/notes/0TD6aphsUjK8vBscC6QgzWgXQ#Clipboard"

repeat until application "Notes" is running
    if (((current date) - Timestamp)) ≥ Time0ut then
        if debug is true then say "Timeout"
        return
    end if
end repeat

#####################################################
#####################################################
#####################################################




###########################
repeat 3 times

    if application "Notes" is not running then return

    try
        tell application "Notes" to activate
        menu_click({"Notes", "Window", "Float Selected Note"})
    end try
    delay 0.5

    if application "Notes" is not running then return

    if index of window "Notes" of application "Notes" is not 1 then
        exit repeat
    else
        if application "Notes" is not running then return
        if debug is true then say "Float Error"
        delay 1
        if application "Notes" is not running then return
    end if
end repeat
###########################



###########################
repeat 3 times

    if application "Notes" is not running then return

    try
        tell application "Notes" to activate
        tell application "Notes" to close (every window whose name is "Notes")
    end try

    if application "Notes" is not running then return

    if visible of window "Notes" of application "Notes" is false then
        exit repeat
    else
        if application "Notes" is not running then return
        if debug is true then say "Close Error"
        delay 1
        if application "Notes" is not running then return
    end if

end repeat
###########################



###########################
repeat 3 times

    if application "Notes" is not running then return

    try
        tell application "Notes" to activate
        set bounds of ((every window whose name is not "Notes") of application "Notes") to {0, 23, 540, 323}
    end try

    if application "Notes" is not running then return

    if (bounds of front window of application "Notes") is equal to {0, 23, 540, 323} then
        exit repeat
    else
        if application "Notes" is not running then return
        if debug is true then say "Resize Error"
        delay 1
        if application "Notes" is not running then return
    end if

end repeat
###########################




#####################################################
#####################################################
#####################################################

on menu_click(mList)
    local appName, topMenu, r
    if mList's length < 3 then error "Menu list is not long enough"
    set {appName, topMenu} to (items 1 through 2 of mList)
    set r to (items 3 through (mList's length) of mList)
    tell application "System Events" to my menu_click_recurse(r, ((process appName)'s ¬
        (menu bar 1)'s (menu bar item topMenu)'s (menu topMenu)))
end menu_click
on menu_click_recurse(mList, parentObject)
    local f, r
    set f to item 1 of mList
    if mList's length > 1 then set r to (items 2 through (mList's length) of mList)
    tell application "System Events"
        if mList's length is 1 then
            click parentObject's menu item f
        else
            my menu_click_recurse(r, (parentObject's (menu item f)'s (menu f)))
        end if
    end tell
end menu_click_recurse
5
  • In macOS Catalina, ⌘1 is a default keyboard shortcut in Notes for View > as List. Also, from what I can see Notes appears to only support one window, so opening it in a new windows doesn't appear possible. Commented Dec 2, 2019 at 21:02
  • 1
    @user3439894 You are correct that ⌘1 is the default keyboard shortcut in Notes for View > as List. However, you can assign a custom keyboard shortcut for any item in an application's menu bar commands. This includes creating a shortcut where none previously existed and replacing the default shortcuts with custom ones (see this apple page for more info. Also, you can open a note in a new window by selecting Window > Float Selected Note or by double clicking on the note in the sidebar. Commented Dec 2, 2019 at 23:21
  • I'm well aware of how to change a default keyboard shortcut or add one to a command on an App's menu if it doesn't exist as I posted answers on how to do that more then once. That said, I didn't take notice of the Notes > Window > Float Selected Note or knew about the double-click, so thanks for that; however, the AppleScript dictionary in Notes doesn't have verb to select a given note. So, in order to use the aforementioned command or double-click the target note must first be selected. How are you going to select it? UI Scripting maybe but not plain vanilla AppleScript. Commented Dec 2, 2019 at 23:49
  • 1
    @user3439894 Ok, I figured it out with UI scripting, but it's not pretty and I have to wait for each command to be executed individually, rather than the note opening in a separate window immediately. Check my original question, I'll put the solution there. Commented Dec 3, 2019 at 0:31
  • Have a look at Notes in Script Editor Window > Library > Notes
    – 0x1801ce
    Commented Dec 15, 2019 at 7:43

2 Answers 2

9

I cannot believe that it took me this long to figure out, but there's an incredibly simple way to do it:

tell application "Notes"
    tell account "iCloud"
        tell folder "Clipboard"
            show note 1
        end tell
    end tell
end tell

The notes are indexed according to their last edited date.

Here's how to open a note by title

tell application "Notes"
    show note "awesome note"
end tell

If you have multiple notes with the same title, specify which folder the note is in:

tell application "Notes"
    tell folder "Code"
        show note "awesome note"
    end tell
end tell

This even works for nested folders:

tell application "Notes"
    tell folder "Code"
        tell folder "Regex"
            show note "another note"
        end tell
    end tell
end tell
5
  • I would love to open a specific note, and when I run your script (thank you for that!), Script editor writes show id "x-coredata://85A2FF86-3623-44CD-9EB8-4FFEB310DACB/ICNote/p3264" of application "Notes" in a console. Could we somehow use this id to open a specific note? I tried show note "x-coredata://<id from above>" but this didn't work. Commented May 28, 2020 at 9:39
  • @TomášKafka You would think that it is possible to use the id to open a specific note, but I can't figure out how to do it. It's probably because of a bug. You can, however, open a note with a specific title. If multiple notes have the same title, the last edited one will be opened. You can get around this limitation by putting notes with the same title into different folders. See my updated answer. Commented May 28, 2020 at 22:59
  • Awesome, title is perfectly OK for me - thank you! Commented May 29, 2020 at 23:23
  • @PeterSchorn would you have any idea what's the same exact script for JXA? Commented Aug 14, 2022 at 4:31
  • @PeterSchorn I tried your example, but it simple selects that Note in the Notes app... it doesn't open it in a new window. (Maybe due to OS changes?) Do you have a current working solution? Commented Jul 10, 2023 at 14:48
0

I hope this isn't indiscreet, but you just have to add 'id'. This works:

tell application "Notes"
    show note id "x-coredata://DCE76A8C-ADAC-48D5-9783-30CD1BE73A78/ICNote/p21884"
end tell

Oddly, I'm having a bit of trouble re-wording this with the 'whose' syntax. This does NOT work:

tell application "Notes"
    show every note whose cell "id" = "x-coredata://DCE76A8C-ADAC-48D5-9783-30CD1BE73A78/ICNote/p21884"
end tell

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .