Page 1 of 1

extracting custom tokens from PDF

PostPosted: Mon Jun 10, 2013 7:56 pm
by rachsmac
I am wanting to use some custom information when Hazel renames a PDF file.

I've run a mdls query in terminal and found the name of the details of the item I want to include (I've annonymised the data):

kMDItemRecipients = (
"\"name of recipient\" <email of recipient>"

I would like Hazel to rename the file with the date and title of the file (I know how to do that) and also the "name of recipient". I don't know how to do that bit

I've tried embedding the following applescript into Hazel and running this before the renaming command:

set posixPath to quoted form of (POSIX path of theFile) as string
set theCustomToken to (do shell script "/usr/bin/mdls -name kMDItemRecipients " & posixPath & " | awk -F ' = ' '{print $2}'")
return {hazelExportTokens:{theCustomTokenValue:theCustomToken}}

I've added the Custom Token as email recipients

Hazel runs the task, renames the file with the date and title but leaves the area for the custom token blank.

I figure it is something simple i've missed but I am really new to scripting. Any help would be greatly appreciated.

Thank you

Re: extracting custom tokens from PDF

PostPosted: Tue Jun 11, 2013 10:43 am
by Mr_Noodle
In the UI, did you export the token from the script using the same name as in the script (theCustomToken)? The names have to match as there's no other way for Hazel to know which token is which.

Re: extracting custom tokens from PDF

PostPosted: Tue Jun 11, 2013 2:35 pm
by rachsmac
I'm sorry could you put that in really simple language for dummys and preferably type out an example for me. I really am new at this.

I'm assuming that the UI means the applescript that is embedded in Hazelsoft?

I tried this:
set posixPath to quoted form of (POSIX path of theFile) as string
set theCustomToken to (do shell script "/usr/bin/mdls -name kMDItemRecipients " & posixPath & " | awk -F ' = ' '{print $2}'")
return {hazelExportTokens:{theCustomTokenValue:emailrecipients}}

and in the box that says "add the names of any custom tokens your script exports" I clicked + and typed emailrecipients

this returned an error saying

so then I tried
set posixPath to quoted form of (POSIX path of theFile) as string
set theCustomToken to (do shell script "/usr/bin/mdls -name kMDItemRecipients " & posixPath & " | awk -F ' = ' '{print $2}'")
return {hazelExportTokens:{kMDItemRecipients:emailrecipients}}

This returned the same error. I've included a copy of the log:

2013-06-12 06:33:15.868 hazelworker[14885] 2013-06-11 07.31.29Z hi.pdf: Rule rename applescript matched.
2013-06-12 06:33:15.931 hazelworker[14885] [Error] AppleScript failed: Error executing AppleScript on file /Users/rachelmackay/Desktop/Hazel experiments/2013-06-11 07.31.29Z hi.pdf.
2013-06-12 06:33:15.931 hazelworker[14885] AppleScript error: {
NSLocalizedDescription = "The variable emailrecipients is not defined.";
NSLocalizedFailureReason = "The variable emailrecipients is not defined.";
OSAScriptErrorAppAddressKey = "<NSAppleEventDescriptor: null()>";
OSAScriptErrorBriefMessageKey = "The variable emailrecipients is not defined.";
OSAScriptErrorMessageKey = "The variable emailrecipients is not defined.";
OSAScriptErrorNumberKey = "-2753";
OSAScriptErrorOffendingObjectKey = "<NSAppleEventDescriptor: 'utxt'(\"emailrecipients\")>";
OSAScriptErrorRangeKey = "NSRange: {0, 0}";

Re: extracting custom tokens from PDF

PostPosted: Wed Jun 12, 2013 2:51 pm
by Mr_Noodle
Looking at it more closely, I think your script is setting things backwards. If the custom token name is emailrecipients and the value is theCustomTokenValue, then the last line should be:

Code: Select all
return {hazelExportTokens:{emailrecipients:theCustomTokenValue}}

Re: extracting custom tokens from PDF

PostPosted: Wed Jun 12, 2013 6:04 pm
by rachsmac
nope that didn't work either. However, I just worked out that I don't need to extract a custom token because I can use "recipients" and it gets this data anyway. Sorry for wasting your time.

Re: extracting custom tokens from PDF

PostPosted: Fri Jun 14, 2013 11:34 am
by a_freyer
... and in the box that says "add the names of any custom tokens your script exports" I clicked + and typed emailrecipients ...


It sounds like your problem is that you did not notice that you *must* name the custom token the exact same as it appears in the exportToken line:

So, for example with this (slightly modified) version of your code:
Code: Select all
set posixPath to quoted form of (POSIX path of theFile) as string
set theCustomToken to (do shell script "/usr/bin/mdls -name kMDItemRecipients " & posixPath & " | awk -F ' = ' '{print $2}'")
return {hazelExportTokens:{theCustomTokenName:theCustomToken}}


You *must* set the name of the token using the (i) dialog to be exactly theCustomTokenName. If you name it emailrecipients as you have, Hazel will look for an exported token called emailrecipients, which it won't find. It will then shoot an error, or it will return blank.

Again, in your second try:

Code: Select all
set posixPath to quoted form of (POSIX path of theFile) as string
set theCustomToken to (do shell script "/usr/bin/mdls -name kMDItemRecipients " & posixPath & " | awk -F ' = ' '{print $2}'")
return {hazelExportTokens:{kMDItemRecipients:emailrecipients}}


The name *must* be kMDItemRecipients. Further, as Mr_Noodle pointed out, in this script you are not passing the value at all. You can however, use a variable.

Below is a generalized example that I hope clarifies both issues:

Code: Select all
set posixPath to quoted form of (POSIX path of theFile) as string

set theCustomTokenName to "emailrecipients"

set theCustomTokenValue to (do shell script "/usr/bin/mdls -name kMDItemRecipients " & posixPath & " | awk -F ' = ' '{print $2}'")

return {hazelExportTokens:{theCustomTokenName: theCustomTokenValue}}


Now, in your (i) menu, add 'emailrecipients'

NOTE: I do understand that you've already solved your problem, but I am posting this here so that you might understand what went wrong.

Re: extracting custom tokens from PDF

PostPosted: Thu Jun 20, 2013 12:47 pm
by rachsmac
Thanks so much, that is really helpful. It is something that I'm likely to want to use again in the future. :D