Set creation date from file names like "Apr 27, 2021"

There’s no need to parse the date in this case.
Apr 27, 2021 is valid syntax for a date object.
For example, run this in Script Editor…

set r to "Apr 27, 2021"
(date r)

-- > date "Tuesday, April 27, 2021 at 12:00:00 AM"

It returns a date object which is what you need to set the creation date of a record in DEVONthink.

However, the name would need to be parsed. Are all the files named this way - Note Mon Day, Year (some number) ?

1 Like

Thanks! It’s great to see that converting to a date object is easy given my date format.

The file names all contain the date string, but they have various text in front of that. Some have a trailing number in parenthesis and some do not. I could build a regex to pull out the date string, but an example on how to do this is AppleScript would be most helpful.

Also, I’m having trouble getting the smart rule embedded script editor to accept anything like:
set r to “Apr 27, 2021”
set theDate to date r
Not sure what I’m doing wrong here…

You’re welcome.

Have you looked at the Help > Documentation > Automation > Smart Rules Scripts?

A RegEx in AppleScript? Tough luck. If you do not need a smart rule but only a script, you could use JavaScript. Where you have RegExes at hand.

Thanks for the pointers so far. I have the following which works in the Script Editor

set theName to "abcd May 1, 2021 (1)"
set sedString to quoted form of "s/^.* \\([A-Z][a-z]\\{2,3\\} [0-9]\\{1,2\\}, [0-9]\\{4\\}\\)/\\1/"
set theString to do shell script "echo " & quoted form of theName & "| sed " & sedString
set theDate to date theString
theDate

and returns date "Saturday, May 1, 2021 at 12:00:00 AM"

But I’m hitting a problem trying to use this code in a smart rule. For the smart rule, I have this:

on performSmartRule(theRecords)
	tell application id "DNtp"
		repeat with theRecord in theRecords
			set theName to the name of theRecord
			set sedString to quoted form of "s/^.* \\([A-Z][a-z]\\{2,3\\} [0-9]\\{1,2\\}, [0-9]\\{4\\}\\)/\\1/"
			set theString to do shell script "echo " & quoted form of theName & "| sed " & sedString
			set theDate to date theString
			set creation date of theRecord to theDate
		end repeat
	end tell
end performSmartRule

But the script editor complains with “Expected end of line, etc. but found identifier.” about the line set theDate to date theString unless this line (or even the keyword “date”) is commented out, or unless I remove the tell application id "DNtp" block.

Seems that my problem is related to using date to convert from string to date object inside the DNtp tell block.

That is correct. You could put that name processing code in a separate handler to alleviate the problem.

I’m interested in seeing the “regex” for this
Regex isn’t directly supported in Applescript, however we can shell out the execution

Thanks for the tip! Now I have:

tell application id "DNtp"
	set sel to (selection as list)
	my performSmartRule(sel)
end tell

on performSmartRule(theRecords)
	tell application id "DNtp"
		repeat with theRecord in theRecords
			set theName to the name of theRecord
			set creation date of theRecord to my getDate(theName)
		end repeat
	end tell
end performSmartRule

on getDate(theName)
	set sedString to quoted form of "s/^.* \\([A-Z][a-z]\\{2,3\\} [0-9]\\{1,2\\}, [0-9]\\{4\\}\\)/\\1/"
	set theString to do shell script "echo " & quoted form of theName & "| sed " & sedString
	set theDate to date theString
	return theDate
end getDate

And this works perfectly when I select records and run from the script editor. However when I try to run this as a smart rule, I get invalid argument date.

I’m guessing, but I think rather than embedding your script in the smart rule, you need to call it as an external script (save it to /Users/yourUserName/Library/Application Scripts/com.devon-technologies.think3/Smart Rules, and use External rather than Embedded in the smart rule. Date seems not to work from within DT.

Thanks, but that’s what I’m already doing now. The exact same file works when run in script editor, but fails with invalid argument date when called as an external script from a smart rule.

This code block works when called that way:

on convertDate(scrapedDate)
	return date scrapedDate
end convertDate

So, although it’s not obvious to me why it should be so, try

on getDate(theName)
	set sedString to quoted form of "s/^.* \\([A-Z][a-z]\\{2,3\\} [0-9]\\{1,2\\}, [0-9]\\{4\\}\\)/\\1/"
	set theString to do shell script "echo " & quoted form of theName & "| sed " & sedString
	return date theString
end getDate

As a side note: could that phenomenon hint to a problem with the implementation of the external scripts integration? Perhaps similar to the weird behavior with external JavaScript in this context?
The common factor being that scripts run fine in script editor but not in the context of DT.

The suggestion by @Blanc does not work for me. The invalid date error occurs in the line set creation date of theRecord to my getDate(theName), not in the getDate handler.

Can you check to see what is actually being returned by you subroutine?

Not obviously, at least; this works:

on performSmartRule(theRecords)
	tell application id "DNtp"
		repeat with theRecord in theRecords
			set documentText to plain text of theRecord
			set scrapedDate to do shell script "echo " & (quoted form of documentText) & " | tr '\\r' '\\n' | sed  -n -E 's_*[0-9]{4}\\/([0-9]{2}).([0-9]{2}).([0-9]{2})_\\1.\\2.\\3_p'"
			set creation date of theRecord to my convertDate(scrapedDate)
		end repeat
	end tell
end performSmartRule

on convertDate(scrapedDate)
	return date scrapedDate
end convertDate

Which is why I am wondering what exactly is being returned by the subroutine, as everything else is effectively the same.

@dleigh are you actually including

tell application id "DNtp"
	set sel to (selection as list)
	my performSmartRule(sel)
end tell

in the script for the smart rule? Or were you only using that section to be able to run the script from script editor?

But that code not working outside of the handler, is that expected behavior?

I don’t know yet, because I’m not sure what is going on. @dleigh could you post the smart rule you are using to trigger the script, pls?

Very strange, the code I shared above in Set creation date from file names like "Apr 27, 2021" - #9 by dleigh is now working fine when called from a smart rule. This started working after I change the smart rule to use a different script file in order to test the proposed code from @Blanc (which also worked). After I changed the smart rule to point back to my original script, I could not reproduce the error. Perhaps there was some caching behavior on the smart rule so it wasn’t picking up changes to my external script?

Ah :smiley: you are not yet aware of the fact that external scripts are cached :crazy_face: External scripts are cached, you need to restart DT after making modifications.

See here for a suggestion from the resident script master for circumventing this problem :wink:

Wow, I guess I learned that the hard way! I’m really glad I know now… Thanks for the pointer on workarounds.

1 Like