Thanks for your discovery and terrific job!
I have tried so hard in the past 48h to modify your script so that I could programmatically also add some text/body in the note (together with the attachment). I also struggled immensely to have the note created in a desired subfolder.
Whenever I tried to "add" a body to the newly created note, Notes.app was basically overwriting the entire note, including the attachment.
At some point I discovered the version Ethan Schoonover authored as a "Folder Action" (see https://youtu.be/KrVcf2nN0b8, and his GitHub repo https://github.com/altercation/apple-notes-inbox). It works almost with no adaptation as a Print Plugin workflow!
This is finally the version I made, with a very minor addition (i.e. the user is prompted to specify a different Note title). I share it here with you and the Internet, hoping it might be a useful starting point for posterity.
-- This script is designed to be used in Automator to create a new note in the Notes app
-- It takes a PDF file as input, prompts the user for a note title, and creates a
-- new note with the PDF attached.
-- The note will be created in a specified folder within the Notes app.
-- The script also includes a timestamp and the original filename in the note body.
-- The script assumes the Notes app is available and the specified folder exists.
-- Note: This script is intended to be run in the context of Automator with a file input (e.g. Print Plugins or as Folder Action).
-- Heavily based on the code from: https://github.com/altercation/apple-notes-inbox
property notePrefix : ""
property notesFolder : "Resources"
on run {fileToProcess, parameters}
try
set theFile to fileToProcess as text
tell application "Finder" to set noteName to name of file theFile
-- Ask the user for a title and tags for the new note
set noteTitleDialog to display dialog "Note title:" default answer noteName
set noteTitle to text returned of noteTitleDialog
set timeStamp to short date string of (current date) as string
set noteBody to "<body><h1>" & notePrefix & noteTitle & "</h1><br><br><p><b>Filename:</b> <i>" & noteName & "</i></p><br><p><b>Automatically Imported on:</b> <i>" & timeStamp & "</i></p><br></body>"
tell application "Notes"
if not (exists folder notesFolder) then
make new folder with properties {name:notesFolder}
end if
set newNote to make note at folder notesFolder with properties {body:noteBody}
make new attachment at end of attachments of newNote with data (file theFile)
(*
Note: the following delete is a workaround because creating the attachment
apparently creates TWO attachements, the first being a sort of "ghost" attachment
of the second, real attachment. The ghost attachment shows up as a large empty
whitespace placeholder the same size as a PDF page in the document and makes the
result look empty
*)
delete first attachment of newNote
show newNote
end tell
-- tell application "Finder" to delete file theFile
on error errText
display dialog "Error: " & errText
end try
return theFile
end run
Note: I was hoping to add programmatically one or more tags to the newly created note (e.g. by asking the user, within a dialog prompt), but I failed. It seems Notes does NOT recognize strings like "#blablabla" as tags, unless they are typed within the Notes.app.