Calculate `month` -1 in placeholder names possible?

Hi all

im wondering if one can use date calculations in placeholder names? for example:

can how can one use in the above example the previous month when creating a filename, that is

  • Month (-1)-

does that make sense?

thx alot

Z

Placeholders do not support calculations.

got it thx @cgrunenberg

is there another way (automatically) to get the date of the previous month when using smat rules when processing files?

z

No, you can’t do that. The parameter to Change name is a string and is not evaluated except for placeholders. Use a script like (that’s JavaScript code, not AppleScript) in a smart rule:

function performsmartrule(records) {
  records.forEach(r => {
    r.name = `12.01--${r.creationDate().getYear()} - ${r.creationDate().getMonth()}-01--salary`;
  })
}

Not tested, and note that getMonth() in JavaScript returns the months starting with 0.

  1. Why would you want the previous month?
  2. Is this supposed to be the month name or number?
  3. Please explain your naming convention, 12.01--….

I guess that the “creationDate” of the document is one month later than the date the document itself refers to (think payslip or account statement). So they want to have the “referral date” in the name.
But subtracting 1 from the month is not doing that – it will give you 2023-0 for January 2023, while it should probably be 2022-12.

Who knows.

thx both

yes this is a payslip (non english) so the scan/OCR wont work

and yes i get the payslip 1 of the month but the pay period is the previous month so wanted the actuall name to indicate the month before (the pay period)

the naming convention would be a number so something like this

12.01--YYYY-MM-DD--payslip

i mean its 1st world problems :slight_smile: so i can just manually change it later

thx guys

Z

I’m still confused by your naming convention (and yes I have code that works)…

What is 12.01 ?
Is the MM that part that should be replaced with the previous month?

thx @BLUEFROG , the 12.01 is part of a index system i use (johnny decimal Link)

the month is for the -MM- part, ie the final file looks like this

12.02--2024-08-16--salary where 08 would be the month if i created it today (in September)

its just a index, sorry for the confusion :slight_smile:

Gotcha and no worries! I just like to be as thorough as possible. So you are preserving the day, like so…

Here’s the code…

on performSmartRule(theRecords)
	tell application id "DNtp"
		repeat with theRecord in theRecords
			tell theRecord
				set recordDate to its addition date
				set {previousMonth, theYear} to {month, year} of (recordDate - (((day of recordDate) + 1) * days)) -- This is where the magic happens
				set dateString to (theYear & "-" & (zeropad of me to (previousMonth as integer)) & "-" & (zeropad of me to (day of recordDate)))
				set name to dateString & "_" & (name without extension) as string
			end tell
		end repeat
	end tell
end performSmartRule

to zeropad to num
	return (characters -1 thru -2) of ("0" & num) as string
end zeropad

You just need to put the dateString variable wherever it’s supposed to go in the name. In my code it is a prefix to the existing name.

And yes, this will correctly identify the previous year, so running it on a document with an addition date of January 1, 2024 will yield a date string of 2023-12-01.

yes that would be great

Cool code (I mean the “magic”, of course). Here’s a JavaScript version:

function performsmartrule(records) {
  records.forEach(r => {
    const recordDate = r.additionDate();
    const oneMonthBefore = new Date(recordDate.valueOf()-(recordDate.getDate()+1)*86400*1000);
    r.name = `${oneMonthBefore.toISOString().replace(/T.*$/,'')}_${r.nameWithoutExtension()}`
  })
}

Same “magic”, but with milliseconds (JavaScript uses those instead of AppleScript’s seconds). toISOString then performs formatting and zero padding. The only drawback is that it adds the time, so we have to cut that off again with replace().
Another thing to note is that we do not need Application("DEVONthink 3") here, since the application object doesn’t come into play – only records.

1 Like

thx so much both! really appreciated!

@BLUEFROG , is this how i use the dateString in the rule?

or do you have to define/wrap it in some charecter?

thx

Z

This line is where the name change happens…

set name to dateString & "_" & (name without extension) as string

So if the document was named my salary, the script in the form I presented would change the name to e.g., 2024-08-02_my salary.
So if you wanted to maintain the form you were using, you’d need to modify that line, like so…

set name to ("12.01--" & dateString & "--01--salary") as string

No. You add an execute script action to the rule.