macOS Sierra - Get the details selected note in Notes.app?

Hoping someone can help me get a Notes.app applescript that worked under El Cap, but since Sierra (with iCloud), the following script no longer works:

tell application "System Events"
	tell process "Notes"
		set selectedRow to 1st row of table 1 of scroll area 1 of group 1 of group 1 of splitter group 1 of splitter group 1 of window 1 whose selected is true
		set rowName to name of UI element 1 of selectedRow
	end tell
end tell

rowName

Result: error “System Events got an error: Can’t get group 1 of group 1 of splitter group 1 of splitter group 1 of window 1 of process "Notes". Invalid index.” number -1719 from group 1 of group 1 of splitter group 1 of splitter group 1 of window 1 of process “Notes”

I got as far as figuring out with Accessibility Inspector that I might need to remove one “of group 1”.

tell application "System Events"
	tell process "Notes"
		set selectedRow to 1st row of table 1 of scroll area 1 of group 1 of splitter group 1 of splitter group 1 of window 1 whose selected is true
		set rowName to name of UI element 1 of selectedRow
	end tell
end tell

rowName

Result: missing value

FYI - Accessibility Inspector hierarchy is attached just in case.

You have an extraneous group 1 in our instruction.

tell application "System Events"
	tell process "Notes"
		--	set selectedRow to 1st row of table 1 of scroll area 1 of group 1 of group 1 of splitter group 1 of splitter group 1 of window 1 whose selected is true
		set selectedRow to 1st row of table 1 of scroll area 1 of group 1 of splitter group 1 of splitter group 1 of window 1 whose selected is true

		set rowItems to class of UI elements of selectedRow --> {UI element}
		set rowProperties to properties of UI element 1 of selectedRow --> {minimum value:missing value, orientation:missing value, position:{1165, 223}, class:UI element, role description:"cellule", accessibility description:missing value, focused:true, title:missing value, size:{300, 56}, value:missing value, help:missing value, enabled:true, maximum value:missing value, role:"AXCell", entire contents:{}, subrole:missing value, selected:true, name:missing value, description:"cellule"}
		set rowName to name of UI element 1 of selectedRow --> missing value
	end tell
end tell

rowName

Yvan KOENIG running Sierra 10.12.1 in French (VALLAURIS, France) vendredi 9 décembre 2016 17:49:48

Actually, that extra group 1 was required to make things work for me in El Capitan (using OS X Server’s notes account). That same script just fails under Sierra.

Taking your new script, I used the open this script in editor link, ran it, still “missing value”.

Not sure if it matters, but I use the Notes.app in the three pane config: left side account/folders, middle are note titles, right side are the notes. I never pop-out a note; I edit it in the third pane. And I did confirm that everything looks OK in the app, I have a note selected, etc.

Thank you,

John

Here Notes display the described three panes too but - happily - hiding the folders pane changes nothing to its behavior.

I took care to extract the entire set of properties of the targeted object - a cell.
The property name as well as the property value are set to missing value.
I would be surprised if asking only for the name returned a different thing.

What I don’t understand is how you missed the extraneous group 1 as you posted the hierarchy returned by the Accessibility Inspector which was correct - with a single group in it.

Yvan KOENIG running Sierra 10.12.1 in French (VALLAURIS, France) samedi 10 décembre 2016 10:09:25

I took your script as is and I’m still getting “missing value”. I’ve attached a screenshot showing the script, result, and also that I have GUI scripting enabled as well as that I actually have a note selected. I hope you can help me identify what is wrong.

It seems that my English was not clear enough.

Everything behave correctly.
The targeted UI element HASN’T a name.

Reading carefully what is displayed by Accessibility Inspector would have helped you to understand.
Everything is available in what you posted.

The script below was tested with 10.9.5, 10.10.5, 10.11.5 and 10.12.1

