Trying my hand at automatically adding a file to the Apps Note section. I've gotten to the point where I can get it done via Applescript, but I fail to get the Applescript working in Hazel, unless there are no spaces in the filename.
My research so far has told me that the file in question first has to be in ~/Library/Containers/com.apple.Notes.HTMLConverter/Data. That doesn't seem to be true, but i've stuck with that for the time being, so my first action is to move it there.
Then I execute an Applescript that will put it into Notes.
A very basic version of the Applescript (embedded) works, but *only* if the file doesn't contain spaces.
- Code: Select all
set notesSrc to "file://" & theFile
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
I had a heck of a time trying to troubleshoot what to do when a space is present. since the body of an Apple Note is HTML, a space would have to be replaced with %20 for the path to be valid. There are other characters like this as well.
I've found a couple of functions that I can add to the Applescript that does just that, but it never ever works in Hazel.
The problem seems to be that the moment I need to refer to subroutines, Hazel fails to execute.
I currently a script set as an external file, but it still fails (with a bit of tweaking, such as defining theFile and removing the Hazel-required wrapper it works in Applescript.
- Code: Select all
on hazelProcessFile(theFile)
set notesSrc to "file://" & encodeText(theFile, false, false)
log notesSrc
set noteBody to "<html><head></head><body><img src=" & notesSrc & "></body></html>"
log noteBody
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
My questions are:
-How do I properly wrap the Hazel code so that it executes?
- Is it possible to have subroutines in embedded scripts (my preferred option)
Can some kind stranger advise what I'm missing?