Automatic Rename Documents based on its content

Hello and a happy new year to all of you!

I’d like to automatically rename and file my phone company’s monthly bills.

The invoice contains the Subject: “Your invoice for April 2020”. But the invoice date in this case was 13.05.2020.

How can I resolve April 2020 to mm JJJJ so that I can rename the file to invoice-phonecompany_2020-04.pdf?

Thank you.

Is the date also inside the document or only in the subject? Then DEVONthink’s placeholders (see e.g. Change Name action of smart rules) could use it.

No, the date is just in the subject. Here is an example:

In that case only a script could convert the subject to the desired format.

OK, can you tell me where I can find an example for this case?

First off, my telecoms mobile bill contains the month in the filename of the PDF. I.e. it is something like “Rechnung_2021_05_…” for the May bill, even though the date in the file is something like 6.5.2021. So going for the filename might be easier than converting an alpha month to a numeric one.

In any case, I do this kind of processing with Hazel because it is (still) a bit more versatile than DT in that respect. YMMV, of course. If I had to do it in DT and could not rely on the filename, I’d probably go for a JavaScript and regular expressions.
In any case, this topic has been discussed numerous times here already, so there are tons of examples available.


OK, in the past I have scanned my Telekom Invoices and therefore their filenames didn’t contain the billing date. Right now I get them by eMail but they didn’t contain the billing date in the filename too…
However I took up the friendly advice and worked a little with applescript and created the following script with the help of a few examples. Maybe someone else can use it. Have fun with it and if there are any suggestions for improvement: please always bring it to me. Thanks.

on performSmartRule(theRecords)
	tell application id "DNtp"
		repeat with theRecord in theRecords
			set theName to name of theRecord
			set phoneNumber to get custom meta data for "mobilfunknummer" from theRecord
			if (theName is not "0-00") then
				set theCreationDate to (current date) -- initialize a date Object
				set day of theCreationDate to (characters 7 thru 8 of theName) as string
				set month of theCreationDate to (characters 4 thru 5 of theName) as string
				set year of theCreationDate to "20" & (characters 1 thru 2 of theName) as string
				set creation date of theRecord to theCreationDate
				set billingDate to theCreationDate
				set the day of billingDate to 1 as string
				set billingDate to billingDate - (1 * days) -- the invoicing period is the one month ago of the billing month
				set billingYear to year of billingDate as integer
				set billingMonth to month of billingDate as integer
				if length of (billingMonth as string) = 1 then
					set billingMonth to "0" & billingMonth as string
				end if
				set d2 to billingYear & "-" & billingMonth
				set theName to "RE Telekom " & phoneNumber & " fuer " & d2
				set theName to "RE Telekom " & phoneNumber & " fuer (ohne Datum)"
			end if
			set the name of the theRecord to theName
		end repeat
	end tell
end performSmartRule

Thanks for sharing. :slight_smile: