I’m creating smart rules to automatically rename and file my monthly statements and most of them have the complete date information, but there is one where I only have the month and year, and I’d like to rename it to has the last day of that month.
My input is a file named Brokerage Statement - XXXX1234 - 202111 where the last digits are the date with the format yyyymm, I create the following Smart Rule to detect the date but it gives me the first day of the month.
Or this other scripting language. I’d use the first day of the next month and then subtract one day. That should work with leap years too, but one has to take care of December/January.
I thought I’d have to go the scripting route, is there a way to pass parameters to the script? In this I’d like to send the script the regex groups that Scan Name found. The actions would be something like this:
Scan Name, Date
Execute Script to rename the file - with the output of scan name as input for the script
The record is passed to the script
See the skeleton script below
on performSmartRule(theRecords)
tell application id "DNtp"
repeat with theRecord in theRecords
<<< insert code
end repeat
end tell
end performSmartRule
I’d like to rename it to has the last day of that month
Here’s example code to get the last day of the month and rename the record
set theName to name of theRecord
set theName to theName & my LastDay(texts -6 thru -1 of theName)
set name of theRecord to theName
on LastDay(theDate)
set theyyyy to characters 1 thru 4 of theDate as string
set themm to characters 5 thru 6 of theDate as string
-- Convert to the first day of the following month
-- Then get the previous day
set NewDate to current date
set year of NewDate to theyyyy
set day of NewDate to 1
set month of NewDate to (themm as integer) + 1
set NewDate to NewDate - (1 * days)
return day of NewDate
end LastDay
Thank you! While my brain finds AppleScript only workable when I have it fully ready to use and only need a few tweaks your example made me try to do it in JavaScript and I got there:
function performsmartrule(records) {
records.forEach (record => {
const originalName = record.nameWithoutExtension()
const rawDate = originalName.match(/test.+(\d{4})(\d{2})/)
const year = rawDate[1]
const month = rawDate[2]
// JS month is zero-indexed, to get last day of a month we pass 0 of the next month
const lastDay = new Date(year, month, 0).getDate()
const documentDate = [year, month, lastDay].join("-")
const newName = record.name().replace(originalName, documentDate + " - Test")
record.name = newName
})
}
It even works for leap year. Given the file test something 202002.txt it gets renamed to 2020-02-29 - Test.txt.