Hazel - renaming using contents of file

Hi,would anyone be able to tell me if there is a way to rename a document using an invoice number that is within the document. I can figure out how to do it using the date but I can’t seem to work this out. Thank you

Provided the invoice number follows a consistent pattern (same number of digits each time) and is sufficiently unique (there isn’t any other number with the same number of digits in the invoice) you should be able to do it using the “contain match” condition.

Thank you for your help but it hasn’t worked unfortunately, I have tried evrything. The invoice number is still searchable using spotlight so not all is lost. Thank you again for your help :smiley:

Is it the same invoice number each time? If so you could use a “contain” instead of a “contain match” like @ChrisUpchurch recommended. I guess any additional information would be helpful so we can better recommend a solution :wink:

Hi, I have provided more information in this post. The invoice number is always different except it always ends in /01 eg. 499970/01 The invoices come in as .txt so I have to convert them each time into pdfs. Unfortunately the words invoice number do not come before the invoice number itself whatever way it has been converted. So I can’t use invoice number in the contains match variable.

I’m not at My Mac right now to test this but have you tried something like this for your match criteria?

111111/01

Where the first six 1s are the digit placeholder and the /01 at the end of it is standard text?

But also since they come in as TXT, you could save those, have Hazel trigger a shell script to extract the reference (command line utilities are awesome for processing text files!), rename the file and then convert it to PDF.

I do have to ask out of curiosity though. Why are you converting the TXT to PDF? For annotations of including in a combined PDF or something?

2 Likes

Hazel matches and renames the file for me using the Custom Text token.

Hazel%20Screenshot%202018-07-14

1 Like

Brilliant thank you very much that worked I was really stuck but didn’t realise you could do that with the custom text. I am converting them into PDF so they are searchable on iOS. They are delivery invoices for a business and they are 5-10 pages long and sometimes we need to find products that are on the invoices and this saves times. Thank you again :grinning:

Thank you for the screenshot it really helped to figure out the custom text input. I’m still learning how to use hazel so this has really helped.

2 Likes

It took me a second to get this. Many thanks!

3 Likes

Hi all,

I have the same problem but with the added inconvenience that the PDF was originally a table-formatted document and there are several numbers (phone numbers) in it that match the invoice number format.

I’ve been using the custom text token with three individual digit placeholders. I’ve tried adjusting the “occurrence” to what seems to be right but it doesn’t give the correct result.

The invoice number is in two “cells”, one above the other: " Factura n° " above and the invoice number, e.g. " 111 ", below. I’ve tried using this text (Factura n°) as a marker for where to seek the invoice number, but I get back a “no match” result on previewing.

In our company address block, there are two phone numbers with formatting as follows:
+11 111 11 11 11

The output I need on renaming is simple enough: Fac_0111+extension
i.e. “Fac_” then the invoice number, adding a preceding " 0 ", then the extension (.pdf).

I can successfully create the “Fac_0” and extension parts of the renaming rule, but grabbing the actual invoice number from the document contents and placing it into the string correctly is proving challenging.

Can anyone point me to a solution?

I’ve been playing with this (couldn’t resist) and I think I have a part-answer, but the rest goes a little outside my technical expertise and I’ve been procrastinating long enough with this (at least for now). Sharing what I learnt (even though I haven’t quite been able to pull it all together) in case it helps…

  • You can use a ‘Run Javascript’ action which can return its own output attributes which can then be used as custom attributes (for example, in a subsequent renaming url). I got this part to work!

  • As part of the ‘Run Javascript’ action you can do a .match using Regex on a string and return the match. I got this to work too!

  • In theory, Javascript supports negative lookbehinds in Regex (apparently, but it looks like this is new-ish). Even though something like /?<!\+\d{2}) \d{3}/ seems like it should work in theory just to pick up the invoice number (and not the phone numbers) in my testing, it doesn’t work in Hazel (and was a little bit dodgy in my testing on regex tools too), so perhaps isn’t supported yet. I’m sure there’s a workaround to this someone more familiar with Regex than me (or more thorough Googling) can help with.

  • BUT after all this the biggest problem I have is how to get the contents of the file (as opposed to just the file itself) into the Javascript script. The file itself is automatically passed in, so perhaps there is a Javascript tool that can help with this (it looks like pdf.js might do this—but I didn’t have time to play with it—and I have no idea if you can use it within Hazel). Or maybe there is something built into Hazel that I have missed.

So, basically, I didn’t really help you with your problem, but I learnt a fair bit, so thanks! :slight_smile: Hopefully someone else can fill in the gaps? (Or point you to a much simpler solution, haha!)

Actually…I have been thinking about this some more and I believe this could work by sending the file from Hazel to Keyboard Maestro, if that’s an option for you. From there it could find the regular expression (the docs suggest it supports negative lookbehind) and rename the file.

Happy to play around with that option and try to build something if that would help you at all?

(There’s still the possibility someone has a much better, simpler, solution, though, haha.)

Glad to be of service by providing a problem you were inspired to solve :blush:

I do have Keyboard Maestro but haven’t got to grips with it yet. Might be the perfect time.

Thanks very much for continuing to stick your horns into this problem. :grin: