Script to extract custom field informations out of file name

Hey,
I did exactly this (see my smart rule), but it still seems to mislead the script somehow.

It seems u are using “date” as the datatype of ur field. The script is returning a String datatype. Try to change the datatype to “Single-Line Text”. If this still doesn’t work, wait for the expert…

1 Like

Hey, thanks, this works as it should. But I’d prefer to have this setup as a date format.

…wait for the expert…

I think he means you, @BLUEFROG :wink: Are you able to help (and maybe even able to explain while it works within the applescript editor but now within the smart rule?)

Glad to see that the script works, at least to what it intends. Now you know how script, custom fields, string manipulation, and smart rule can work together in DT and create something quite handy. I learn something from this process, too!

I am guessing the rest is just how to format the year/mon/date in a way that can be understood by the “date” data type in the cmd.

Good luck. And post the script when it works in date field.

Anyone able to help out here? I am almost at the finish line…

Shortform from above:
I have an applescript, that extracts a string out of a filename and puts it into a custom metadata field, that displays the date. So this script shoudl be able to change the string into a date format. When using the script below within the applescript editor, it works fine as it should.
But as soon as I copy it into a smart rule in DT, a complete different date including an blank before the year is being put into that field (setup as date-field “mdeingangsdatum” in the preferences) When I change it ifrom date into single-line-text, it works fine. But why not with a date-identifier?

on performSmartRule(theRecords)
tell application id "DNtp"
	repeat with thisRecord in (selection as list)
		set recordName to (name of thisRecord)
		set oldDelimiters to AppleScript's text item delimiters
		set AppleScript's text item delimiters to "_"
		
		set partsOfTheName to (text items of recordName)
		set AppleScript's text item delimiters to oldDelimiters
		set theDate to (item 1 of partsOfTheName)
		
		set theYear to (characters 1 thru 2 of theDate) as string
		set theMonth to (characters 3 thru 4 of theDate) as string
		set theDay to (characters 5 thru 6 of theDate) as string
		set metadataDate to (theDay & "." & theMonth & ".20" & theYear) as string
		add custom meta data metadataDate for "mdeingangsdatum" to thisRecord
		
	end repeat
end tell
end performSmartRule

Any ideas?
Many thanks.

Perhaps I can only contribute to your confusion, but the script works for me in a smart rule…sort of.

As explained below, I am getting different results using your script and filename convention.

The date in the custom metadata field is expressed correctly, but with the custom field set to type:date and format:date, the result is 8/11/2019. It does not matter whether I use “.” or “/” as the separator in the “set metadataDate to…” line. and I am not getting the blank before the year that you experienced. Could this be a difference between the German and English localizations?

If I set the data type to Single Line Text, nothing is added to the field. (???)

I copied the script directly from your last post and am using a file named by copying from your example in your original post.

I also made the mistake of naming the custom metadata item eingangsdatum (without the md prefix) and the script filled it in anyway, so I’m not sure what the md prefix accomplishes…it seems to be assumed if not specified.

Finally, I get the same end result executing the script within Script Debugger and as part of a Smart Rule embedded script.

1 Like

I just noticed one clue after reading back through the thread. What you are seeing in the metadata field is a modification of the default…if a file is selected which does not have the mdeingangsdatum field filled in, the default value 12/31/2000 is shown (in the American date style, on my machine). Or if I change to German localization, 31.12.2000.

Also after changing the localization in MacOS preferences to Europe > Germany, the smart rule executed correctly as shown in the second screenshot below…in other words, this did not duplicate the result you are getting.


It appears that DT3 is using the OS localization…which is to be expected…for date rendering (However I use a custom date format that replicates the day-month-year order of the European style, and DT3 is not picking up that modification.)

I can also modify the script as follows, and the separators are added in the metadata field, so all it really needs is ddmmyyyy (when formatted as date:date):

set metadataDate to (theDay & theMonth & “20” & theYear) as string

I still cannot replicate or explain why your script in your smart rule is only modifying the custom metadata field to add a space in front of the year and not inserting the new information. Perhaps it’s time for @BLUEFROG to weigh back in?

Hello everyone.
Thanks for the input, @wmc .
What is funny is the fact, that I started DT today and everything was working fine. I just changed the identifier of the metadate-field back to Date:Date and now it works as it should.

I’ll report if anything changes. But so far, this topic seems to be solved.

Well, too early to be happy.
What worked this morning, did not work in the afternoon.
I still have the same issue, that a blank is suddenly inserted and a complete different date. The identifier is set to Date:Date.
The filename 180916_ is being set to the date 5.1._201

DevonThink runs on 3.0.1, my macbook 10.14.6 in german localization.

I’ve been thinking about what could be causing the problem you see and one thing that comes to mind is if some other process is changing Applescript Text Item Delimiters. T/he default is nothing…expressed by two quote marks with nothing between them (""). Could another script be changing that?

Try using the following as the embedded script and see if it improves:

on performSmartRule(theRecords)
tell application id "DNtp"

	set oldDelimiters to AppleScript's text item delimiters 
	repeat with thisRecord in (selection as list)
		set recordName to (name of thisRecord)
		set AppleScript's text item delimiters to "_"
		
		set partsOfTheName to (text items of recordName)
		-- next line explicitly sets delimiters to default of null
		set AppleScript's text item delimiters to ""
		set theDate to (item 1 of partsOfTheName)
		
		set theYear to (characters 1 thru 2 of theDate) as string
		set theMonth to (characters 3 thru 4 of theDate) as string
		set theDay to (characters 5 thru 6 of theDate) as string
		set metadataDate to (theDay & "." & theMonth & ".20" & theYear) as string
		add custom meta data metadataDate for "mdeingangsdatum" to thisRecord
	end repeat
	set AppleScript's text item delimiters to oldDelimiters
end tell
end performSmartRule

Another problem could be if there are more than one custom metadata with the same or similar names…have you checked that?
Hope this is helpful.

Just to wrap this up:
I have tested the script now for quite a long time and it works just perfect.
Many thanks to @wmc and all others for the support.

Glad you got it working. Would you mind posting the final version of the script?

Sorry for de Delay, total forgot to post the final script in here:

on performSmartRule(theRecords)
tell application id "DNtp"
	
	set oldDelimiters to AppleScript's text item delimiters
	repeat with thisRecord in (selection as list)
		set recordName to (name of thisRecord)
		set AppleScript's text item delimiters to "_"
		
		set partsOfTheName to (text items of recordName)
		-- next line explicitly sets delimiters to default of null
		set AppleScript's text item delimiters to ""
		set theDate to (item 1 of partsOfTheName)
		
		set theYear to (characters 1 thru 2 of theDate) as string
		set theMonth to (characters 3 thru 4 of theDate) as string
		set theDay to (characters 5 thru 6 of theDate) as string
		set metadataDate to (theDay & "." & theMonth & ".20" & theYear) as string
		add custom meta data metadataDate for "mdeingangsdatum" to thisRecord
	end repeat
	set AppleScript's text item delimiters to oldDelimiters
end tell
end performSmartRule

I don’t quite understand why you set the text delimiter to nothing before you split the date – I’m not an AppleScript expert, but I doubt that this is necessary given that you extract the characters by their position from the date string.

BTW, How do you get all this information in the filename in the first place? DT3? Hazel? Manually?

Hey @BLUEFROG or any others. I’ve got a little question, resulting from this original code you have provided.

I have split my RecordName into different pieces, based on the delimiter which is set to my underscores. But now I would like to extract the last item out of my recordName, which is 1598.png in the example.
But I only would like to extract the 1598 and put it into another metadata field.
How would I tell the script, that the last item of my RecordName is only the digits till the .png (or any other file extension)?

Many thanks.