How do I use AppleScript to open a specific note from Apple's Notes application?
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 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
Solution 1:
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