I was surprised at how little I could find out there, so I clobbered this together for using in your own rules.
Before we start you need to know a few quirks about how Apple Notes notes work.
- Every note is an HTML document
- You currently cannot attach a note from just anywhere. Possibly due to sandboxing, the file has to be moved/copied reside into a special place ~/Library/Containers/com.apple.Notes/Data
Having said all that, all you need to do is to open Script Editor, paste the below into it, and save the file somewhere you keep those sorts of things:
- Code: Select all
on hazelProcessFile(theFile, dummmy)
set incomingFile to the POSIX path of theFile
set notesSrc to "file://" & encodeText(incomingFile, false, false)
set noteBody to "<html><head></head><body><img src=" & notesSrc & "></body></html>"
tell application "Notes"
make new note at folder "Notes" with properties {name:theFile, body:noteBody}
end tell
end hazelProcessFile
on encodeText(theText, encodeCommonSpecialCharacters, encodeExtendedSpecialCharacters)
set theStandardCharacters to "abcdefghijklmnopqrstuvwxyz0123456789"
set theCommonSpecialCharacterList to "$+!'/?;&@=#%><{}\"~`^\\|*"
set theExtendedSpecialCharacterList to ".-_:"
set theAcceptableCharacters to theStandardCharacters
if encodeCommonSpecialCharacters is false then set theAcceptableCharacters to theAcceptableCharacters & theCommonSpecialCharacterList
if encodeExtendedSpecialCharacters is false then set theAcceptableCharacters to theAcceptableCharacters & theExtendedSpecialCharacterList
set theEncodedText to ""
repeat with theCurrentCharacter in theText
if theCurrentCharacter is in theAcceptableCharacters then
set theEncodedText to (theEncodedText & theCurrentCharacter)
else
set theEncodedText to (theEncodedText & encodeCharacter(theCurrentCharacter)) as string
end if
end repeat
return theEncodedText
end encodeText
on encodeCharacter(theCharacter)
set theASCIINumber to (the ASCII number theCharacter)
set theHexList to {"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "A", "B", "C", "D", "E", "F"}
set theFirstItem to item ((theASCIINumber div 16) + 1) of theHexList
set theSecondItem to item ((theASCIINumber mod 16) + 1) of theHexList
return ("%" & theFirstItem & theSecondItem) as string
end encodeCharacter
- Line 2 & 3 gets the filename Hazel is working with and does some conversion to it to make it usable in HTML. The functions it calls are not my own and are actually very powerful. Credit where credit is due
- Line 4 is where you'll probably want to fiddle a bit to make the note more useful. As it stands, it constructs a variable with only puts the path of the file in the body text and the file itself. If you have any cool ideas about how to prettify the document with your own HTML knowledge, please do share!.
- Lines 5-7 does the deed. Note that it creates the note in the default "Notes" folder.
- Everything after line 10 is concerned with making sure that filenames with spaces or other weirdnesses are being handles properly
As a final note, you can delete the file once the Applescript has done its work with a followup action in Hazel. It serves no further purpose.
Here's an example of how I've set it up in a test rule:
I've tested this on several PDFs and "unknown" file types. For supported file types such as PDF, Notes will display a preview. For unknown filetypes, only a placeholder will appear.
Everything syncs as expected.