Page 1 of 1

In progress: Adding a file to Apple Notes - Need some advice

PostPosted: Mon Jan 16, 2017 11:17 am
by snorbaard
Hi All,

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?

Re: In progress: Adding a file to Apple Notes - Need some ad

PostPosted: Mon Jan 16, 2017 3:05 pm
by Mr_Noodle
Embedded scripts are actually wrapped in the same wrapper you use for an external script. It just isn't shown.

Check the help on AppleScript. The handler takes two arguments now, not one, which may the problem in your case.

Re: In progress: Adding a file to Apple Notes - Need some ad

PostPosted: Mon Jan 16, 2017 4:26 pm
by snorbaard
Mr_Noodle wrote:Embedded scripts are actually wrapped in the same wrapper you use for an external script. It just isn't shown.

Check the help on AppleScript. The handler takes two arguments now, not one, which may the problem in your case.


Thanks for the reply.
I did have the second argument in the call originally, but it didn't change anything (the script still failed), and removed it, thinking it was an optional argument.

If I turn on, Hazel still recognises the file, and moves it to that folder (still not sure if it's really necessary), then I just get an "Error executing Applescript on file Stress questionnaire.pdf". So I don't think the lack of an argument makes a different (unless I have to fill something into the (i)?

Code: Select all
on hazelProcessFile(theFile, dummy)
   
   set notesSrc to "file://" & encodeText(theFile, false, false)


..doesn't make any difference.

The single log entry that really pertains to the error is below. Is this any help?
Code: Select all
2017-01-16 22:11:02.840 hazelworker[75813] [Error] AppleScript failed: Error executing AppleScript on file /Users/snorbaard/Library/Containers/com.apple.Notes/Data/HTMLConverter/Stress questionnaire.pdf.
2017-01-16 22:11:02.840 hazelworker[75813] OSAScript error: {
    NSLocalizedDescription = "alias \"Macintosh HD:Users:snorbaard:Library:Containers:com.apple.Notes:Data:HTMLConverter:Stress questionnaire.pdf\" doesn\U2019t understand the \U201ccount\U201d message.";
    NSLocalizedFailureReason = "alias \"Macintosh HD:Users:snorbaard:Library:Containers:com.apple.Notes:Data:HTMLConverter:Stress questionnaire.pdf\" doesn\U2019t understand the \U201ccount\U201d message.";
    OSAScriptErrorAppAddressKey = "<NSAppleEventDescriptor: [0x0,2 \"hazelworker\"]>";
    OSAScriptErrorAppNameKey = "Hazel.prefPane";
    OSAScriptErrorBriefMessageKey = "alias \"Macintosh HD:Users:snorbaard:Library:Containers:com.apple.Notes:Data:HTMLConverter:Stress questionnaire.pdf\" doesn\U2019t understand the \U201ccount\U201d message.";
    OSAScriptErrorMessageKey = "alias \"Macintosh HD:Users:snorbaard:Library:Containers:com.apple.Notes:Data:HTMLConverter:Stress questionnaire.pdf\" doesn\U2019t understand the \U201ccount\U201d message.";
    OSAScriptErrorNumberKey = "-1708";
    OSAScriptErrorOffendingObjectKey = "<NSAppleEventDescriptor: 'alis'($00000000021C000200000C4D6163696E746F7368204844000000000000000000000000000000D459E820482B000000A943CB18537472657373207175657374696F6E6E616972652E70646600000000000000000000000000000000000000000000000000000000000000000000000000000000A92378D49AD1915044462050444670FFFFFFFF000009200000000000000000000000000000000D48544D4C436F6E76657274657200001000080000D459CC000000001100080000D49AB57100000001001C00A943CB000BAD43000BAD42000BAA25000BA938000BA93300000077000200724D6163696E746F73682048443A55736572733A00736E6F7262616172643A004C6962726172793A00436F6E7461696E6572733A00636F6D2E6170706C652E4E6F7465733A00446174613A0048544D4C436F6E7665727465723A00537472657373207175657374696F6E6E616972652E706466000E003200180053007400720065007300730020007100750065007300740069006F006E006E0061006900720065002E007000640066000F001A000C004D006100630069006E0074006F007300680020004800440012005E55736572732F736E6F7262616172642F4C6962726172792F436F6E7461696E6572732F636F6D2E6170706C652E4E6F7465732F446174612F48544D4C436F6E7665727465722F537472657373207175657374696F6E6E616972652E706466001300012F00001500020010FFFF0000$)>";
    OSAScriptErrorRangeKey = "NSRange: {0, 0}";
}


Ultimately, I have this pretty scary external script, when all I need to do is replace whatever the thefile variable contains to be a valid file:// URI (in other words add file:// in front and replace characters like spaces to their URI equivalents (spaces become %20 and so on). The two on methods I got from another Applescript scripting site, and they're super powerful.

When run from within the Script Editor, the log notesSrc outputs the correct result.
(*file:///Users/snorbaard/Library/Containers/com.apple.Notes/Data/HTMLConverter/Stress%20questionnaire.pdf*)

I'm frustrated by the lack of seeing what the error message Hazel is giving, but I'm not above asking for "can you fix it for me?" either..

Re: In progress: Adding a file to Apple Notes - Need some ad

PostPosted: Tue Jan 17, 2017 1:07 pm
by Mr_Noodle
The error points out that theFile is an alias, not a string. You'll need to do something like "the POSIX path of theFile" to get at a string representation of it.

Re: In progress: Adding a file to Apple Notes - Need some ad

PostPosted: Wed Jan 18, 2017 7:07 am
by snorbaard
Thanks for the tip MrNoodle. There's a difference, but I'm still not getting the result I need.

Compare the following two scripts (one is the version I use in Hazel, and the other is a slightly modified one I run straight from Script Editor)

The one used in Hazel
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=" & POSIX path of notesSrc & "></body></html>"
   
   tell application "Notes"
      make new note at folder "Notes" with properties {name:theFile, body:noteBody}
   end tell
   
end hazelProcessFile


The one used for testing
I just remove the Hazel code and manually assign theFile.
Code: Select all
property theFile : "/Users/snorbaard/Library/Containers/com.apple.Notes/Data/HTMLConverter/Stress questionnaire.pdf"

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=" & POSIX path of notesSrc & "></body></html>"

tell application "Notes"
   make new note at folder "Notes" with properties {name:theFile, body:noteBody}
end tell



You can see I don't know what I'm doing because I added POSIX path of in two places.

The second one works perfectly, and I get what I want (or at least the start of it):
Image

But the one that goes to via Hazel now creates the note and heading, but the note is blank (although there is a placeholder for the img tag:
Image

I feel I'm getting so close, but without knowing how to troubleshoot Applescript within Hazel (the Applescript section doesn't say much - is there a log command or something? Notifications don't say much) I'm stuck.

Is additional support for the Notes app coming?

Re: In progress: Adding a file to Apple Notes - Need some ad

PostPosted: Wed Jan 18, 2017 1:22 pm
by Mr_Noodle
I don't think you should be getting the path of notesSrc as that is an URL string.

If you turn on debug mode, you might get more information in the logs: https://www.noodlesoft.com/kb/hazel-debug-mode/

In general, though, I'd display dialogs with the values of various variables to make sure they are what you expect.

Re: In progress: Adding a file to Apple Notes - Need some ad

PostPosted: Thu Jan 19, 2017 7:28 am
by snorbaard
Mr_Noodle wrote:I don't think you should be getting the path of notesSrc as that is an URL string.

If you turn on debug mode, you might get more information in the logs: https://www.noodlesoft.com/kb/hazel-debug-mode/

In general, though, I'd display dialogs with the values of various variables to make sure they are what you expect.


Thank you for that. The additional debug mode helped show that Display Dialog isn't allowed, but your tip about notesSrc not needing the POSIX version definitely did. It appears to be working now.

THANK YOU

I want to package this up into a neat little tip for future users, so I have a couple questions:
  • Will this kind of functionality (adding a file to a a Folder in the Notes App be coming at some point?
  • My External AppleScript is only external because it looks like the Hazel internal Applescript interpreter doesn't like additional procedures/methods - is there any way I can bundle this into an embedded script?

Re: In progress: Adding a file to Apple Notes - Need some ad

PostPosted: Thu Jan 19, 2017 12:20 pm
by Mr_Noodle
Will consider adding it though I'm not sure how common it is.

As for the embedded script, probably not. An embedded script is already in a handler so there would be no way to do what you want.

Re: In progress: Adding a file to Apple Notes - Need some ad

PostPosted: Mon Mar 11, 2019 3:17 pm
by kevinreynolds
Did you manage to get a notes Hazel AppleScript to work? any code or help would be very helpful please.