1

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
  • 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. – user3439894 Dec 2 at 21:02
  • @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. – Peter Schorn Dec 2 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. – user3439894 Dec 2 at 23:49
  • @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. – Peter Schorn Dec 3 at 0:31

You must log in to answer this question.

Browse other questions tagged .