1

I would like to get the id (or name) of the Apple Note (using the "notes" app on mac) that is currently in view (not a floated not, just the "selected" note), using either Applescript or JXA. Is that possible?

I know how to get the id of the most recently saved note with Applescript:

tell application "Notes"
  get id of note 0  //side note: I think saying "note 1" also gives same result--not sure of difference
end tell

But what about when you are looking at a note (so the note is in the foreground window), but it is not the last saved note. Is there a way to get the id of that note?

SeanRtS
  • 1,005
  • 1
  • 13
  • 31

1 Answers1

2

I've also found the following to work on macOS 11+. I got the answer based on this macscripter.net discussion, which suggests it works starting with macOS 10.15, but I am not able to verify at the moment earlier than macOS 11:

tell application "Notes"
    set noteID to «class seld» of (selection as record)
    set noteContainerID to «class seld» of ((container of note id noteID) as record)
    set selectedNoteName to name of note id noteID
    get selectedNoteName
end tell
SeanRtS
  • 1,005
  • 1
  • 13
  • 31
  • Tested and **does not** work in **macOS Mojave**, as `selection` is not part of the **AppleScript** _dictionary_ in **Notes** in this version of **macOS**. It probably will not work in other older versions as well for the same reason. – user3439894 Jul 13 '21 at 19:48
  • Thanks that is helpful to know. The macscripter.net source said it would work starting in 10.15, and it looks like that is the case. – SeanRtS Jul 13 '21 at 20:38
  • 1
    I'm marking this as the answer because it seems to be a working solution that works back to macOS 10.15. wch1zpink's pre-update answer is also good (and cleaner IMO), but seems like it doesn't work as far back as 10.15. – SeanRtS Jul 15 '21 at 14:24
  • Hi @user3439894: sometimes I find that this runs pretty slowly (takes a few seconds). Would you happen to know if there is a way to speed the apple script up? – SeanRtS Aug 20 '21 at 20:29