tell application "System Events"
	tell process "Notes"
		set sys2 to (system attribute "sys2")
		if sys2 > 10 then
			# code used by 10.11.y and 10.12.z
			set selectedRow to 1st row of table 1 of scroll area 1 of group 1 of splitter group 1 of splitter group 1 of window 1 whose selected is true
			
			set rowItems to class of UI elements of selectedRow --> {UI element}
			set rowProperties to properties of UI element 1 of selectedRow --> {minimum value:missing value, orientation:missing value, position:{1165, 223}, class:UI element, role description:"cellule", accessibility description:missing value, focused:true, title:missing value, size:{300, 56}, value:missing value, help:missing value, enabled:true, maximum value:missing value, role:"AXCell", entire contents:{}, subrole:missing value, selected:true, name:missing value, description:"cellule"}
			tell UI element 1 of selectedRow
				set rowName to its name --> missing value
				class of UI elements --> {image, image, static text, static text, static text}
				set theNames to name of static texts --> {"azerty uiop", "Tagada Tsoin Tsoin", "18/09/2015"}
			end tell
		else if sys2 > 8 then
			# code used by 10.9.w and 10.10.x
			set selectedRow to 1st row of table 1 of scroll area 1 of group 1 of splitter group 1 of window 1 whose selected is true
			set rowItems to class of UI elements of selectedRow --> {UI element}
			set rowProperties to properties of UI element 1 of selectedRow --> {minimum value:missing value, orientation:missing value, position:{1165, 223}, class:UI element, role description:"cellule", accessibility description:missing value, focused:true, title:missing value, size:{300, 56}, value:missing value, help:missing value, enabled:true, maximum value:missing value, role:"AXCell", entire contents:{}, subrole:missing value, selected:true, name:missing value, description:"cellule"}
			set rowName to name of UI element 1 of selectedRow
		else # Here is the code which YOU posted and which DOESN'T apply for 10.9.w, 10.10.x, 10.11.y and 10.12.z
			set selectedRow to 1st row of table 1 of scroll area 1 of group 1 of group 1 of splitter group 1 of splitter group 1 of window 1 whose selected is true
			set rowItems to class of UI elements of selectedRow --> {UI element}
			set rowProperties to properties of UI element 1 of selectedRow --> {minimum value:missing value, orientation:missing value, position:{1165, 223}, class:UI element, role description:"cellule", accessibility description:missing value, focused:true, title:missing value, size:{300, 56}, value:missing value, help:missing value, enabled:true, maximum value:missing value, role:"AXCell", entire contents:{}, subrole:missing value, selected:true, name:missing value, description:"cellule"}
			set rowName to name of UI element 1 of selectedRow
		end if
	end tell
end tell

I guess that on your machine, theNames will be[format] {“Retrain "hey Siri"”, “No additional text”, “16:28”}[/format]

Yvan KOENIG running Sierra 10.12.1 in French (VALLAURIS, France) samedi 10 décembre 2016 15:35:35

As I am curious I made test in 4 systems versions.

10.11 and 10.12 behave the same way.

Both 10.10 and 10.9 use the same other hierarchy.

None of them use what you gave as running with El Capitan.

I edited the script in message #6 to match what I got.

Yvan KOENIG running Sierra 10.12.1 in French (VALLAURIS, France) lundi 12 décembre 2016 14:30:29

Yvan - I’m not sure what to say. This section of my overall script was stored away in a Keyboard Maestro macro that worked fine for quite a while until it didn’t. But, after removing the superfluous “of group 1”, I can access the note elements. But now I have a new problem. This full script…

tell application "System Events"
	tell process "Notes"
		set selectedRow to 1st row of table 1 of scroll area 1 of group 1 of splitter group 1 of splitter group 1 of window 1 whose selected is true
		set rowName to name of UI element 1 of selectedRow
	end tell
end tell

tell application "Notes"
	set selectedNote to note rowName
	set theDate to creation date of selectedNote
