Script to extract custom field informations out of file name

Dear All,

I am a big fan of DT3 new feature of custom fields. I created two fields to put my tax rates and tax ammount for each invoice in them. Since I already put those two information into my file names, I am looking for an scripting approach to extract those two parts from my file-names and put them right into my custom fields.

my file names always have the same structure:

I divide the informations with underscores, so the script should look for the information before the second last underscore and puts it into my custom field “tax” and for the information before the third-last underscore and put this information into my custom field “steuersatz”.

Is there any way, some might help my out with this little tool? My scripting abilities are very limited, I am just starting to get into this more and more.

Thanks in advance for your support.
And thanks the DT-team for setting this custom-field feature up!

What would be expected to be seen in an actual filename?

1 Like


this is an example of an actual document:
19 is the tax rate of 19% for Germany, 0265 is the tax ammount of 02,65€, 1598 is the full price of 15,98€

Best and thanks.

Do you understand the concept of “class”?
If so, what class do you think a filename is?

No, i dont. But feel free to give me a spin into the right direction :slight_smile:


class is the type of a item, like text or list.
Things like filenames have a class of strings or text (which are generally used interchangeably).

You are using an unusually uniform naming convention (which is the correct thing to do if you’re looking toward automating things; the more uniform, the better). This means we can do a little trick here:
We can process the underscores in the filename with AppleScript's text item delimiters. This allows us to split the string on the underscores, providing a list of the parts of the name separated by them.

Here is a functional code snippet, bearing in mind you’ll have to modify the custom metadata name(s) as needed. Also, I wrote this under the assumption you have created the custom metadata in Preferences > Data already. I approached it this way to show people how the metadata can be displayed as a percent or currency.

And the code (bearing in mind, I have commented it based on your example filename, so you and others can understand what’s happening)

on performSmartRule(theRecords)
	set oldDelimiters to AppleScript's text item delimiters
	set AppleScript's text item delimiters to "_"
	tell application id "DNtp"
		repeat with thisRecord in (selection as list)
			set recordName to (name of thisRecord)
			-- This splits the recordName on the delimiter, here set to an underscore.
			-- It says, "Ignore the underscores and report the parts they separate."
			set partsOfTheName to (text items of recordName)
			--> {"190326", "Amazon", "Blurays-TheDarkKnight-ApocalypseNow", "19", "0265", "ER", "1598.png"}
			-- Counting forwards
			set taxRate to (item 4 of partsOfTheName)
			--> "19"
			-- If using a Decimal Number:Percent data type, you need to convert the value to display correctly in the Custom Info inspector.
			set taxRate to (taxRate / 100)
			add custom meta data taxRate for "taxrate" to thisRecord
			--You can also count backwards, starting at -1 as the last item.
			set taxAmount to item -3 of partsOfTheName
			--> "0265"
			set taxAmount to (taxAmount / 100)
			add custom meta data taxAmount for "taxamount" to thisRecord
		end repeat
	end tell
	-- It's always good to reset the text item delimiters to its original value.
	set AppleScript's text item delimiters to oldDelimiters
end performSmartRule

I also did some basic math to make the values appear as I wanted them to in the Custom inspector.

Really useful little script. Thanks !


1 Like

Glad to hear it! :slight_smile: