I wish to return the name of the folder of the selected note in Notes.app (in Catalina and above). Below is script that works to return the name of the note, and a modified version that attempts - and fails - to return the name of the containing folder. How can the latter script be modified so that it successfully returns the name of the selected note’s containing folder?
The following script does quite a good job of return the name of the note.
[AppleScript]
tell application “Notes”
if (count of windows) is equal to 1 or (name of window 1 as string is equal to “Notes”) then
set notes_path to path to application id “com.apple.Notes”
set shell_script to "defaults read " & POSIX path of notes_path & “/Contents/version.plist ‘CFBundleShortVersionString’”
if “4.7” is less than or equal to (do shell script shell_script) then – selection
property not compatible before MacOS 10.15
tell application id “com.apple.Notes”
if (count of selection) is equal to 1 then
set noteID to «class seld» of (selection as record)
return name of note id noteID
else
– multiple notes selected
return “”
end if
end tell
end if
tell application “System Events”
tell process “Notes”
click menu item “Float Selected Note” of menu “Window” of menu bar 1
end tell
end tell
delay 0.05
end if
get name of window 1 as string
end tell
[/AppleScript]
If I change the line, “return name of note id noteID” to “return container of note id noteID” I get a long string:
ldap server id “x-coredata://05E4ABB2-6DC8-4B9C-AF79-983A45199930/ICFolder/p8693” of application “Notes”
I am attempting to get the name of the folder, like this: ??? Agenda
Here’s the entire, modified script.
[AppleScript]
tell application “Notes”
if (count of windows) is equal to 1 or (name of window 1 as string is equal to “Notes”) then
set notes_path to path to application id “com.apple.Notes”
set shell_script to "defaults read " & POSIX path of notes_path & “/Contents/version.plist ‘CFBundleShortVersionString’”
if “4.7” is less than or equal to (do shell script shell_script) then – selection
property not compatible before MacOS 10.15
tell application id “com.apple.Notes”
if (count of selection) is equal to 1 then
set noteID to «class seld» of (selection as record)
return container of note id noteID
else
– multiple notes selected
return “”
end if
end tell
end if
tell application “System Events”
tell process “Notes”
click menu item “Float Selected Note” of menu “Window” of menu bar 1
end tell
end tell
delay 0.05
end if
get name of window 1 as string
end tell
[/AppleScript]
It is possible:
tell application "Notes"
if ((count of windows) = 1) or (name of window 1 is "Notes") then
if "4.7" ≤ its version then -- `selection` property not compatible before MacOS 10.15
if (count of selection) = 1 then
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
set selectedNoteFolderName to name of (first folder whose id is noteContainerID)
else
-- multiple notes selected
return ""
end if
end if
--tell application "System Events" to tell application process "Notes"
--click menu item "Float Selected Note" of menu "Window" of menu bar 1
--end tell
--delay 0.05
end if
-- get name of window 1 as string
end tell
return {note_Name:selectedNoteName, folder_Name:selectedNoteFolderName}
Thanks again, @KniazidisR
I tweaked your script slightly to fit into my workflow.
tell application "Notes"
if (count of windows) is equal to 1 or (name of window 1 as string is equal to "Notes") then
set notes_path to path to application id "com.apple.Notes"
set shell_script to "defaults read " & POSIX path of notes_path & "/Contents/version.plist 'CFBundleShortVersionString'"
if "4.7" is less than or equal to (do shell script shell_script) then -- `selection` property not compatible before MacOS 10.15
tell application id "com.apple.Notes"
if (count of selection) is equal to 1 then
set noteID to «class seld» of (selection as record)
set noteContainerID to «class seld» of ((container of note id noteID) as record)
return name of (first folder whose id is noteContainerID)
else
-- multiple notes selected
return ""
end if
end tell
end if
tell application "System Events"
tell process "Notes"
click menu item "Float Selected Note" of menu "Window" of menu bar 1
end tell
end tell
delay 0.05
end if
get name of window 1 as string
end tell
Here I looked a little closer at my last script and thought: whose clause is not needed at all:
tell application "Notes"
if ((count of windows) = 1) or (name of window 1 is "Notes") then
if "4.7" ≤ its version then -- `selection` property not compatible before MacOS 10.15
if (count of selection) = 1 then
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
set selectedNoteFolderName to name of folder id noteContainerID
else
-- multiple notes selected
return ""
end if
end if
--tell application "System Events" to tell application process "Notes"
--click menu item "Float Selected Note" of menu "Window" of menu bar 1
--end tell
--delay 0.05
end if
-- get name of window 1 as string
end tell
return {note_Name:selectedNoteName, folder_Name:selectedNoteFolderName}