end tell
-- Convert a date to YYYY-MM-DD HHMMSS
on convertDate(dateToConvert)
	set y to text -4 thru -1 of ("0000" & (year of dateToConvert))
	set m to text -2 thru -1 of ("00" & ((month of dateToConvert) as integer))
	set d to text -2 thru -1 of ("00" & (day of dateToConvert))
	set h to text -2 thru -1 of ("00" & (hours of dateToConvert))
	set min to text -2 thru -1 of ("00" & (minutes of dateToConvert))
	set s to text -2 thru -1 of ("00" & (seconds of dateToConvert))
	return y & "-" & m & "-" & d & " " & h & min & s
end convertDate

set NoteDate to convertDate(theDate) & " "

…still fails in Sierra with the error:

error "Notes got an error: Can't make missing value into type integer." number -1700 from missing value to integer

Nothing to say, just read carefully what I posted.

Under Sierra the variable rowName IS always set to missing value.
To get meaningful values you MUST enter deeper in the GUI structure.
The needed code is available in message #6
Under 10.9 and 10.10 it returns a meaningful rowName.
Under 10.11 and 10.12, it sets rowName to missing value and return the meaningful datas in theNames
So, at the end of my script ou may get wanted value thru these instructions :


# code from message #6
# added code

if rowName is missing value then
	# we are running 10.11 or 10.12
	set altDate to date (theNames's item 3)
	set noteName to theNames's item 1
	set isModern to true
else
	# here we are running 10.9 or 10.10
	copy rowName to noteName
	set isModern to false
end if
tell application "Notes"
	set selectedNote to note noteName
	set theDate to creation date of selectedNote
	set modDate to modification date of selectedNote
end tell
set NoteDate to convertDate(theDate)
set modDate to convertDate(modDate)
if isModern then
	set altDate to convertDate(altDate)
else
	copy NoteDate to altDate
end if
{NoteDate, modDate, altDate}

--> {"2012-10-02 222017", "2015-09-18 145828", "2015-09-18 000000"}

-- Convert a date to YYYY-MM-DD HHMMSS
on convertDate(dateToConvert)
	tell dateToConvert
		set y to text -4 thru -1 of ((10000 + year) as text)
		set m to text -2 thru -1 of ((100 + (its month as integer)) as text)
		set d to text -2 thru -1 of ((100 + day) as text)
		set h to text -2 thru -1 of ((100 + hours) as text)
		set min to text -2 thru -1 of ((100 + minutes) as text)
		set s to text -2 thru -1 of ((100 + (its seconds)) as text)
	end tell
	return y & "-" & m & "-" & d & " " & h & min & s
end convertDate

Yvan KOENIG running Sierra 10.12.2 in French (VALLAURIS, France) mardi 20 décembre 2016 09:24:36

Thank you (again) for helping me advance my script, Yvan.

-John

AppleScript: macOS 10.12.1
Browser: Safari 602.2.14
Operating System: Other

No need GUI scripting to get properties of selected note in the Notes.app:


tell application "Notes"
	-- get id of selected note
	set noteID to «class seld» of (selection as record)
	-- get id of container of selected note
	set noteContainerID to «class seld» of ((container of note id noteID) as record)
	-- get name of selected note
	set selectedNoteName to name of note id noteID
	-- get name of folder of selected note
	set selectedNoteFolderName to name of folder id noteContainerID
	-- get creation date of selected note
	set creationDate to creation date of note id noteID
	return {|creation date|:my convertDate(creationDate) & " "}
	-- get all properties of selected note:
	-- return properties of note id noteID
end tell

on convertDate(dateToConvert)
	set y to text -4 thru -1 of ("0000" & (year of dateToConvert))
	set m to text -2 thru -1 of ("00" & ((month of dateToConvert) as integer))
	set d to text -2 thru -1 of ("00" & (day of dateToConvert))
	set h to text -2 thru -1 of ("00" & (hours of dateToConvert))
	set min to text -2 thru -1 of ("00" & (minutes of dateToConvert))
	set s to text -2 thru -1 of ("00" & (seconds of dateToConvert))
	return y & "-" & m & "-" & d & " " & h & min & s
end convertDate