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…
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.
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.
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.
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.
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?