How to add a day every time I launch my macro

I am trying to create a macro that would add one day every time I use it. For example, first use would display today's date. Second use, tomorrow's date. And so on...

I managed to create a macro that displays today's date in the format i want which means, translated in French format, no year, and a capitalized month (12 Janvier).


(The display text action is only here for testing).

I definitely fail at creating a sort of a loop that would add one more day.

I tried to use the Set variable to calculation action with no more luck as I alway get a date in red.

A simple way to do it, without altering your existing Macro too much, is to have the number you arrive at (i.e. Today + 1 etc) as a Variable. You can then use that Variable directly, instead of a fixed number, in your existing first Action. Each time the Macro is run the Variable VAR_Nes will increase by 1 and the date will also be that many days later.

Depending how the Macro is run (i.e. by hotkey) you could have it reset the VAR_Count back to zero again if a particular key is held down when the Macro is run.

EXAMPLE Dates Addition.kmmacros (6.0 KB)

Click to Show Image of Macro

2 Likes

@Zabobon Thank you very much! This is exactly what I was looking for. Not only a working macro but a way of thinking.

I ended up splitting the macro in two. Felt more logical and way easier to use. One macro will set the value of "VAR_Count" to zero. And another macro will add +1 every time it is fired.

To anyone looking for inspiration, here is the final working macro:

When fired:

  • it adds "1" to the variable of "day"
  • translate it in a French format (12 Janvier)
  • paste it to the frontmost application.

I am curious how one can use modifiers. I tried them. It worked. But had to add a 3 seconds pause action to make sure Keyboard Maestro was not already processing the following action :grinning:

1 Like

Glad you got it working.

Just a tip about Variables. Any Variable with "Local" (any case) at the front of its name will only persist during the Macro run and afterwards not be stored. This is why I named your original Variable, "Nes" with Local at the front of its name. This Variable is only needed for the Macro run itself and no need to store it forever cluttering up your Mac.

Gobal Variables persist between runs, which is why I called the "storing" Macro VAR_Count. We want to store this information between runs so that each time the Macro runs it starts with the last stored value until you reset it.

The VAR_ part of the name is an indicator to me that this is a Global Variable. I could also have just called it Nes as you did but that makes it harder to spot in the Macro and risks that you might be calling your Variable the same thing as a Keyboard Maestro Token. For example there is a Keyboard Maestro Token called Application and if you made a Variable called Application that would cause your Macro to fail. You could safely call your own Variable VAR_Application or something like COUNTApplication without fear of conflicts.

So, in your macro it would be better practice not to rename your Variable "NesTranslatedDate" to some name with Local a the front (Local_NesTranslatedDate or LocalNesTranslatedDate or LOCALTranslatedDate)

See here, for a full explanation.

Another tip. I put the Action to check that the Variable VAR_Count is not empty at the start of my example Macro. This is for the very first run. VAR_Count cannot begin to count unless it already exists since it is adding numbers to itself. After the first run this first Action won't do anything. But it is good practice to leave that Action there, so that the Macro will work even on another computer or if for some reason the Variable has been deleted.

image

1 Like

Thank you for those much needed explanations! I had to come back to this post to understand why my macros where not working as expected. And VAR and Local were the culprits.

You may already know this, but there's a way to assign zero to an empty variable AND at the same time add 1 to the variable if it already exists, all in a single action. Here's what it looks like:

image

I suppose you could argue that this is a "trick", but it does work. (It may not work with negative numbers, but "counts" are usually positive.)

1 Like