How to Generate Previous Month Number in a Variable?


I have following macro:

I have question how I can generate in folder name previous month, and next month by current month, at the moment I can only generate currently month in variable.

For some summaries and further links, see:

1 Like



There is an example of this and other calculations on the wiki page:

1 Like

In your macro, you call ICUDateTime twice, once for the year and once for the month. Note from @Zabobon's answer that you're not restricted to using just things like yyyy and MM in your formatting string—you can get the entire formatted date using a single formatting string.

This is the case for all the various ICUDateTime… tokens.


As well as the above suggestions -- you don't need the shell script action because KM's "Create New Folder" can also create intermediate folders as required. It's an option under the "Settings" cogwheel.

And you only need to create the newDirPath variable if you need it elsewhere later in the macro -- if not, you can replace all three actions in your screenshot with:



Thanks guys for helping.

I have two questions:

  1. what is current date is December-2022, and i want create +1 month, how I should script it, to create 01-2023 (January)?

  2. I must create for my finance manager 100 macros.
    At the moment example macros:

We have 3 companies, I must double this position x3, and changed each position about different variable.

Can I create one .kmmacros file, with all macros from this group, and using for each macro common variables?

@Zabobon already gave you that -- %ICUDateTimePlus%1%Months%yyyy-MM%.

  1. "I'd like you get the current date and time": %ICUDateTime%
  2. "I'd like you to get the current date and time and add something": %ICUDateTimePlus%
  3. "I'd like you to add 1 of something": %ICUDateTimePlus%1%
  4. "The something is months": %ICUDateTimePlus%1%Months%
  5. "Give me the answer in the format 'four number year, then a dash, then a two-number month'": %ICUDateTimePlus%1%Months%yyyy-MM%

That is... horrible. Horrible for you to create, horrible to maintain, horrible to use (if user has to select from 300 macros it would probably be quicker to do the job manually!).

But you can use variables to greatly reduce the number of macros you need -- maybe even reduce it to just one! For example, if your folder structure is as above you don't need a full set of macros for each company, just one set where you feed the company name into the path. So:

...will make a new folder at ~/Documents/Acme Inc/finance/taxes/2022-11-n-income/.

That doesn't seem much easier -- you still have to set the company name in the first action. But that can be done as well -- the answer to a dialog, by extracting it from the path of the folder you are copying from, even "on Thursdays we process Acme's accounts, today is a Thursday, set Local_companyName to Acme Inc".

So I suggest you start again. A good way is to document a specific case, then see what you can generalise:

  1. Today I want to copy the selected "Income" files to a new folder in the company's taxes folder, named 2022-11-income
  2. The year and month will be the same as the year and month when the macro is run, so I can use ``%ICUDateTime%yyyy-MM%` to generalise that piece
  3. The selected files are in an "Income" folder, which is in folder named for the company -- I can extract both those from the selected files' paths and use them for the destination -- let's call those Local_companyName and Local_recordType ("Income", "Cost", etc)

"Create New Folder at ~/Documents/%Local_companyName%/finance/taxes/%ICUDateTime%yyyy-MM%-n-%Local_recordType%"

That will let you have one macro that will move the selected files to the correct new folder for any company, for any "recordType", when run in any month of any year.

That does, of course, depend on you having some organisation in how the files are placed and/or named. But (almost) anyone dealing with a lot of files will have some kind of organisation in place, it's just a case of latching onto that somehow and using it.

How are these macros to be triggered? You seem to be working with Finder selections, so probably manually? So even if the user doesn't have their files nicely organised you could start the macro with a prompt dialog where they can pick the company name, the record type, and even whether they want to use the current, previous, or next month.

You have loads of options, all of which can be used to reduce both your and the user's workload. They aren't all obvious, or easy, from the KM interface or manual so come up with what you'd like to do and then ask here how.

1 Like